MCPcopy Create free account
hub / github.com/ByConity/ByConity / readFloatText

Function readFloatText

base/common/JSON.cpp:99–163  ·  view source on GitHub ↗

Прочитать число с плавающей запятой в простом формате, с грубым округлением, из не-0-terminated строки.

Source from the content-addressed store, hash-verified

97
98/// Прочитать число с плавающей запятой в простом формате, с грубым округлением, из не-0-terminated строки.
99static double readFloatText(const char * buf, const char * end)
100{
101 bool negative = false;
102 double x = 0;
103 bool after_point = false;
104 double power_of_ten = 1;
105
106 if (buf == end)
107 throw JSONException("JSON: cannot parse floating point number: unexpected end of data.");
108
109 bool run = true;
110 while (buf != end && run)
111 {
112 switch (*buf)
113 {
114 case '+':
115 break;
116 case '-':
117 negative = true;
118 break;
119 case '.':
120 after_point = true;
121 break;
122 case '0':
123 case '1':
124 case '2':
125 case '3':
126 case '4':
127 case '5':
128 case '6':
129 case '7':
130 case '8':
131 case '9':
132 if (after_point)
133 {
134 power_of_ten /= 10;
135 x += (*buf - '0') * power_of_ten;
136 }
137 else
138 {
139 x *= 10;
140 x += *buf - '0';
141 }
142 break;
143 case 'e':
144 case 'E':
145 {
146 ++buf;
147 Int32 exponent = readIntText(buf, end);
148 x *= preciseExp10(exponent);
149
150 run = false;
151 break;
152 }
153 default:
154 run = false;
155 break;
156 }

Callers 1

getDoubleMethod · 0.70

Calls 2

preciseExp10Function · 0.85
readIntTextFunction · 0.70

Tested by

no test coverage detected