issue #193 Lua 5.4 string library improvements back-ported

lua54-strings
Dibyendu Majumdar 4 years ago
parent a2e0c2fc3d
commit d2b9810a06

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.h,v 1.131.1.1 2017/04/19 17:20:42 roberto Exp $
** $Id: lauxlib.h $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -15,6 +15,12 @@
#include "lua.h"
/* global table */
#define LUA_GNAME "_G"
typedef struct luaL_Buffer luaL_Buffer;
/* extra error code for 'luaL_loadfilex' */
#define LUA_ERRFILE (LUA_ERRERR+1)
@ -48,6 +54,7 @@ LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
api function ravi_typename() */
LUALIB_API const char *(raviL_tolstring) (lua_State *L, int idx, size_t *len);
LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg);
LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname);
LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg,
size_t *l);
LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg,
@ -77,6 +84,7 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
/* predefined references */
#define LUA_NOREF (-2)
#define LUA_REFNIL (-1)
@ -97,8 +105,10 @@ LUALIB_API lua_State *(luaL_newstate) (void);
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
const char *r);
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
const char *p, const char *r);
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
const char *p, const char *r);
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
@ -125,6 +135,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
#define luaL_argcheck(L, cond,arg,extramsg) \
((void)((cond) || luaL_argerror(L, (arg), (extramsg))))
#define luaL_argexpected(L,cond,arg,tname) \
((void)((cond) || luaL_typeerror(L, (arg), (tname))))
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))

@ -33,6 +33,7 @@ typedef long l_mem;
/* chars used as small naturals (so that 'char' is reserved for characters) */
typedef unsigned char lu_byte;
typedef signed char ls_byte;
/* maximum value for size_t */
@ -121,10 +122,15 @@ typedef LUAI_UACINT l_uacInt;
#define cast(t, exp) ((t)(exp))
#define cast_void(i) cast(void, (i))
#define cast_byte(i) cast(lu_byte, (i))
#define cast_voidp(i) cast(void *, (i))
#define cast_num(i) cast(lua_Number, (i))
#define cast_int(i) cast(int, (i))
#define cast_uint(i) cast(unsigned int, (i))
#define cast_byte(i) cast(lu_byte, (i))
#define cast_uchar(i) cast(unsigned char, (i))
#define cast_char(i) cast(char, (i))
#define cast_charp(i) cast(char *, (i))
#define cast_sizet(i) cast(size_t, (i))
/* cast a signed lua_Integer to lua_Unsigned */
@ -142,6 +148,22 @@ typedef LUAI_UACINT l_uacInt;
#endif
/*
** macros to improve jump prediction (used mainly for error handling)
*/
#if !defined(likely)
#if defined(__GNUC__)
#define likely(x) (__builtin_expect(((x) != 0), 1))
#define unlikely(x) (__builtin_expect(((x) != 0), 0))
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#endif
/*
** non-return type
*/

@ -76,7 +76,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
if (findfield(L, top + 1, 2)) {
const char *name = lua_tostring(L, -1);
if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */
lua_pushstring(L, name + 3); /* push name without prefix */
lua_remove(L, -2); /* remove original name */
}
@ -179,7 +179,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
}
static int typeerror (lua_State *L, int arg, const char *tname) {
int luaL_typeerror (lua_State *L, int arg, const char *tname) {
const char *msg;
const char *typearg; /* name for the type of the actual argument */
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
@ -194,7 +194,7 @@ static int typeerror (lua_State *L, int arg, const char *tname) {
static void tag_error (lua_State *L, int arg, int tag) {
typeerror(L, arg, lua_typename(L, tag));
luaL_typeerror(L, arg, lua_typename(L, tag));
}
@ -332,7 +332,7 @@ LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
void *p = luaL_testudata(L, ud, tname);
if (p == NULL) typeerror(L, ud, tname);
luaL_argexpected(L, p != NULL, ud, tname);
return p;
}
@ -1017,18 +1017,24 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
}
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
const char *r) {
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
const char *p, const char *r) {
const char *wild;
size_t l = strlen(p);
luaL_Buffer b;
luaL_buffinit(L, &b);
while ((wild = strstr(s, p)) != NULL) {
luaL_addlstring(&b, s, wild - s); /* push prefix */
luaL_addstring(&b, r); /* push replacement in place of pattern */
luaL_addlstring(b, s, wild - s); /* push prefix */
luaL_addstring(b, r); /* push replacement in place of pattern */
s = wild + l; /* continue after 'p' */
}
luaL_addstring(&b, s); /* push last suffix */
luaL_addstring(b, s); /* push last suffix */
}
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
const char *p, const char *r) {
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addgsub(&b, s, p, r);
luaL_pushresult(&b);
return lua_tostring(L, -1);
}

@ -637,8 +637,7 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
const TValue *p2, const char *msg) {
lua_Number temp;
if (!tonumber(p1, &temp)) /* first operand is wrong? */
if (!ttisnumber(p1)) /* first operand is wrong? */
p2 = p1; /* now second is wrong */
luaG_typeerror(L, p2, msg);
}
@ -649,7 +648,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1,
*/
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
lua_Integer temp;
if (!tointeger(p1, &temp))
if (!tointegerns(p1, &temp))
p2 = p1;
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
}

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.259 2017/11/16 13:19:06 roberto Exp roberto $
** $Id: lstrlib.c $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -60,23 +60,50 @@ static int str_len (lua_State *L) {
}
/* translate a relative string position: negative means back from end */
static lua_Integer posrelat (lua_Integer pos, size_t len) {
if (pos >= 0) return pos;
else if (0u - (size_t)pos > len) return 0;
else return (lua_Integer)len + pos + 1;
/*
** translate a relative initial string position
** (negative means back from end): clip result to [1, inf).
** The length of any string in Lua must fit in a lua_Integer,
** so there are no overflows in the casts.
** The inverted comparison avoids a possible overflow
** computing '-pos'.
*/
static size_t posrelatI (lua_Integer pos, size_t len) {
if (pos > 0)
return (size_t)pos;
else if (pos == 0)
return 1;
else if (pos < -(lua_Integer)len) /* inverted comparison */
return 1; /* clip to 1 */
else return len + (size_t)pos + 1;
}
/*
** Gets an optional ending string position from argument 'arg',
** with default value 'def'.
** Negative means back from end: clip result to [0, len]
*/
static size_t getendpos (lua_State *L, int arg, lua_Integer def,
size_t len) {
lua_Integer pos = luaL_optinteger(L, arg, def);
if (pos > (lua_Integer)len)
return len;
else if (pos >= 0)
return (size_t)pos;
else if (pos < -(lua_Integer)len)
return 0;
else return len + (size_t)pos + 1;
}
static int str_sub (lua_State *L) {
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
lua_Integer start = posrelat(luaL_checkinteger(L, 2), l);
lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l);
if (start < 1) start = 1;
if (end > (lua_Integer)l) end = l;
size_t start = posrelatI(luaL_checkinteger(L, 2), l);
size_t end = getendpos(L, 3, -1, l);
if (start <= end)
lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1);
lua_pushlstring(L, s + start - 1, (end - start) + 1);
else lua_pushliteral(L, "");
return 1;
}
@ -149,13 +176,12 @@ static int str_rep (lua_State *L) {
static int str_byte (lua_State *L) {
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l);
lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l);
lua_Integer pi = luaL_optinteger(L, 2, 1);
size_t posi = posrelatI(pi, l);
size_t pose = getendpos(L, 3, pi, l);
int n, i;
if (posi < 1) posi = 1;
if (pose > (lua_Integer)l) pose = l;
if (posi > pose) return 0; /* empty interval; return no values */
if (pose - posi >= INT_MAX) /* arithmetic overflow? */
if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */
return luaL_error(L, "string slice too long");
n = (int)(pose - posi) + 1;
luaL_checkstack(L, n, "string slice too long");
@ -171,8 +197,8 @@ static int str_char (lua_State *L) {
luaL_Buffer b;
char *p = luaL_buffinitsize(L, &b, n);
for (i=1; i<=n; i++) {
lua_Integer c = luaL_checkinteger(L, i);
luaL_argcheck(L, uchar(c) == c, i, "value out of range");
lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
p[i - 1] = uchar(c);
}
luaL_pushresultsize(&b, n);
@ -180,22 +206,38 @@ static int str_char (lua_State *L) {
}
static int writer (lua_State *L, const void *b, size_t size, void *B) {
(void)L;
luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
/*
** Buffer to store the result of 'string.dump'. It must be initialized
** after the call to 'lua_dump', to ensure that the function is on the
** top of the stack when 'lua_dump' is called. ('luaL_buffinit' might
** push stuff.)
*/
struct str_Writer {
int init; /* true iff buffer has been initialized */
luaL_Buffer B;
};
static int writer (lua_State *L, const void *b, size_t size, void *ud) {
struct str_Writer *state = (struct str_Writer *)ud;
if (!state->init) {
state->init = 1;
luaL_buffinit(L, &state->B);
}
luaL_addlstring(&state->B, (const char *)b, size);
return 0;
}
static int str_dump (lua_State *L) {
luaL_Buffer b;
struct str_Writer state;
int strip = lua_toboolean(L, 2);
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, 1);
luaL_buffinit(L,&b);
if (lua_dump(L, writer, &b, strip) != 0)
lua_settop(L, 1); /* ensure function is on the top of the stack */
state.init = 0;
if (lua_dump(L, writer, &state, strip) != 0)
return luaL_error(L, "unable to dump given function");
luaL_pushresult(&b);
luaL_pushresult(&state.B);
return 1;
}
@ -207,6 +249,17 @@ static int str_dump (lua_State *L) {
** =======================================================
*/
#if defined(LUA_NOCVTS2N) /* { */
/* no coercion from strings to numbers */
static const luaL_Reg stringmetamethods[] = {
{"__index", NULL}, /* placeholder */
{NULL, NULL}
};
#else /* }{ */
static int tonum (lua_State *L, int arg) {
if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */
lua_pushvalue(L, arg);
@ -220,25 +273,6 @@ static int tonum (lua_State *L, int arg) {
}
static int toint (lua_State *L, int arg) {
if (!tonum(L, arg))
return 0; /* not coercible to a number */
else if (lua_isinteger(L, arg))
return 1; /* already an integer */
else { /* a float */
int ok;
lua_Integer n = lua_tointegerx(L, arg, &ok);
if (!ok)
return 0;
else {
lua_pop(L, 1); /* remove the float */
lua_pushinteger(L, n); /* push an integer */
return 1;
}
}
}
static void trymt (lua_State *L, const char *mtname) {
lua_settop(L, 2); /* back to the original arguments */
if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname))
@ -258,15 +292,6 @@ static int arith (lua_State *L, int op, const char *mtname) {
}
static int bitwise (lua_State *L, int op, const char *mtname) {
if (toint(L, 1) && toint(L, 2))
lua_arith(L, op); /* result will be on the top */
else
trymt(L, mtname);
return 1;
}
static int arith_add (lua_State *L) {
return arith(L, LUA_OPADD, "__add");
}
@ -299,30 +324,6 @@ static int arith_unm (lua_State *L) {
return arith(L, LUA_OPUNM, "__unm");
}
static int bitwise_band (lua_State *L) {
return bitwise(L, LUA_OPBAND, "__band");
}
static int bitwise_bor (lua_State *L) {
return bitwise(L, LUA_OPBOR, "__bor");
}
static int bitwise_bxor (lua_State *L) {
return bitwise(L, LUA_OPBXOR, "__bxor");
}
static int bitwise_shl (lua_State *L) {
return bitwise(L, LUA_OPSHL, "__shl");
}
static int bitwise_shr (lua_State *L) {
return bitwise(L, LUA_OPSHR, "__shr");
}
static int bitwise_bnot (lua_State *L) {
return bitwise(L, LUA_OPBNOT, "__bnot");
}
static const luaL_Reg stringmetamethods[] = {
{"__add", arith_add},
@ -333,16 +334,11 @@ static const luaL_Reg stringmetamethods[] = {
{"__div", arith_div},
{"__idiv", arith_idiv},
{"__unm", arith_unm},
{"__band", bitwise_band},
{"__bor", bitwise_bor},
{"__bxor", bitwise_bxor},
{"__shl", bitwise_shl},
{"__shr", bitwise_shr},
{"__bnot", bitwise_bnot},
{"__index", NULL}, /* placeholder */
{NULL, NULL}
};
#endif /* } */
/* }====================================================== */
@ -693,25 +689,46 @@ static const char *lmemfind (const char *s1, size_t l1,
}
static void push_onecapture (MatchState *ms, int i, const char *s,
const char *e) {
/*
** get information about the i-th capture. If there are no captures
** and 'i==0', return information about the whole match, which
** is the range 's'..'e'. If the capture is a string, return
** its length and put its address in '*cap'. If it is an integer
** (a position), push it on the stack and return CAP_POSITION.
*/
static size_t get_onecapture (MatchState *ms, int i, const char *s,
const char *e, const char **cap) {
if (i >= ms->level) {
if (i == 0) /* ms->level == 0, too */
lua_pushlstring(ms->L, s, e - s); /* add whole match */
else
if (i != 0)
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
*cap = s;
return e - s;
}
else {
ptrdiff_t l = ms->capture[i].len;
if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
if (l == CAP_POSITION)
ptrdiff_t capl = ms->capture[i].len;
*cap = ms->capture[i].init;
if (capl == CAP_UNFINISHED)
luaL_error(ms->L, "unfinished capture");
else if (capl == CAP_POSITION)
lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
else
lua_pushlstring(ms->L, ms->capture[i].init, l);
return capl;
}
}
/*
** Push the i-th capture on the stack.
*/
static void push_onecapture (MatchState *ms, int i, const char *s,
const char *e) {
const char *cap;
ptrdiff_t l = get_onecapture(ms, i, s, e, &cap);
if (l != CAP_POSITION)
lua_pushlstring(ms->L, cap, l);
/* else position was already pushed */
}
static int push_captures (MatchState *ms, const char *s, const char *e) {
int i;
int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
@ -754,16 +771,15 @@ static int str_find_aux (lua_State *L, int find) {
size_t ls, lp;
const char *s = luaL_checklstring(L, 1, &ls);
const char *p = luaL_checklstring(L, 2, &lp);
lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
if (init < 1) init = 1;
else if (init > (lua_Integer)ls + 1) { /* start after string's end? */
lua_pushnil(L); /* cannot find anything */
size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
if (init > ls) { /* start after string's end? */
luaL_pushfail(L); /* cannot find anything */
return 1;
}
/* explicit request or no special characters? */
if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
/* do a plain search */
const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
const char *s2 = lmemfind(s + init, ls - init, p, lp);
if (s2) {
lua_pushinteger(L, (s2 - s) + 1);
lua_pushinteger(L, (s2 - s) + lp);
@ -772,7 +788,7 @@ static int str_find_aux (lua_State *L, int find) {
}
else {
MatchState ms;
const char *s1 = s + init - 1;
const char *s1 = s + init;
int anchor = (*p == '^');
if (anchor) {
p++; lp--; /* skip anchor character */
@ -792,7 +808,7 @@ static int str_find_aux (lua_State *L, int find) {
}
} while (s1++ < ms.src_end && !anchor);
}
lua_pushnil(L); /* not found */
luaL_pushfail(L); /* not found */
return 1;
}
@ -836,11 +852,14 @@ static int gmatch (lua_State *L) {
size_t ls, lp;
const char *s = luaL_checklstring(L, 1, &ls);
const char *p = luaL_checklstring(L, 2, &lp);
size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
GMatchState *gm;
lua_settop(L, 2); /* keep them on closure to avoid being collected */
lua_settop(L, 2); /* keep strings on closure to avoid being collected */
gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState));
if (init > ls) /* start after string's end? */
init = ls + 1; /* avoid overflows in 's + init' */
prepstate(&gm->ms, L, s, ls, p, lp);
gm->src = s; gm->p = p; gm->lastmatch = NULL;
gm->src = s + init; gm->p = p; gm->lastmatch = NULL;
lua_pushcclosure(L, gmatch_aux, 3);
return 1;
}
@ -848,60 +867,72 @@ static int gmatch (lua_State *L) {
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
const char *e) {
size_t l, i;
size_t l;
lua_State *L = ms->L;
const char *news = lua_tolstring(L, 3, &l);
for (i = 0; i < l; i++) {
if (news[i] != L_ESC)
luaL_addchar(b, news[i]);
else {
i++; /* skip ESC */
if (!isdigit(uchar(news[i]))) {
if (news[i] != L_ESC)
luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
luaL_addchar(b, news[i]);
}
else if (news[i] == '0')
luaL_addlstring(b, s, e - s);
else {
push_onecapture(ms, news[i] - '1', s, e);
luaL_tolstring(L, -1, NULL); /* if number, convert it to string */
lua_remove(L, -2); /* remove original value */
luaL_addvalue(b); /* add capture to accumulated result */
}
const char *p;
while ((p = (char *)memchr(news, L_ESC, l)) != NULL) {
luaL_addlstring(b, news, p - news);
p++; /* skip ESC */
if (*p == L_ESC) /* '%%' */
luaL_addchar(b, *p);
else if (*p == '0') /* '%0' */
luaL_addlstring(b, s, e - s);
else if (isdigit(uchar(*p))) { /* '%n' */
const char *cap;
ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
if (resl == CAP_POSITION)
luaL_addvalue(b); /* add position to accumulated result */
else
luaL_addlstring(b, cap, resl);
}
else
luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
l -= p + 1 - news;
news = p + 1;
}
luaL_addlstring(b, news, l);
}
static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
const char *e, int tr) {
/*
** Add the replacement value to the string buffer 'b'.
** Return true if the original string was changed. (Function calls and
** table indexing resulting in nil or false do not change the subject.)
*/
static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
const char *e, int tr) {
lua_State *L = ms->L;
switch (tr) {
case LUA_TFUNCTION: {
case LUA_TFUNCTION: { /* call the function */
int n;
lua_pushvalue(L, 3);
n = push_captures(ms, s, e);
lua_call(L, n, 1);
lua_pushvalue(L, 3); /* push the function */
n = push_captures(ms, s, e); /* all captures as arguments */
lua_call(L, n, 1); /* call it */
break;
}
case LUA_TTABLE: {
push_onecapture(ms, 0, s, e);
case LUA_TTABLE: { /* index the table */
push_onecapture(ms, 0, s, e); /* first capture is the index */
lua_gettable(L, 3);
break;
}
default: { /* LUA_TNUMBER or LUA_TSTRING */
add_s(ms, b, s, e);
return;
add_s(ms, b, s, e); /* add value to the buffer */
return 1; /* something changed */
}
}
if (!lua_toboolean(L, -1)) { /* nil or false? */
lua_pop(L, 1);
lua_pushlstring(L, s, e - s); /* keep original text */
lua_pop(L, 1); /* remove value */
luaL_addlstring(b, s, e - s); /* keep original text */
return 0; /* no changes */
}
else if (!lua_isstring(L, -1))
luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
luaL_addvalue(b); /* add result to accumulator */
return luaL_error(L, "invalid replacement value (a %s)",
luaL_typename(L, -1));
else {
luaL_addvalue(b); /* add result to accumulator */
return 1; /* something changed */
}
}
@ -914,11 +945,12 @@ static int str_gsub (lua_State *L) {
lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
int anchor = (*p == '^');
lua_Integer n = 0; /* replacement count */
int changed = 0; /* change flag */
MatchState ms;
luaL_Buffer b;
luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
"string/function/table expected");
"string/function/table");
luaL_buffinit(L, &b);
if (anchor) {
p++; lp--; /* skip anchor character */
@ -929,7 +961,7 @@ static int str_gsub (lua_State *L) {
reprepstate(&ms); /* (re)prepare state for new match */
if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */
n++;
add_value(&ms, &b, src, e, tr); /* add replacement to buffer */
changed = add_value(&ms, &b, src, e, tr) | changed;
src = lastmatch = e;
}
else if (src < ms.src_end) /* otherwise, skip one character */
@ -937,8 +969,12 @@ static int str_gsub (lua_State *L) {
else break; /* end of subject */
if (anchor) break;
}
luaL_addlstring(&b, src, ms.src_end-src);
luaL_pushresult(&b);
if (!changed) /* no changes? */
lua_pushvalue(L, 1); /* return original string */
else { /* something changed */
luaL_addlstring(&b, src, ms.src_end-src);
luaL_pushresult(&b); /* create and return new string */
}
lua_pushinteger(L, n); /* number of substitutions */
return 2;
}
@ -959,8 +995,6 @@ static int str_gsub (lua_State *L) {
** Hexadecimal floating-point formatter
*/
#include <math.h>
#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
@ -1053,7 +1087,10 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
/* valid flags in a format specification */
#define FLAGS "-+ #0"
#if !defined(L_FMTFLAGS)
#define L_FMTFLAGS "-+ #0"
#endif
/*
** maximum size of each format specification (such as "%-099.99d")
@ -1085,14 +1122,32 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
/*
** Ensures the 'buff' string uses a dot as the radix character.
** Serialize a floating-point number in such a way that it can be
** scanned back by Lua. Use hexadecimal format for "common" numbers
** (to preserve precision); inf, -inf, and NaN are handled separately.
** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.)
*/
static void checkdp (char *buff, int nb) {
if (memchr(buff, '.', nb) == NULL) { /* no dot? */
char point = lua_getlocaledecpoint(); /* try locale point */
char *ppoint = (char *)memchr(buff, point, nb);
if (ppoint) *ppoint = '.'; /* change it to a dot */
static int quotefloat (lua_State *L, char *buff, lua_Number n) {
const char *s; /* for the fixed representations */
if (n == (lua_Number)HUGE_VAL) /* inf? */
s = "1e9999";
else if (n == -(lua_Number)HUGE_VAL) /* -inf? */
s = "-1e9999";
else if (n != n) /* NaN? */
s = "(0/0)";
else { /* format number as hexadecimal */
int nb = lua_number2strx(L, buff, MAX_ITEM,
"%" LUA_NUMBER_FRMLEN "a", n);
/* ensures that 'buff' string uses a dot as the radix character */
if (memchr(buff, '.', nb) == NULL) { /* no dot? */
char point = lua_getlocaledecpoint(); /* try locale point */
char *ppoint = (char *)memchr(buff, point, nb);
if (ppoint) *ppoint = '.'; /* change it to a dot */
}
return nb;
}
/* for the fixed representations */
return l_sprintf(buff, MAX_ITEM, "%s", s);
}
@ -1107,15 +1162,12 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
case LUA_TNUMBER: {
char *buff = luaL_prepbuffsize(b, MAX_ITEM);
int nb;
if (!lua_isinteger(L, arg)) { /* float? */
lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */
nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n);
checkdp(buff, nb); /* ensure it uses a dot */
}
if (!lua_isinteger(L, arg)) /* float? */
nb = quotefloat(L, buff, lua_tonumber(L, arg));
else { /* integers */
lua_Integer n = lua_tointeger(L, arg);
const char *format = (n == LUA_MININTEGER) /* corner case? */
? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */
? "0x%" LUA_INTEGER_FRMLEN "x" /* use hex */
: LUA_INTEGER_FMT; /* else use default format */
nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
}
@ -1136,8 +1188,8 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
const char *p = strfrmt;
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
while (*p != '\0' && strchr(L_FMTFLAGS, *p) != NULL) p++; /* skip flags */
if ((size_t)(p - strfrmt) >= sizeof(L_FMTFLAGS)/sizeof(char))
luaL_error(L, "invalid format (repeated flags)");
if (isdigit(uchar(*p))) p++; /* skip width */
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
@ -1592,17 +1644,12 @@ static int str_packsize (lua_State *L) {
while (*fmt != '\0') {
int size, ntoalign;
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
"variable-length format");
size += ntoalign; /* total space used by option */
luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
"format result too large");
totalsize += size;
switch (opt) {
case Kstring: /* strings with length count */
case Kzstr: /* zero-terminated string */
luaL_argerror(L, 1, "variable-length format");
/* call never return, but to avoid warnings: *//* FALLTHROUGH */
default: break;
}
}
lua_pushinteger(L, (lua_Integer)totalsize);
return 1;
@ -1648,15 +1695,15 @@ static int str_unpack (lua_State *L) {
const char *fmt = luaL_checkstring(L, 1);
size_t ld;
const char *data = luaL_checklstring(L, 2, &ld);
size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1;
int n = 0; /* number of results */
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
initheader(L, &h);
while (*fmt != '\0') {
int size, ntoalign;
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
luaL_argerror(L, 2, "data string too short");
luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2,
"data string too short");
pos += ntoalign; /* skip alignment */
/* stack space for item + next position */
luaL_checkstack(L, 2, "too many results");
@ -1685,13 +1732,15 @@ static int str_unpack (lua_State *L) {
}
case Kstring: {
size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
lua_pushlstring(L, data + pos + size, len);
pos += len; /* skip string */
break;
}
case Kzstr: {
size_t len = (int)strlen(data + pos);
luaL_argcheck(L, pos + len < ld, 2,
"unfinished string for format 'z'");
lua_pushlstring(L, data + pos, len);
pos += len + 1; /* skip string plus final '\0' */
break;

@ -1,8 +1,10 @@
-- $Id: bitwise.lua,v 1.26 2016/11/07 13:11:28 roberto Exp $
-- $Id: testes/bitwise.lua $
-- See Copyright Notice in file all.lua
print("testing bitwise operations")
require "bwcoercion"
local numbits = string.packsize('j') * 8
assert(~0 == -1)
@ -54,6 +56,22 @@ assert("0xffffffffffffffff" | 0 == -1)
assert("0xfffffffffffffffe" & "-1" == -2)
assert(" \t-0xfffffffffffffffe\n\t" & "-1" == 2)
assert(" \n -45 \t " >> " -2 " == -45 * 4)
assert("1234.0" << "5.0" == 1234 * 32)
assert("0xffff.0" ~ "0xAAAA" == 0x5555)
assert(~"0x0.000p4" == -1)
assert(("7" .. 3) << 1 == 146)
assert(0xffffffff >> (1 .. "9") == 0x1fff)
assert(10 | (1 .. "9") == 27)
do
local st, msg = pcall(function () return 4 & "a" end)
assert(string.find(msg, "'band'"))
local st, msg = pcall(function () return ~"a" end)
assert(string.find(msg, "'bnot'"))
end
-- out of range number
assert(not pcall(function () return "0xffffffffffffffff.0" | 0 end))

@ -0,0 +1,78 @@
local tonumber, tointeger = tonumber, math.tointeger
local type, getmetatable, rawget, error = type, getmetatable, rawget, error
local strsub = string.sub
local print = print
_ENV = nil
-- Try to convert a value to an integer, without assuming any coercion.
local function toint (x)
x = tonumber(x) -- handle numerical strings
if not x then
return false -- not coercible to a number
end
return tointeger(x)
end
-- If operation fails, maybe second operand has a metamethod that should
-- have been called if not for this string metamethod, so try to
-- call it.
local function trymt (x, y, mtname)
if type(y) ~= "string" then -- avoid recalling original metamethod
local mt = getmetatable(y)
local mm = mt and rawget(mt, mtname)
if mm then
return mm(x, y)
end
end
-- if any test fails, there is no other metamethod to be called
error("attempt to '" .. strsub(mtname, 3) ..
"' a " .. type(x) .. " with a " .. type(y), 4)
end
local function checkargs (x, y, mtname)
local xi = toint(x)
local yi = toint(y)
if xi and yi then
return xi, yi
else
return trymt(x, y, mtname), nil
end
end
local smt = getmetatable("")
smt.__band = function (x, y)
local x, y = checkargs(x, y, "__band")
return y and x & y or x
end
smt.__bor = function (x, y)
local x, y = checkargs(x, y, "__bor")
return y and x | y or x
end
smt.__bxor = function (x, y)
local x, y = checkargs(x, y, "__bxor")
return y and x ~ y or x
end
smt.__shl = function (x, y)
local x, y = checkargs(x, y, "__shl")
return y and x << y or x
end
smt.__shr = function (x, y)
local x, y = checkargs(x, y, "__shr")
return y and x >> y or x
end
smt.__bnot = function (x)
local x, y = checkargs(x, x, "__bnot")
return y and ~x or x
end

@ -32,10 +32,6 @@ assert(-3%5 == 2 and -3+5 == 2)
assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33");
assert(not(2+1 > 3*1) and "a".."b" > "a");
assert("7" .. 3 << 1 == 146)
assert(10 >> 1 .. "9" == 0)
assert(10 | 1 .. "9" == 27)
assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4)
assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4)
assert(0xF0 & 0x0F + 1 == 0x10)

@ -204,6 +204,8 @@ do
checkerror("contains zeros", pack, "z", "alo\0");
checkerror("unfinished string", unpack, "zc10000000", "alo")
for i = 2, NB do
local s1 = pack("s" .. i, s)
assert(unpack("s" .. i, s1) == s and #s1 == #s + i)
@ -312,9 +314,7 @@ do -- testing initial position
for i = 1, #x + 1 do
assert(unpack("c0", x, i) == "")
end
checkerror("out of string", unpack, "c0", x, 0)
checkerror("out of string", unpack, "c0", x, #x + 2)
checkerror("out of string", unpack, "c0", x, -(#x + 1))
end

Loading…
Cancel
Save