MCPcopy Create free account
hub / github.com/bblanchon/ArduinoJson / parseNumber

Function parseNumber

src/ArduinoJson/Numbers/parseNumber.hpp:105–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

103};
104
105inline Number parseNumber(const char* s) {
106 using traits = FloatTraits<JsonFloat>;
107 using mantissa_t = largest_type<traits::mantissa_type, JsonUInt>;
108 using exponent_t = traits::exponent_type;
109
110 ARDUINOJSON_ASSERT(s != 0);
111
112 bool is_negative = false;
113 switch (*s) {
114 case '-':
115 is_negative = true;
116 s++;
117 break;
118 case '+':
119 s++;
120 break;
121 }
122
123#if ARDUINOJSON_ENABLE_NAN
124 if (*s == 'n' || *s == 'N') {
125 return Number(traits::nan());
126 }
127#endif
128
129#if ARDUINOJSON_ENABLE_INFINITY
130 if (*s == 'i' || *s == 'I') {
131 return Number(is_negative ? -traits::inf() : traits::inf());
132 }
133#endif
134
135 if (!isdigit(*s) && *s != '.')
136 return Number();
137
138 mantissa_t mantissa = 0;
139 exponent_t exponent_offset = 0;
140 const mantissa_t maxUint = JsonUInt(-1);
141
142 while (isdigit(*s)) {
143 uint8_t digit = uint8_t(*s - '0');
144 if (mantissa > maxUint / 10)
145 break;
146 mantissa *= 10;
147 if (mantissa > maxUint - digit)
148 break;
149 mantissa += digit;
150 s++;
151 }
152
153 if (*s == '\0') {
154 if (is_negative) {
155 const mantissa_t sintMantissaMax = mantissa_t(1)
156 << (sizeof(JsonInteger) * 8 - 1);
157 if (mantissa <= sintMantissaMax) {
158 return Number(JsonInteger(~mantissa + 1));
159 }
160 } else {
161 return Number(JsonUInt(mantissa));
162 }

Callers 5

parseNumericValueFunction · 0.85
checkFloatFunction · 0.85
parseNumber.cppFile · 0.85
checkFloatFunction · 0.85
use_double_0.cppFile · 0.85

Calls 3

NumberClass · 0.85
isdigitFunction · 0.85
make_floatFunction · 0.85

Tested by 2

checkFloatFunction · 0.68
checkFloatFunction · 0.68