lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 5c519a69d3e25f5c210a858f0abf721cd7a2fdab
parent 6f54b07663c48c6f306e93844598b509c2db444f
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Wed, 10 Dec 2014 09:35:38 -0200

new function 'string.packsize'

Diffstat:
Mlstrlib.c | 25++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lstrlib.c b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.217 2014/11/11 19:40:20 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.218 2014/12/04 16:25:40 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -1258,6 +1258,28 @@ static int str_pack (lua_State *L) { } +static int str_packsize (lua_State *L) { + Header h; + const char *fmt = luaL_checkstring(L, 1); /* format string */ + lua_Integer totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); + while (*fmt != '\0') { + int size, ntoalign; + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + totalsize += ntoalign + size; + switch (opt) { + case Kstring: /* strings with length count */ + case Kzstr: /* zero-terminated string */ + luaL_argerror(L, 1, "variable-length format"); + break; + default: break; + } + } + lua_pushinteger(L, totalsize); + return 1; +} + + /* ** Unpack an integer with 'size' bytes and 'islittle' endianness. ** If size is smaller than the size of a Lua integer and integer @@ -1374,6 +1396,7 @@ static const luaL_Reg strlib[] = { {"sub", str_sub}, {"upper", str_upper}, {"pack", str_pack}, + {"packsize", str_packsize}, {"unpack", str_unpack}, {NULL, NULL} };