diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-11-04 12:34:43 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-11-04 12:34:43 -0200 |
commit | cfabcbfb1785a64352b384e16e0477e2b47cee22 (patch) | |
tree | c8a9bfcee373a8a73d0c4153e42da7ce4508efb3 /lstrlib.c | |
parent | bde14c3adc055d2a16cfe718f1b698801dcd4568 (diff) | |
download | lua-cfabcbfb1785a64352b384e16e0477e2b47cee22.tar.gz lua-cfabcbfb1785a64352b384e16e0477e2b47cee22.tar.bz2 lua-cfabcbfb1785a64352b384e16e0477e2b47cee22.zip |
added macro for configuring padding value in 'string.pack'
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 18 |
1 files changed, 12 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.211 2014/10/31 15:53:31 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.212 2014/11/02 19:19:04 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -949,6 +949,11 @@ static int str_format (lua_State *L) { | |||
949 | */ | 949 | */ |
950 | 950 | ||
951 | 951 | ||
952 | /* value used for padding */ | ||
953 | #if !defined(LUA_PACKPADBYTE) | ||
954 | #define LUA_PACKPADBYTE 0x00 | ||
955 | #endif | ||
956 | |||
952 | /* maximum size for the binary representation of an integer */ | 957 | /* maximum size for the binary representation of an integer */ |
953 | #define MAXINTSIZE 16 | 958 | #define MAXINTSIZE 16 |
954 | 959 | ||
@@ -1172,7 +1177,8 @@ static int str_pack (lua_State *L) { | |||
1172 | int size, ntoalign; | 1177 | int size, ntoalign; |
1173 | KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); | 1178 | KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); |
1174 | totalsize += ntoalign + size; | 1179 | totalsize += ntoalign + size; |
1175 | while (ntoalign-- > 0) luaL_addchar(&b, '\0'); /* fill alignment */ | 1180 | while (ntoalign-- > 0) |
1181 | luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ | ||
1176 | arg++; | 1182 | arg++; |
1177 | switch (opt) { | 1183 | switch (opt) { |
1178 | case Kint: { /* signed integers */ | 1184 | case Kint: { /* signed integers */ |
@@ -1232,7 +1238,7 @@ static int str_pack (lua_State *L) { | |||
1232 | totalsize += len + 1; | 1238 | totalsize += len + 1; |
1233 | break; | 1239 | break; |
1234 | } | 1240 | } |
1235 | case Kpadding: luaL_addchar(&b, '\0'); /* go through */ | 1241 | case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* go through */ |
1236 | case Kpaddalign: case Knop: | 1242 | case Kpaddalign: case Knop: |
1237 | arg--; /* undo increment */ | 1243 | arg--; /* undo increment */ |
1238 | break; | 1244 | break; |
@@ -1283,7 +1289,7 @@ static int str_unpack (lua_State *L) { | |||
1283 | KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); | 1289 | KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); |
1284 | if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld) | 1290 | if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld) |
1285 | luaL_argerror(L, 2, "data string too short"); | 1291 | luaL_argerror(L, 2, "data string too short"); |
1286 | pos += ntoalign; | 1292 | pos += ntoalign; /* skip alignment */ |
1287 | /* stack space for item + next position */ | 1293 | /* stack space for item + next position */ |
1288 | luaL_checkstack(L, 2, "too many results"); | 1294 | luaL_checkstack(L, 2, "too many results"); |
1289 | n++; | 1295 | n++; |
@@ -1313,13 +1319,13 @@ static int str_unpack (lua_State *L) { | |||
1313 | size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); | 1319 | size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); |
1314 | luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short"); | 1320 | luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short"); |
1315 | lua_pushlstring(L, data + pos + size, len); | 1321 | lua_pushlstring(L, data + pos + size, len); |
1316 | pos += len; | 1322 | pos += len; /* skip string */ |
1317 | break; | 1323 | break; |
1318 | } | 1324 | } |
1319 | case Kzstr: { | 1325 | case Kzstr: { |
1320 | size_t len = (int)strlen(data + pos); | 1326 | size_t len = (int)strlen(data + pos); |
1321 | lua_pushlstring(L, data + pos, len); | 1327 | lua_pushlstring(L, data + pos, len); |
1322 | pos += len + 1; /* skip final '\0' */ | 1328 | pos += len + 1; /* skip string plus final '\0' */ |
1323 | break; | 1329 | break; |
1324 | } | 1330 | } |
1325 | case Kpaddalign: case Kpadding: case Knop: | 1331 | case Kpaddalign: case Kpadding: case Knop: |