aboutsummaryrefslogtreecommitdiff
path: root/lobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'lobject.c')
-rw-r--r--lobject.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/lobject.c b/lobject.c
index 223bbd0c..f8ea917a 100644
--- a/lobject.c
+++ b/lobject.c
@@ -220,32 +220,37 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
220#define L_MAXLENNUM 200 220#define L_MAXLENNUM 200
221#endif 221#endif
222 222
223/*
224** Convert string 's' to a Lua number (put in 'result'). Return NULL on
225** fail or the address of the ending '\0' on success. ('mode' == 'x')
226** means a hexadecimal numeral.
227*/
223static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { 228static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
224 char *endptr; 229 char *endptr;
225 *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ 230 *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
226 : lua_str2number(s, &endptr); 231 : lua_str2number(s, &endptr);
227 if (endptr == s) return NULL; /* nothing recognized? */ 232 if (endptr == s) return NULL; /* nothing recognized? */
228 while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ 233 while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
229 return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */ 234 return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */
230} 235}
231 236
232 237
233/* 238/*
234** Convert string 's' to a Lua number (put in 'result'). Return NULL 239** Convert string 's' to a Lua number (put in 'result') handling the
235** on fail or the address of the ending '\0' on success. 240** current locale.
236** 'pmode' points to (and 'mode' contains) special things in the string:
237** - 'x'/'X' means a hexadecimal numeral
238** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
239** - '.' just optimizes the search for the common case (nothing special)
240** This function accepts both the current locale or a dot as the radix 241** This function accepts both the current locale or a dot as the radix
241** mark. If the conversion fails, it may mean number has a dot but 242** mark. If the conversion fails, it may mean number has a dot but
242** locale accepts something else. In that case, the code copies 's' 243** locale accepts something else. In that case, the code copies 's'
243** to a buffer (because 's' is read-only), changes the dot to the 244** to a buffer (because 's' is read-only), changes the dot to the
244** current locale radix mark, and tries to convert again. 245** current locale radix mark, and tries to convert again.
246** The variable 'mode' checks for special characters in the string:
247** - 'n' means 'inf' or 'nan' (which should be rejected)
248** - 'x' means a hexadecimal numeral
249** - '.' just optimizes the search for the common case (no special chars)
245*/ 250*/
246static const char *l_str2d (const char *s, lua_Number *result) { 251static const char *l_str2d (const char *s, lua_Number *result) {
247 const char *endptr; 252 const char *endptr;
248 const char *pmode = strpbrk(s, ".xXnN"); 253 const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
249 int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; 254 int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
250 if (mode == 'n') /* reject 'inf' and 'nan' */ 255 if (mode == 'n') /* reject 'inf' and 'nan' */
251 return NULL; 256 return NULL;