** Convert string 's' to a Lua number (put in 'result') handling the ** current locale. ** This function accepts both the current locale or a dot as the radix ** mark. If the conversion fails, it may mean number has a dot but ** locale accepts something else. In that case, the code copies 's' ** to a buffer (because 's' is read-only), changes the dot to the ** current locale radix mark, and tries
| 249 | ** - '.' just optimizes the search for the common case (no special chars) |
| 250 | */ |
| 251 | static const char *l_str2d (const char *s, lua_Number *result) { |
| 252 | const char *endptr; |
| 253 | const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ |
| 254 | int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; |
| 255 | if (mode == 'n') /* reject 'inf' and 'nan' */ |
| 256 | return NULL; |
| 257 | endptr = l_str2dloc(s, result, mode); /* try to convert */ |
| 258 | if (endptr == NULL) { /* failed? may be a different locale */ |
| 259 | char buff[L_MAXLENNUM + 1]; |
| 260 | const char *pdot = strchr(s, '.'); |
| 261 | if (pdot == NULL || strlen(s) > L_MAXLENNUM) |
| 262 | return NULL; /* string too long or no dot; fail */ |
| 263 | strcpy(buff, s); /* copy string to buffer */ |
| 264 | buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ |
| 265 | endptr = l_str2dloc(buff, result, mode); /* try again */ |
| 266 | if (endptr != NULL) |
| 267 | endptr = s + (endptr - buff); /* make relative to 's' */ |
| 268 | } |
| 269 | return endptr; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) |
no test coverage detected