| 103 | }; |
| 104 | |
| 105 | inline 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 | } |