| 58 | #define SPACECHARS " \f\n\r\t\v" |
| 59 | |
| 60 | static const char *b_str2int (const char *s, int base, lua_Integer *pn) { |
| 61 | lua_Unsigned n = 0; |
| 62 | int neg = 0; |
| 63 | s += strspn(s, SPACECHARS); /* skip initial spaces */ |
| 64 | if (*s == '-') { s++; neg = 1; } /* handle sign */ |
| 65 | else if (*s == '+') s++; |
| 66 | if (!isalnum((unsigned char)*s)) /* no digit? */ |
| 67 | return NULL; |
| 68 | do { |
| 69 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' |
| 70 | : (toupper((unsigned char)*s) - 'A') + 10; |
| 71 | if (digit >= base) return NULL; /* invalid numeral */ |
| 72 | n = n * base + digit; |
| 73 | s++; |
| 74 | } while (isalnum((unsigned char)*s)); |
| 75 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ |
| 76 | *pn = (lua_Integer)((neg) ? (0u - n) : n); |
| 77 | return s; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static int luaB_tonumber (lua_State *L) { |