aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-10-28 15:56:51 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-10-28 15:56:51 -0200
commitcc3a42b1909c2c2cac2086fa10fea8d212d174ad (patch)
tree379a4e7d11a129ac25b48ce814508236dd42ba71 /lstrlib.c
parent6707ce63491ad5f233f30b1d561ed7f0f0b9a0c8 (diff)
downloadlua-cc3a42b1909c2c2cac2086fa10fea8d212d174ad.tar.gz
lua-cc3a42b1909c2c2cac2086fa10fea8d212d174ad.tar.bz2
lua-cc3a42b1909c2c2cac2086fa10fea8d212d174ad.zip
option 'c' in 'string.pack' accepts any string size (truncating
if larger and padding if smaller)
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 9708e36e..bc3da951 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.234 2015/09/28 18:05:01 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.235 2015/10/08 15:53:05 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*/
@@ -1339,8 +1339,13 @@ static int str_pack (lua_State *L) {
1339 case Kchar: { /* fixed-size string */ 1339 case Kchar: { /* fixed-size string */
1340 size_t len; 1340 size_t len;
1341 const char *s = luaL_checklstring(L, arg, &len); 1341 const char *s = luaL_checklstring(L, arg, &len);
1342 luaL_argcheck(L, len == (size_t)size, arg, "wrong length"); 1342 if (size <= len) /* string larger than (or equal to) needed? */
1343 luaL_addlstring(&b, s, size); 1343 luaL_addlstring(&b, s, size); /* truncate string to asked size */
1344 else { /* string smaller than needed */
1345 luaL_addlstring(&b, s, len); /* add it all */
1346 while (len++ < size) /* pad extra space */
1347 luaL_addchar(&b, LUA_PACKPADBYTE);
1348 }
1344 break; 1349 break;
1345 } 1350 }
1346 case Kstring: { /* strings with length count */ 1351 case Kstring: { /* strings with length count */