MCPcopy Create free account
hub / github.com/Bwar/CJsonObject / parse_number

Function parse_number

cJSON.c:119–183  ·  view source on GitHub ↗

Parse the input text to generate a number, and populate the result into item. */

Source from the content-addressed store, hash-verified

117
118/* Parse the input text to generate a number, and populate the result into item. */
119static const char *parse_number(cJSON *item, const char *num)
120{
121 int64 n = 0;
122 double d = 0.0;
123 double base = 0.0;
124 double point = 0.1;
125 int scale = 0.0;
126 int subscale = 0;
127 int signsubscale = 1;
128 item->sign = 1;
129
130 /* Could use sscanf for this? */
131 if (*num == '-')
132 item->sign = -1, num++; /* Has sign? */
133 if (*num == '0')
134 num++; /* is zero */
135 while (*num >= '0' && *num <= '9')
136 {
137 n = (n * 10) + (*num++ - '0');
138 }
139 d = n;
140 if (*num == '.' && num[1] >= '0' && num[1] <= '9')
141 {
142 num++;
143 base = d;
144 do
145 {
146 d += point * (*num - '0');
147 point *= 0.1;
148 base = (base * 10.0) + (*num - '0'), scale--;
149 ++num;
150 }
151 while (*num >= '0' && *num <= '9');
152 } /* Fractional part? */
153 if (*num == 'e' || *num == 'E') /* Exponent? */
154 {
155 num++;
156 if (*num == '+')
157 num++;
158 else if (*num == '-')
159 signsubscale = -1, num++; /* With sign? */
160 while (*num >= '0' && *num <= '9')
161 subscale = (subscale * 10) + (*num++ - '0'); /* Number? */
162 }
163
164 if (scale == 0 && subscale == 0)
165 {
166 item->valuedouble = item->sign * d;
167 item->valueint = item->sign * n;
168 item->type = cJSON_Int;
169 }
170 else
171 {
172 printf("subscale = %d, signsubscale = %d, s = %.16f\n", subscale, signsubscale, d);
173 if (subscale != 0)
174 {
175 d = item->sign * base * pow(10.0, (scale + subscale * signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
176 }

Callers 1

parse_valueFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected