MCPcopy Create free account
hub / github.com/Tencent/TAD_Sim / parse_number

Function parse_number

common/map_sdk/transmission/src/cJSON.cpp:92–135  ·  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

90
91/* Parse the input text to generate a number, and populate the result into item. */
92static const char *parse_number(cJSON *item, const char *num) {
93 long double n = 0, scale = 0;
94 int subscale = 0, signsubscale = 1;
95 item->sign = 1;
96
97 /* Could use sscanf for this? */
98 if (*num == '-') item->sign = -1, num++; /* Has sign? */
99 if (*num == '0') num++; /* is zero */
100 if (*num >= '1' && *num <= '9') {
101 do {
102 n = (n * 10.0) + (*num++ - '0');
103 } while (*num >= '0' && *num <= '9'); /* Number? */
104 }
105 if (*num == '.' && num[1] >= '0' && num[1] <= '9') {
106 num++;
107 do {
108 n = (n * 10.0) + (*num++ - '0'), scale--;
109 } while (*num >= '0' && *num <= '9');
110 } /* Fractional part? */
111 if (*num == 'e' || *num == 'E') { /* Exponent? */
112 num++;
113 if (*num == '+') {
114 num++;
115 } else if (*num == '-') {
116 signsubscale = -1, num++;
117 } /* With sign? */
118 while (*num >= '0' && *num <= '9') {
119 subscale = (subscale * 10) + (*num++ - '0');
120 } /* Number? */
121 }
122
123 if (scale == 0 && subscale == 0) {
124 item->valuedouble = static_cast<double>(item->sign * static_cast<uint64>(n));
125 item->valueint = static_cast<uint64>(item->sign * static_cast<uint64>(n));
126 item->type = cJSON_Int;
127 } else {
128 n = item->sign * n *
129 pow(10.0, (scale + subscale * signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
130 item->valuedouble = static_cast<double>(n);
131 item->valueint = static_cast<uint64>(n);
132 item->type = cJSON_Double;
133 }
134 return num;
135}
136
137/* Render the number nicely from the given item into a string. */
138static char *print_double(cJSON *item) {

Callers 1

parse_valueFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected