mirror of
https://github.com/lua/lua
synced 2024-11-25 22:29:39 +03:00
'luaO_str2int' more generic: accepts white spaces around the numeral
and handles signal
This commit is contained in:
parent
27f09415e3
commit
36e8771076
5
llex.c
5
llex.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.c,v 2.64 2013/04/16 18:46:28 roberto Exp roberto $
|
||||
** $Id: llex.c,v 2.65 2013/04/26 13:07:53 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -248,7 +248,8 @@ static int read_numeral (LexState *ls, SemInfo *seminfo, int isf) {
|
||||
}
|
||||
save(ls, '\0');
|
||||
if (!isf) {
|
||||
if (!luaO_str2int(luaZ_buffer(ls->buff), &seminfo->i))
|
||||
if (!luaO_str2int(luaZ_buffer(ls->buff), luaZ_bufflen(ls->buff) - 1,
|
||||
&seminfo->i))
|
||||
lexerror(ls, "malformed number", TK_INT);
|
||||
return TK_INT;
|
||||
}
|
||||
|
33
lobject.c
33
lobject.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 2.61 2013/04/29 16:57:28 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 2.62 2013/05/02 12:37:24 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -133,11 +133,6 @@ int luaO_hexavalue (int c) {
|
||||
}
|
||||
|
||||
|
||||
#if !defined(lua_strx2number)
|
||||
|
||||
#include <math.h>
|
||||
|
||||
|
||||
static int isneg (const char **s) {
|
||||
if (**s == '-') { (*s)++; return 1; }
|
||||
else if (**s == '+') (*s)++;
|
||||
@ -145,6 +140,11 @@ static int isneg (const char **s) {
|
||||
}
|
||||
|
||||
|
||||
#if !defined(lua_strx2number)
|
||||
|
||||
#include <math.h>
|
||||
|
||||
|
||||
static lua_Number readhexa (const char **s, lua_Number r, int *count) {
|
||||
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
|
||||
r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
|
||||
@ -212,21 +212,32 @@ int luaO_str2d (const char *s, size_t len, lua_Number *result) {
|
||||
}
|
||||
|
||||
|
||||
int luaO_str2int (const char *s, lua_Integer *result) {
|
||||
int luaO_str2int (const char *s, size_t len, lua_Integer *result) {
|
||||
const char *ends = s + len;
|
||||
lua_Unsigned a = 0;
|
||||
int empty = 1;
|
||||
int neg;
|
||||
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
||||
neg = isneg(&s);
|
||||
if (s[0] == '0' &&
|
||||
(s[1] == 'x' || s[1] == 'X')) { /* hexa? */
|
||||
s += 2; /* skip '0x' */
|
||||
for (; lisxdigit(cast_uchar(*s)); s++)
|
||||
for (; lisxdigit(cast_uchar(*s)); s++) {
|
||||
a = a * 16 + luaO_hexavalue(cast_uchar(*s));
|
||||
empty = 0;
|
||||
}
|
||||
}
|
||||
else { /* decimal */
|
||||
for (; lisdigit(cast_uchar(*s)); s++)
|
||||
for (; lisdigit(cast_uchar(*s)); s++) {
|
||||
a = a * 10 + luaO_hexavalue(cast_uchar(*s));
|
||||
empty = 0;
|
||||
}
|
||||
}
|
||||
if (*s != '\0') return 0; /* something wrong in the numeral */
|
||||
while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
|
||||
if (empty || s != ends) return 0; /* something wrong in the numeral */
|
||||
else {
|
||||
*result = cast(lua_Integer, a);
|
||||
if (neg) *result = -cast(lua_Integer, a);
|
||||
else *result = cast(lua_Integer, a);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 2.76 2013/05/02 12:37:24 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 2.77 2013/05/06 17:17:09 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -496,7 +496,7 @@ LUAI_FUNC int luaO_ceillog2 (unsigned int x);
|
||||
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
|
||||
const TValue *p2, TValue *res);
|
||||
LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
|
||||
LUAI_FUNC int luaO_str2int (const char *s, lua_Integer *result);
|
||||
LUAI_FUNC int luaO_str2int (const char *s, size_t len, lua_Integer *result);
|
||||
LUAI_FUNC int luaO_hexavalue (int c);
|
||||
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
|
||||
va_list argp);
|
||||
|
Loading…
Reference in New Issue
Block a user