| 337 | #define MAXLASTD cast_int(LUA_MAXINTEGER % 10) |
| 338 | |
| 339 | static const char *l_str2int (const char *s, lua_Integer *result) { |
| 340 | lua_Unsigned a = 0; |
| 341 | int empty = 1; |
| 342 | int neg; |
| 343 | while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ |
| 344 | neg = isneg(&s); |
| 345 | if (s[0] == '0' && |
| 346 | (s[1] == 'x' || s[1] == 'X')) { /* hex? */ |
| 347 | s += 2; /* skip '0x' */ |
| 348 | for (; lisxdigit(cast_uchar(*s)); s++) { |
| 349 | a = a * 16 + luaO_hexavalue(*s); |
| 350 | empty = 0; |
| 351 | } |
| 352 | } |
| 353 | else { /* decimal */ |
| 354 | for (; lisdigit(cast_uchar(*s)); s++) { |
| 355 | int d = *s - '0'; |
| 356 | if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ |
| 357 | return NULL; /* do not accept it (as integer) */ |
| 358 | a = a * 10 + cast_uint(d); |
| 359 | empty = 0; |
| 360 | } |
| 361 | } |
| 362 | while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ |
| 363 | if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ |
| 364 | else { |
| 365 | *result = l_castU2S((neg) ? 0u - a : a); |
| 366 | return s; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | |
| 371 | size_t luaO_str2num (const char *s, TValue *o) { |
no test coverage detected