aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-01 15:18:06 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-01 15:18:06 -0300
commitc549d4fe64c48ab645740e6d12c69c91250fad3d (patch)
tree1f98183ee7518f107d5902916468d99d7fce9527 /lbaselib.c
parentddff6ecf305e6da4f275b473ef6b66811848a3c6 (diff)
downloadlua-c549d4fe64c48ab645740e6d12c69c91250fad3d.tar.gz
lua-c549d4fe64c48ab645740e6d12c69c91250fad3d.tar.bz2
lua-c549d4fe64c48ab645740e6d12c69c91250fad3d.zip
'lua_strtonum' (and 'luaO_str2num') now return string size, instead of
receiving it
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/lbaselib.c b/lbaselib.c
index e0e6a66c..5b22a1fc 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.284 2014/02/14 16:45:38 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.285 2014/03/12 20:57:40 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -45,31 +45,31 @@ static int luaB_print (lua_State *L) {
45 45
46#define SPACECHARS " \f\n\r\t\v" 46#define SPACECHARS " \f\n\r\t\v"
47 47
48static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) { 48static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
49 lua_Unsigned n = 0; 49 lua_Unsigned n = 0;
50 int neg = 0; 50 int neg = 0;
51 s += strspn(s, SPACECHARS); /* skip initial spaces */ 51 s += strspn(s, SPACECHARS); /* skip initial spaces */
52 if (*s == '-') { s++; neg = 1; } /* handle signal */ 52 if (*s == '-') { s++; neg = 1; } /* handle signal */
53 else if (*s == '+') s++; 53 else if (*s == '+') s++;
54 if (!isalnum((unsigned char)*s)) /* no digit? */ 54 if (!isalnum((unsigned char)*s)) /* no digit? */
55 return 0; 55 return NULL;
56 do { 56 do {
57 int digit = (isdigit((unsigned char)*s)) ? *s - '0' 57 int digit = (isdigit((unsigned char)*s)) ? *s - '0'
58 : toupper((unsigned char)*s) - 'A' + 10; 58 : toupper((unsigned char)*s) - 'A' + 10;
59 if (digit >= base) return 0; /* invalid numeral */ 59 if (digit >= base) return NULL; /* invalid numeral */
60 n = n * base + digit; 60 n = n * base + digit;
61 s++; 61 s++;
62 } while (isalnum((unsigned char)*s)); 62 } while (isalnum((unsigned char)*s));
63 s += strspn(s, SPACECHARS); /* skip trailing spaces */ 63 s += strspn(s, SPACECHARS); /* skip trailing spaces */
64 if (s != e) /* invalid trailing characters? */ 64 if (*s != '\0') /* invalid trailing characters? */
65 return 0; 65 return NULL;
66 *pn = (lua_Integer)((neg) ? (0u - n) : n); 66 *pn = (lua_Integer)((neg) ? (0u - n) : n);
67 return 1; 67 return s;
68} 68}
69 69
70 70
71static int luaB_tonumber (lua_State *L) { 71static int luaB_tonumber (lua_State *L) {
72 if (lua_isnoneornil(L, 2)) { /* standard conversion */ 72 if (lua_isnoneornil(L, 2)) { /* standard conversion? */
73 luaL_checkany(L, 1); 73 luaL_checkany(L, 1);
74 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ 74 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
75 lua_settop(L, 1); /* yes; return it */ 75 lua_settop(L, 1); /* yes; return it */
@@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) {
78 else { 78 else {
79 size_t l; 79 size_t l;
80 const char *s = lua_tolstring(L, 1, &l); 80 const char *s = lua_tolstring(L, 1, &l);
81 if (s != NULL && lua_strtonum(L, s, l)) /* can convert to a number? */ 81 if (s != NULL && lua_strtonum(L, s) == l + 1)
82 return 1; 82 return 1; /* successful conversion to number */
83 /* else not a number */ 83 /* else not a number */
84 } 84 }
85 } 85 }
86 else { 86 else {
87 size_t l; 87 size_t l;
88 const char *s; 88 const char *s;
89 lua_Integer n; 89 lua_Integer n = 0; /* to avoid warnings */
90 int base = luaL_checkint(L, 2); 90 int base = luaL_checkint(L, 2);
91 luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ 91 luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
92 s = luaL_checklstring(L, 1, &l); 92 s = luaL_checklstring(L, 1, &l);
93 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 93 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
94 if (b_str2int(s, s + l, base, &n)) { 94 if (b_str2int(s, base, &n) == s + l) {
95 lua_pushinteger(L, n); 95 lua_pushinteger(L, n);
96 return 1; 96 return 1;
97 } /* else not a number */ 97 } /* else not a number */