mirror of
https://github.com/lua/lua.git
synced 2025-01-14 05:43:00 +08:00
added macro for configuring padding value in 'string.pack'
This commit is contained in:
parent
bde14c3adc
commit
cfabcbfb17
18
lstrlib.c
18
lstrlib.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstrlib.c,v 1.211 2014/10/31 15:53:31 roberto Exp roberto $
|
** $Id: lstrlib.c,v 1.212 2014/11/02 19:19:04 roberto Exp roberto $
|
||||||
** Standard library for string operations and pattern-matching
|
** Standard library for string operations and pattern-matching
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -949,6 +949,11 @@ static int str_format (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/* value used for padding */
|
||||||
|
#if !defined(LUA_PACKPADBYTE)
|
||||||
|
#define LUA_PACKPADBYTE 0x00
|
||||||
|
#endif
|
||||||
|
|
||||||
/* maximum size for the binary representation of an integer */
|
/* maximum size for the binary representation of an integer */
|
||||||
#define MAXINTSIZE 16
|
#define MAXINTSIZE 16
|
||||||
|
|
||||||
@ -1172,7 +1177,8 @@ static int str_pack (lua_State *L) {
|
|||||||
int size, ntoalign;
|
int size, ntoalign;
|
||||||
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||||
totalsize += ntoalign + size;
|
totalsize += ntoalign + size;
|
||||||
while (ntoalign-- > 0) luaL_addchar(&b, '\0'); /* fill alignment */
|
while (ntoalign-- > 0)
|
||||||
|
luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */
|
||||||
arg++;
|
arg++;
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case Kint: { /* signed integers */
|
case Kint: { /* signed integers */
|
||||||
@ -1232,7 +1238,7 @@ static int str_pack (lua_State *L) {
|
|||||||
totalsize += len + 1;
|
totalsize += len + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kpadding: luaL_addchar(&b, '\0'); /* go through */
|
case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* go through */
|
||||||
case Kpaddalign: case Knop:
|
case Kpaddalign: case Knop:
|
||||||
arg--; /* undo increment */
|
arg--; /* undo increment */
|
||||||
break;
|
break;
|
||||||
@ -1283,7 +1289,7 @@ static int str_unpack (lua_State *L) {
|
|||||||
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
||||||
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
|
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
|
||||||
luaL_argerror(L, 2, "data string too short");
|
luaL_argerror(L, 2, "data string too short");
|
||||||
pos += ntoalign;
|
pos += ntoalign; /* skip alignment */
|
||||||
/* stack space for item + next position */
|
/* stack space for item + next position */
|
||||||
luaL_checkstack(L, 2, "too many results");
|
luaL_checkstack(L, 2, "too many results");
|
||||||
n++;
|
n++;
|
||||||
@ -1313,13 +1319,13 @@ static int str_unpack (lua_State *L) {
|
|||||||
size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
|
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, pos + len + size <= ld, 2, "data string too short");
|
||||||
lua_pushlstring(L, data + pos + size, len);
|
lua_pushlstring(L, data + pos + size, len);
|
||||||
pos += len;
|
pos += len; /* skip string */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kzstr: {
|
case Kzstr: {
|
||||||
size_t len = (int)strlen(data + pos);
|
size_t len = (int)strlen(data + pos);
|
||||||
lua_pushlstring(L, data + pos, len);
|
lua_pushlstring(L, data + pos, len);
|
||||||
pos += len + 1; /* skip final '\0' */
|
pos += len + 1; /* skip string plus final '\0' */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kpaddalign: case Kpadding: case Knop:
|
case Kpaddalign: case Kpadding: case Knop:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user