xref: /freebsd/contrib/lua/src/lstrlib.c (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: lstrlib.c $
38e3e3a7aSWarner Losh ** Standard library for string operations and pattern-matching
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #define lstrlib_c
88e3e3a7aSWarner Losh #define LUA_LIB
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh #include "lprefix.h"
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh 
138e3e3a7aSWarner Losh #include <ctype.h>
148e3e3a7aSWarner Losh #include <float.h>
158e3e3a7aSWarner Losh #include <limits.h>
168e3e3a7aSWarner Losh #include <locale.h>
170495ed39SKyle Evans #include <math.h>
188e3e3a7aSWarner Losh #include <stddef.h>
198e3e3a7aSWarner Losh #include <stdio.h>
208e3e3a7aSWarner Losh #include <stdlib.h>
218e3e3a7aSWarner Losh #include <string.h>
228e3e3a7aSWarner Losh 
238e3e3a7aSWarner Losh #include "lua.h"
248e3e3a7aSWarner Losh 
258e3e3a7aSWarner Losh #include "lauxlib.h"
268e3e3a7aSWarner Losh #include "lualib.h"
278e3e3a7aSWarner Losh 
288e3e3a7aSWarner Losh 
298e3e3a7aSWarner Losh /*
308e3e3a7aSWarner Losh ** maximum number of captures that a pattern can do during
318e3e3a7aSWarner Losh ** pattern-matching. This limit is arbitrary, but must fit in
328e3e3a7aSWarner Losh ** an unsigned char.
338e3e3a7aSWarner Losh */
348e3e3a7aSWarner Losh #if !defined(LUA_MAXCAPTURES)
358e3e3a7aSWarner Losh #define LUA_MAXCAPTURES		32
368e3e3a7aSWarner Losh #endif
378e3e3a7aSWarner Losh 
388e3e3a7aSWarner Losh 
398e3e3a7aSWarner Losh /* macro to 'unsign' a character */
408e3e3a7aSWarner Losh #define uchar(c)	((unsigned char)(c))
418e3e3a7aSWarner Losh 
428e3e3a7aSWarner Losh 
438e3e3a7aSWarner Losh /*
448e3e3a7aSWarner Losh ** Some sizes are better limited to fit in 'int', but must also fit in
458e3e3a7aSWarner Losh ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
468e3e3a7aSWarner Losh */
478e3e3a7aSWarner Losh #define MAX_SIZET	((size_t)(~(size_t)0))
488e3e3a7aSWarner Losh 
498e3e3a7aSWarner Losh #define MAXSIZE  \
508e3e3a7aSWarner Losh 	(sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
518e3e3a7aSWarner Losh 
528e3e3a7aSWarner Losh 
538e3e3a7aSWarner Losh 
548e3e3a7aSWarner Losh 
str_len(lua_State * L)558e3e3a7aSWarner Losh static int str_len (lua_State *L) {
568e3e3a7aSWarner Losh   size_t l;
578e3e3a7aSWarner Losh   luaL_checklstring(L, 1, &l);
588e3e3a7aSWarner Losh   lua_pushinteger(L, (lua_Integer)l);
598e3e3a7aSWarner Losh   return 1;
608e3e3a7aSWarner Losh }
618e3e3a7aSWarner Losh 
628e3e3a7aSWarner Losh 
630495ed39SKyle Evans /*
640495ed39SKyle Evans ** translate a relative initial string position
650495ed39SKyle Evans ** (negative means back from end): clip result to [1, inf).
660495ed39SKyle Evans ** The length of any string in Lua must fit in a lua_Integer,
670495ed39SKyle Evans ** so there are no overflows in the casts.
680495ed39SKyle Evans ** The inverted comparison avoids a possible overflow
690495ed39SKyle Evans ** computing '-pos'.
700495ed39SKyle Evans */
posrelatI(lua_Integer pos,size_t len)710495ed39SKyle Evans static size_t posrelatI (lua_Integer pos, size_t len) {
720495ed39SKyle Evans   if (pos > 0)
730495ed39SKyle Evans     return (size_t)pos;
740495ed39SKyle Evans   else if (pos == 0)
750495ed39SKyle Evans     return 1;
760495ed39SKyle Evans   else if (pos < -(lua_Integer)len)  /* inverted comparison */
770495ed39SKyle Evans     return 1;  /* clip to 1 */
780495ed39SKyle Evans   else return len + (size_t)pos + 1;
790495ed39SKyle Evans }
800495ed39SKyle Evans 
810495ed39SKyle Evans 
820495ed39SKyle Evans /*
830495ed39SKyle Evans ** Gets an optional ending string position from argument 'arg',
840495ed39SKyle Evans ** with default value 'def'.
850495ed39SKyle Evans ** Negative means back from end: clip result to [0, len]
860495ed39SKyle Evans */
getendpos(lua_State * L,int arg,lua_Integer def,size_t len)870495ed39SKyle Evans static size_t getendpos (lua_State *L, int arg, lua_Integer def,
880495ed39SKyle Evans                          size_t len) {
890495ed39SKyle Evans   lua_Integer pos = luaL_optinteger(L, arg, def);
900495ed39SKyle Evans   if (pos > (lua_Integer)len)
910495ed39SKyle Evans     return len;
920495ed39SKyle Evans   else if (pos >= 0)
930495ed39SKyle Evans     return (size_t)pos;
940495ed39SKyle Evans   else if (pos < -(lua_Integer)len)
950495ed39SKyle Evans     return 0;
960495ed39SKyle Evans   else return len + (size_t)pos + 1;
978e3e3a7aSWarner Losh }
988e3e3a7aSWarner Losh 
998e3e3a7aSWarner Losh 
str_sub(lua_State * L)1008e3e3a7aSWarner Losh static int str_sub (lua_State *L) {
1018e3e3a7aSWarner Losh   size_t l;
1028e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &l);
1030495ed39SKyle Evans   size_t start = posrelatI(luaL_checkinteger(L, 2), l);
1040495ed39SKyle Evans   size_t end = getendpos(L, 3, -1, l);
1058e3e3a7aSWarner Losh   if (start <= end)
1060495ed39SKyle Evans     lua_pushlstring(L, s + start - 1, (end - start) + 1);
1078e3e3a7aSWarner Losh   else lua_pushliteral(L, "");
1088e3e3a7aSWarner Losh   return 1;
1098e3e3a7aSWarner Losh }
1108e3e3a7aSWarner Losh 
1118e3e3a7aSWarner Losh 
str_reverse(lua_State * L)1128e3e3a7aSWarner Losh static int str_reverse (lua_State *L) {
1138e3e3a7aSWarner Losh   size_t l, i;
1148e3e3a7aSWarner Losh   luaL_Buffer b;
1158e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &l);
1168e3e3a7aSWarner Losh   char *p = luaL_buffinitsize(L, &b, l);
1178e3e3a7aSWarner Losh   for (i = 0; i < l; i++)
1188e3e3a7aSWarner Losh     p[i] = s[l - i - 1];
1198e3e3a7aSWarner Losh   luaL_pushresultsize(&b, l);
1208e3e3a7aSWarner Losh   return 1;
1218e3e3a7aSWarner Losh }
1228e3e3a7aSWarner Losh 
1238e3e3a7aSWarner Losh 
str_lower(lua_State * L)1248e3e3a7aSWarner Losh static int str_lower (lua_State *L) {
1258e3e3a7aSWarner Losh   size_t l;
1268e3e3a7aSWarner Losh   size_t i;
1278e3e3a7aSWarner Losh   luaL_Buffer b;
1288e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &l);
1298e3e3a7aSWarner Losh   char *p = luaL_buffinitsize(L, &b, l);
1308e3e3a7aSWarner Losh   for (i=0; i<l; i++)
1318e3e3a7aSWarner Losh     p[i] = tolower(uchar(s[i]));
1328e3e3a7aSWarner Losh   luaL_pushresultsize(&b, l);
1338e3e3a7aSWarner Losh   return 1;
1348e3e3a7aSWarner Losh }
1358e3e3a7aSWarner Losh 
1368e3e3a7aSWarner Losh 
str_upper(lua_State * L)1378e3e3a7aSWarner Losh static int str_upper (lua_State *L) {
1388e3e3a7aSWarner Losh   size_t l;
1398e3e3a7aSWarner Losh   size_t i;
1408e3e3a7aSWarner Losh   luaL_Buffer b;
1418e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &l);
1428e3e3a7aSWarner Losh   char *p = luaL_buffinitsize(L, &b, l);
1438e3e3a7aSWarner Losh   for (i=0; i<l; i++)
1448e3e3a7aSWarner Losh     p[i] = toupper(uchar(s[i]));
1458e3e3a7aSWarner Losh   luaL_pushresultsize(&b, l);
1468e3e3a7aSWarner Losh   return 1;
1478e3e3a7aSWarner Losh }
1488e3e3a7aSWarner Losh 
1498e3e3a7aSWarner Losh 
str_rep(lua_State * L)1508e3e3a7aSWarner Losh static int str_rep (lua_State *L) {
1518e3e3a7aSWarner Losh   size_t l, lsep;
1528e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &l);
1538e3e3a7aSWarner Losh   lua_Integer n = luaL_checkinteger(L, 2);
1548e3e3a7aSWarner Losh   const char *sep = luaL_optlstring(L, 3, "", &lsep);
1558c784bb8SWarner Losh   if (n <= 0)
1568c784bb8SWarner Losh     lua_pushliteral(L, "");
1578c784bb8SWarner Losh   else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n))
1588e3e3a7aSWarner Losh     return luaL_error(L, "resulting string too large");
1598e3e3a7aSWarner Losh   else {
1608e3e3a7aSWarner Losh     size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
1618e3e3a7aSWarner Losh     luaL_Buffer b;
1628e3e3a7aSWarner Losh     char *p = luaL_buffinitsize(L, &b, totallen);
1638e3e3a7aSWarner Losh     while (n-- > 1) {  /* first n-1 copies (followed by separator) */
1648e3e3a7aSWarner Losh       memcpy(p, s, l * sizeof(char)); p += l;
1658e3e3a7aSWarner Losh       if (lsep > 0) {  /* empty 'memcpy' is not that cheap */
1668e3e3a7aSWarner Losh         memcpy(p, sep, lsep * sizeof(char));
1678e3e3a7aSWarner Losh         p += lsep;
1688e3e3a7aSWarner Losh       }
1698e3e3a7aSWarner Losh     }
1708e3e3a7aSWarner Losh     memcpy(p, s, l * sizeof(char));  /* last copy (not followed by separator) */
1718e3e3a7aSWarner Losh     luaL_pushresultsize(&b, totallen);
1728e3e3a7aSWarner Losh   }
1738e3e3a7aSWarner Losh   return 1;
1748e3e3a7aSWarner Losh }
1758e3e3a7aSWarner Losh 
1768e3e3a7aSWarner Losh 
str_byte(lua_State * L)1778e3e3a7aSWarner Losh static int str_byte (lua_State *L) {
1788e3e3a7aSWarner Losh   size_t l;
1798e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &l);
1800495ed39SKyle Evans   lua_Integer pi = luaL_optinteger(L, 2, 1);
1810495ed39SKyle Evans   size_t posi = posrelatI(pi, l);
1820495ed39SKyle Evans   size_t pose = getendpos(L, 3, pi, l);
1838e3e3a7aSWarner Losh   int n, i;
1848e3e3a7aSWarner Losh   if (posi > pose) return 0;  /* empty interval; return no values */
1858c784bb8SWarner Losh   if (l_unlikely(pose - posi >= (size_t)INT_MAX))  /* arithmetic overflow? */
1868e3e3a7aSWarner Losh     return luaL_error(L, "string slice too long");
1878e3e3a7aSWarner Losh   n = (int)(pose -  posi) + 1;
1888e3e3a7aSWarner Losh   luaL_checkstack(L, n, "string slice too long");
1898e3e3a7aSWarner Losh   for (i=0; i<n; i++)
1908e3e3a7aSWarner Losh     lua_pushinteger(L, uchar(s[posi+i-1]));
1918e3e3a7aSWarner Losh   return n;
1928e3e3a7aSWarner Losh }
1938e3e3a7aSWarner Losh 
1948e3e3a7aSWarner Losh 
str_char(lua_State * L)1958e3e3a7aSWarner Losh static int str_char (lua_State *L) {
1968e3e3a7aSWarner Losh   int n = lua_gettop(L);  /* number of arguments */
1978e3e3a7aSWarner Losh   int i;
1988e3e3a7aSWarner Losh   luaL_Buffer b;
1998e3e3a7aSWarner Losh   char *p = luaL_buffinitsize(L, &b, n);
2008e3e3a7aSWarner Losh   for (i=1; i<=n; i++) {
2010495ed39SKyle Evans     lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
2020495ed39SKyle Evans     luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
2038e3e3a7aSWarner Losh     p[i - 1] = uchar(c);
2048e3e3a7aSWarner Losh   }
2058e3e3a7aSWarner Losh   luaL_pushresultsize(&b, n);
2068e3e3a7aSWarner Losh   return 1;
2078e3e3a7aSWarner Losh }
2088e3e3a7aSWarner Losh 
2098e3e3a7aSWarner Losh 
2100495ed39SKyle Evans /*
2110495ed39SKyle Evans ** Buffer to store the result of 'string.dump'. It must be initialized
2120495ed39SKyle Evans ** after the call to 'lua_dump', to ensure that the function is on the
2130495ed39SKyle Evans ** top of the stack when 'lua_dump' is called. ('luaL_buffinit' might
2140495ed39SKyle Evans ** push stuff.)
2150495ed39SKyle Evans */
2160495ed39SKyle Evans struct str_Writer {
2170495ed39SKyle Evans   int init;  /* true iff buffer has been initialized */
2180495ed39SKyle Evans   luaL_Buffer B;
2190495ed39SKyle Evans };
2200495ed39SKyle Evans 
2210495ed39SKyle Evans 
writer(lua_State * L,const void * b,size_t size,void * ud)2220495ed39SKyle Evans static int writer (lua_State *L, const void *b, size_t size, void *ud) {
2230495ed39SKyle Evans   struct str_Writer *state = (struct str_Writer *)ud;
2240495ed39SKyle Evans   if (!state->init) {
2250495ed39SKyle Evans     state->init = 1;
2260495ed39SKyle Evans     luaL_buffinit(L, &state->B);
2270495ed39SKyle Evans   }
2280495ed39SKyle Evans   luaL_addlstring(&state->B, (const char *)b, size);
2298e3e3a7aSWarner Losh   return 0;
2308e3e3a7aSWarner Losh }
2318e3e3a7aSWarner Losh 
2328e3e3a7aSWarner Losh 
str_dump(lua_State * L)2338e3e3a7aSWarner Losh static int str_dump (lua_State *L) {
2340495ed39SKyle Evans   struct str_Writer state;
2358e3e3a7aSWarner Losh   int strip = lua_toboolean(L, 2);
2368e3e3a7aSWarner Losh   luaL_checktype(L, 1, LUA_TFUNCTION);
2370495ed39SKyle Evans   lua_settop(L, 1);  /* ensure function is on the top of the stack */
2380495ed39SKyle Evans   state.init = 0;
2398c784bb8SWarner Losh   if (l_unlikely(lua_dump(L, writer, &state, strip) != 0))
2408e3e3a7aSWarner Losh     return luaL_error(L, "unable to dump given function");
2410495ed39SKyle Evans   luaL_pushresult(&state.B);
2428e3e3a7aSWarner Losh   return 1;
2438e3e3a7aSWarner Losh }
2448e3e3a7aSWarner Losh 
2458e3e3a7aSWarner Losh 
2468e3e3a7aSWarner Losh 
2478e3e3a7aSWarner Losh /*
2488e3e3a7aSWarner Losh ** {======================================================
2490495ed39SKyle Evans ** METAMETHODS
2500495ed39SKyle Evans ** =======================================================
2510495ed39SKyle Evans */
2520495ed39SKyle Evans 
2530495ed39SKyle Evans #if defined(LUA_NOCVTS2N)	/* { */
2540495ed39SKyle Evans 
2550495ed39SKyle Evans /* no coercion from strings to numbers */
2560495ed39SKyle Evans 
2570495ed39SKyle Evans static const luaL_Reg stringmetamethods[] = {
2580495ed39SKyle Evans   {"__index", NULL},  /* placeholder */
2590495ed39SKyle Evans   {NULL, NULL}
2600495ed39SKyle Evans };
2610495ed39SKyle Evans 
2620495ed39SKyle Evans #else		/* }{ */
2630495ed39SKyle Evans 
tonum(lua_State * L,int arg)2640495ed39SKyle Evans static int tonum (lua_State *L, int arg) {
2650495ed39SKyle Evans   if (lua_type(L, arg) == LUA_TNUMBER) {  /* already a number? */
2660495ed39SKyle Evans     lua_pushvalue(L, arg);
2670495ed39SKyle Evans     return 1;
2680495ed39SKyle Evans   }
2690495ed39SKyle Evans   else {  /* check whether it is a numerical string */
2700495ed39SKyle Evans     size_t len;
2710495ed39SKyle Evans     const char *s = lua_tolstring(L, arg, &len);
2720495ed39SKyle Evans     return (s != NULL && lua_stringtonumber(L, s) == len + 1);
2730495ed39SKyle Evans   }
2740495ed39SKyle Evans }
2750495ed39SKyle Evans 
2760495ed39SKyle Evans 
trymt(lua_State * L,const char * mtname)2770495ed39SKyle Evans static void trymt (lua_State *L, const char *mtname) {
2780495ed39SKyle Evans   lua_settop(L, 2);  /* back to the original arguments */
2798c784bb8SWarner Losh   if (l_unlikely(lua_type(L, 2) == LUA_TSTRING ||
2808c784bb8SWarner Losh                  !luaL_getmetafield(L, 2, mtname)))
2810495ed39SKyle Evans     luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2,
2820495ed39SKyle Evans                   luaL_typename(L, -2), luaL_typename(L, -1));
2830495ed39SKyle Evans   lua_insert(L, -3);  /* put metamethod before arguments */
2840495ed39SKyle Evans   lua_call(L, 2, 1);  /* call metamethod */
2850495ed39SKyle Evans }
2860495ed39SKyle Evans 
2870495ed39SKyle Evans 
arith(lua_State * L,int op,const char * mtname)2880495ed39SKyle Evans static int arith (lua_State *L, int op, const char *mtname) {
2890495ed39SKyle Evans   if (tonum(L, 1) && tonum(L, 2))
2900495ed39SKyle Evans     lua_arith(L, op);  /* result will be on the top */
2910495ed39SKyle Evans   else
2920495ed39SKyle Evans     trymt(L, mtname);
2930495ed39SKyle Evans   return 1;
2940495ed39SKyle Evans }
2950495ed39SKyle Evans 
2960495ed39SKyle Evans 
arith_add(lua_State * L)2970495ed39SKyle Evans static int arith_add (lua_State *L) {
2980495ed39SKyle Evans   return arith(L, LUA_OPADD, "__add");
2990495ed39SKyle Evans }
3000495ed39SKyle Evans 
arith_sub(lua_State * L)3010495ed39SKyle Evans static int arith_sub (lua_State *L) {
3020495ed39SKyle Evans   return arith(L, LUA_OPSUB, "__sub");
3030495ed39SKyle Evans }
3040495ed39SKyle Evans 
arith_mul(lua_State * L)3050495ed39SKyle Evans static int arith_mul (lua_State *L) {
3060495ed39SKyle Evans   return arith(L, LUA_OPMUL, "__mul");
3070495ed39SKyle Evans }
3080495ed39SKyle Evans 
arith_mod(lua_State * L)3090495ed39SKyle Evans static int arith_mod (lua_State *L) {
3100495ed39SKyle Evans   return arith(L, LUA_OPMOD, "__mod");
3110495ed39SKyle Evans }
3120495ed39SKyle Evans 
arith_pow(lua_State * L)3130495ed39SKyle Evans static int arith_pow (lua_State *L) {
3140495ed39SKyle Evans   return arith(L, LUA_OPPOW, "__pow");
3150495ed39SKyle Evans }
3160495ed39SKyle Evans 
arith_div(lua_State * L)3170495ed39SKyle Evans static int arith_div (lua_State *L) {
3180495ed39SKyle Evans   return arith(L, LUA_OPDIV, "__div");
3190495ed39SKyle Evans }
3200495ed39SKyle Evans 
arith_idiv(lua_State * L)3210495ed39SKyle Evans static int arith_idiv (lua_State *L) {
3220495ed39SKyle Evans   return arith(L, LUA_OPIDIV, "__idiv");
3230495ed39SKyle Evans }
3240495ed39SKyle Evans 
arith_unm(lua_State * L)3250495ed39SKyle Evans static int arith_unm (lua_State *L) {
3260495ed39SKyle Evans   return arith(L, LUA_OPUNM, "__unm");
3270495ed39SKyle Evans }
3280495ed39SKyle Evans 
3290495ed39SKyle Evans 
3300495ed39SKyle Evans static const luaL_Reg stringmetamethods[] = {
3310495ed39SKyle Evans   {"__add", arith_add},
3320495ed39SKyle Evans   {"__sub", arith_sub},
3330495ed39SKyle Evans   {"__mul", arith_mul},
3340495ed39SKyle Evans   {"__mod", arith_mod},
3350495ed39SKyle Evans   {"__pow", arith_pow},
3360495ed39SKyle Evans   {"__div", arith_div},
3370495ed39SKyle Evans   {"__idiv", arith_idiv},
3380495ed39SKyle Evans   {"__unm", arith_unm},
3390495ed39SKyle Evans   {"__index", NULL},  /* placeholder */
3400495ed39SKyle Evans   {NULL, NULL}
3410495ed39SKyle Evans };
3420495ed39SKyle Evans 
3430495ed39SKyle Evans #endif		/* } */
3440495ed39SKyle Evans 
3450495ed39SKyle Evans /* }====================================================== */
3460495ed39SKyle Evans 
3470495ed39SKyle Evans /*
3480495ed39SKyle Evans ** {======================================================
3498e3e3a7aSWarner Losh ** PATTERN MATCHING
3508e3e3a7aSWarner Losh ** =======================================================
3518e3e3a7aSWarner Losh */
3528e3e3a7aSWarner Losh 
3538e3e3a7aSWarner Losh 
3548e3e3a7aSWarner Losh #define CAP_UNFINISHED	(-1)
3558e3e3a7aSWarner Losh #define CAP_POSITION	(-2)
3568e3e3a7aSWarner Losh 
3578e3e3a7aSWarner Losh 
3588e3e3a7aSWarner Losh typedef struct MatchState {
3598e3e3a7aSWarner Losh   const char *src_init;  /* init of source string */
3608e3e3a7aSWarner Losh   const char *src_end;  /* end ('\0') of source string */
3618e3e3a7aSWarner Losh   const char *p_end;  /* end ('\0') of pattern */
3628e3e3a7aSWarner Losh   lua_State *L;
3638e3e3a7aSWarner Losh   int matchdepth;  /* control for recursive depth (to avoid C stack overflow) */
3648e3e3a7aSWarner Losh   unsigned char level;  /* total number of captures (finished or unfinished) */
3658e3e3a7aSWarner Losh   struct {
3668e3e3a7aSWarner Losh     const char *init;
3678e3e3a7aSWarner Losh     ptrdiff_t len;
3688e3e3a7aSWarner Losh   } capture[LUA_MAXCAPTURES];
3698e3e3a7aSWarner Losh } MatchState;
3708e3e3a7aSWarner Losh 
3718e3e3a7aSWarner Losh 
3728e3e3a7aSWarner Losh /* recursive function */
3738e3e3a7aSWarner Losh static const char *match (MatchState *ms, const char *s, const char *p);
3748e3e3a7aSWarner Losh 
3758e3e3a7aSWarner Losh 
3768e3e3a7aSWarner Losh /* maximum recursion depth for 'match' */
3778e3e3a7aSWarner Losh #if !defined(MAXCCALLS)
3788e3e3a7aSWarner Losh #define MAXCCALLS	200
3798e3e3a7aSWarner Losh #endif
3808e3e3a7aSWarner Losh 
3818e3e3a7aSWarner Losh 
3828e3e3a7aSWarner Losh #define L_ESC		'%'
3838e3e3a7aSWarner Losh #define SPECIALS	"^$*+?.([%-"
3848e3e3a7aSWarner Losh 
3858e3e3a7aSWarner Losh 
check_capture(MatchState * ms,int l)3868e3e3a7aSWarner Losh static int check_capture (MatchState *ms, int l) {
3878e3e3a7aSWarner Losh   l -= '1';
3888c784bb8SWarner Losh   if (l_unlikely(l < 0 || l >= ms->level ||
3898c784bb8SWarner Losh                  ms->capture[l].len == CAP_UNFINISHED))
3908e3e3a7aSWarner Losh     return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
3918e3e3a7aSWarner Losh   return l;
3928e3e3a7aSWarner Losh }
3938e3e3a7aSWarner Losh 
3948e3e3a7aSWarner Losh 
capture_to_close(MatchState * ms)3958e3e3a7aSWarner Losh static int capture_to_close (MatchState *ms) {
3968e3e3a7aSWarner Losh   int level = ms->level;
3978e3e3a7aSWarner Losh   for (level--; level>=0; level--)
3988e3e3a7aSWarner Losh     if (ms->capture[level].len == CAP_UNFINISHED) return level;
3998e3e3a7aSWarner Losh   return luaL_error(ms->L, "invalid pattern capture");
4008e3e3a7aSWarner Losh }
4018e3e3a7aSWarner Losh 
4028e3e3a7aSWarner Losh 
classend(MatchState * ms,const char * p)4038e3e3a7aSWarner Losh static const char *classend (MatchState *ms, const char *p) {
4048e3e3a7aSWarner Losh   switch (*p++) {
4058e3e3a7aSWarner Losh     case L_ESC: {
4068c784bb8SWarner Losh       if (l_unlikely(p == ms->p_end))
4078e3e3a7aSWarner Losh         luaL_error(ms->L, "malformed pattern (ends with '%%')");
4088e3e3a7aSWarner Losh       return p+1;
4098e3e3a7aSWarner Losh     }
4108e3e3a7aSWarner Losh     case '[': {
4118e3e3a7aSWarner Losh       if (*p == '^') p++;
4128e3e3a7aSWarner Losh       do {  /* look for a ']' */
4138c784bb8SWarner Losh         if (l_unlikely(p == ms->p_end))
4148e3e3a7aSWarner Losh           luaL_error(ms->L, "malformed pattern (missing ']')");
4158e3e3a7aSWarner Losh         if (*(p++) == L_ESC && p < ms->p_end)
4168e3e3a7aSWarner Losh           p++;  /* skip escapes (e.g. '%]') */
4178e3e3a7aSWarner Losh       } while (*p != ']');
4188e3e3a7aSWarner Losh       return p+1;
4198e3e3a7aSWarner Losh     }
4208e3e3a7aSWarner Losh     default: {
4218e3e3a7aSWarner Losh       return p;
4228e3e3a7aSWarner Losh     }
4238e3e3a7aSWarner Losh   }
4248e3e3a7aSWarner Losh }
4258e3e3a7aSWarner Losh 
4268e3e3a7aSWarner Losh 
match_class(int c,int cl)4278e3e3a7aSWarner Losh static int match_class (int c, int cl) {
4288e3e3a7aSWarner Losh   int res;
4298e3e3a7aSWarner Losh   switch (tolower(cl)) {
4308e3e3a7aSWarner Losh     case 'a' : res = isalpha(c); break;
4318e3e3a7aSWarner Losh     case 'c' : res = iscntrl(c); break;
4328e3e3a7aSWarner Losh     case 'd' : res = isdigit(c); break;
4338e3e3a7aSWarner Losh     case 'g' : res = isgraph(c); break;
4348e3e3a7aSWarner Losh     case 'l' : res = islower(c); break;
4358e3e3a7aSWarner Losh     case 'p' : res = ispunct(c); break;
4368e3e3a7aSWarner Losh     case 's' : res = isspace(c); break;
4378e3e3a7aSWarner Losh     case 'u' : res = isupper(c); break;
4388e3e3a7aSWarner Losh     case 'w' : res = isalnum(c); break;
4398e3e3a7aSWarner Losh     case 'x' : res = isxdigit(c); break;
4408e3e3a7aSWarner Losh     case 'z' : res = (c == 0); break;  /* deprecated option */
4418e3e3a7aSWarner Losh     default: return (cl == c);
4428e3e3a7aSWarner Losh   }
4438e3e3a7aSWarner Losh   return (islower(cl) ? res : !res);
4448e3e3a7aSWarner Losh }
4458e3e3a7aSWarner Losh 
4468e3e3a7aSWarner Losh 
matchbracketclass(int c,const char * p,const char * ec)4478e3e3a7aSWarner Losh static int matchbracketclass (int c, const char *p, const char *ec) {
4488e3e3a7aSWarner Losh   int sig = 1;
4498e3e3a7aSWarner Losh   if (*(p+1) == '^') {
4508e3e3a7aSWarner Losh     sig = 0;
4518e3e3a7aSWarner Losh     p++;  /* skip the '^' */
4528e3e3a7aSWarner Losh   }
4538e3e3a7aSWarner Losh   while (++p < ec) {
4548e3e3a7aSWarner Losh     if (*p == L_ESC) {
4558e3e3a7aSWarner Losh       p++;
4568e3e3a7aSWarner Losh       if (match_class(c, uchar(*p)))
4578e3e3a7aSWarner Losh         return sig;
4588e3e3a7aSWarner Losh     }
4598e3e3a7aSWarner Losh     else if ((*(p+1) == '-') && (p+2 < ec)) {
4608e3e3a7aSWarner Losh       p+=2;
4618e3e3a7aSWarner Losh       if (uchar(*(p-2)) <= c && c <= uchar(*p))
4628e3e3a7aSWarner Losh         return sig;
4638e3e3a7aSWarner Losh     }
4648e3e3a7aSWarner Losh     else if (uchar(*p) == c) return sig;
4658e3e3a7aSWarner Losh   }
4668e3e3a7aSWarner Losh   return !sig;
4678e3e3a7aSWarner Losh }
4688e3e3a7aSWarner Losh 
4698e3e3a7aSWarner Losh 
singlematch(MatchState * ms,const char * s,const char * p,const char * ep)4708e3e3a7aSWarner Losh static int singlematch (MatchState *ms, const char *s, const char *p,
4718e3e3a7aSWarner Losh                         const char *ep) {
4728e3e3a7aSWarner Losh   if (s >= ms->src_end)
4738e3e3a7aSWarner Losh     return 0;
4748e3e3a7aSWarner Losh   else {
4758e3e3a7aSWarner Losh     int c = uchar(*s);
4768e3e3a7aSWarner Losh     switch (*p) {
4778e3e3a7aSWarner Losh       case '.': return 1;  /* matches any char */
4788e3e3a7aSWarner Losh       case L_ESC: return match_class(c, uchar(*(p+1)));
4798e3e3a7aSWarner Losh       case '[': return matchbracketclass(c, p, ep-1);
4808e3e3a7aSWarner Losh       default:  return (uchar(*p) == c);
4818e3e3a7aSWarner Losh     }
4828e3e3a7aSWarner Losh   }
4838e3e3a7aSWarner Losh }
4848e3e3a7aSWarner Losh 
4858e3e3a7aSWarner Losh 
matchbalance(MatchState * ms,const char * s,const char * p)4868e3e3a7aSWarner Losh static const char *matchbalance (MatchState *ms, const char *s,
4878e3e3a7aSWarner Losh                                    const char *p) {
4888c784bb8SWarner Losh   if (l_unlikely(p >= ms->p_end - 1))
4898e3e3a7aSWarner Losh     luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
4908e3e3a7aSWarner Losh   if (*s != *p) return NULL;
4918e3e3a7aSWarner Losh   else {
4928e3e3a7aSWarner Losh     int b = *p;
4938e3e3a7aSWarner Losh     int e = *(p+1);
4948e3e3a7aSWarner Losh     int cont = 1;
4958e3e3a7aSWarner Losh     while (++s < ms->src_end) {
4968e3e3a7aSWarner Losh       if (*s == e) {
4978e3e3a7aSWarner Losh         if (--cont == 0) return s+1;
4988e3e3a7aSWarner Losh       }
4998e3e3a7aSWarner Losh       else if (*s == b) cont++;
5008e3e3a7aSWarner Losh     }
5018e3e3a7aSWarner Losh   }
5028e3e3a7aSWarner Losh   return NULL;  /* string ends out of balance */
5038e3e3a7aSWarner Losh }
5048e3e3a7aSWarner Losh 
5058e3e3a7aSWarner Losh 
max_expand(MatchState * ms,const char * s,const char * p,const char * ep)5068e3e3a7aSWarner Losh static const char *max_expand (MatchState *ms, const char *s,
5078e3e3a7aSWarner Losh                                  const char *p, const char *ep) {
5088e3e3a7aSWarner Losh   ptrdiff_t i = 0;  /* counts maximum expand for item */
5098e3e3a7aSWarner Losh   while (singlematch(ms, s + i, p, ep))
5108e3e3a7aSWarner Losh     i++;
5118e3e3a7aSWarner Losh   /* keeps trying to match with the maximum repetitions */
5128e3e3a7aSWarner Losh   while (i>=0) {
5138e3e3a7aSWarner Losh     const char *res = match(ms, (s+i), ep+1);
5148e3e3a7aSWarner Losh     if (res) return res;
5158e3e3a7aSWarner Losh     i--;  /* else didn't match; reduce 1 repetition to try again */
5168e3e3a7aSWarner Losh   }
5178e3e3a7aSWarner Losh   return NULL;
5188e3e3a7aSWarner Losh }
5198e3e3a7aSWarner Losh 
5208e3e3a7aSWarner Losh 
min_expand(MatchState * ms,const char * s,const char * p,const char * ep)5218e3e3a7aSWarner Losh static const char *min_expand (MatchState *ms, const char *s,
5228e3e3a7aSWarner Losh                                  const char *p, const char *ep) {
5238e3e3a7aSWarner Losh   for (;;) {
5248e3e3a7aSWarner Losh     const char *res = match(ms, s, ep+1);
5258e3e3a7aSWarner Losh     if (res != NULL)
5268e3e3a7aSWarner Losh       return res;
5278e3e3a7aSWarner Losh     else if (singlematch(ms, s, p, ep))
5288e3e3a7aSWarner Losh       s++;  /* try with one more repetition */
5298e3e3a7aSWarner Losh     else return NULL;
5308e3e3a7aSWarner Losh   }
5318e3e3a7aSWarner Losh }
5328e3e3a7aSWarner Losh 
5338e3e3a7aSWarner Losh 
start_capture(MatchState * ms,const char * s,const char * p,int what)5348e3e3a7aSWarner Losh static const char *start_capture (MatchState *ms, const char *s,
5358e3e3a7aSWarner Losh                                     const char *p, int what) {
5368e3e3a7aSWarner Losh   const char *res;
5378e3e3a7aSWarner Losh   int level = ms->level;
5388e3e3a7aSWarner Losh   if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
5398e3e3a7aSWarner Losh   ms->capture[level].init = s;
5408e3e3a7aSWarner Losh   ms->capture[level].len = what;
5418e3e3a7aSWarner Losh   ms->level = level+1;
5428e3e3a7aSWarner Losh   if ((res=match(ms, s, p)) == NULL)  /* match failed? */
5438e3e3a7aSWarner Losh     ms->level--;  /* undo capture */
5448e3e3a7aSWarner Losh   return res;
5458e3e3a7aSWarner Losh }
5468e3e3a7aSWarner Losh 
5478e3e3a7aSWarner Losh 
end_capture(MatchState * ms,const char * s,const char * p)5488e3e3a7aSWarner Losh static const char *end_capture (MatchState *ms, const char *s,
5498e3e3a7aSWarner Losh                                   const char *p) {
5508e3e3a7aSWarner Losh   int l = capture_to_close(ms);
5518e3e3a7aSWarner Losh   const char *res;
5528e3e3a7aSWarner Losh   ms->capture[l].len = s - ms->capture[l].init;  /* close capture */
5538e3e3a7aSWarner Losh   if ((res = match(ms, s, p)) == NULL)  /* match failed? */
5548e3e3a7aSWarner Losh     ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */
5558e3e3a7aSWarner Losh   return res;
5568e3e3a7aSWarner Losh }
5578e3e3a7aSWarner Losh 
5588e3e3a7aSWarner Losh 
match_capture(MatchState * ms,const char * s,int l)5598e3e3a7aSWarner Losh static const char *match_capture (MatchState *ms, const char *s, int l) {
5608e3e3a7aSWarner Losh   size_t len;
5618e3e3a7aSWarner Losh   l = check_capture(ms, l);
5628e3e3a7aSWarner Losh   len = ms->capture[l].len;
5638e3e3a7aSWarner Losh   if ((size_t)(ms->src_end-s) >= len &&
5648e3e3a7aSWarner Losh       memcmp(ms->capture[l].init, s, len) == 0)
5658e3e3a7aSWarner Losh     return s+len;
5668e3e3a7aSWarner Losh   else return NULL;
5678e3e3a7aSWarner Losh }
5688e3e3a7aSWarner Losh 
5698e3e3a7aSWarner Losh 
match(MatchState * ms,const char * s,const char * p)5708e3e3a7aSWarner Losh static const char *match (MatchState *ms, const char *s, const char *p) {
5718c784bb8SWarner Losh   if (l_unlikely(ms->matchdepth-- == 0))
5728e3e3a7aSWarner Losh     luaL_error(ms->L, "pattern too complex");
573*a9490b81SWarner Losh   init: /* using goto to optimize tail recursion */
5748e3e3a7aSWarner Losh   if (p != ms->p_end) {  /* end of pattern? */
5758e3e3a7aSWarner Losh     switch (*p) {
5768e3e3a7aSWarner Losh       case '(': {  /* start capture */
5778e3e3a7aSWarner Losh         if (*(p + 1) == ')')  /* position capture? */
5788e3e3a7aSWarner Losh           s = start_capture(ms, s, p + 2, CAP_POSITION);
5798e3e3a7aSWarner Losh         else
5808e3e3a7aSWarner Losh           s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
5818e3e3a7aSWarner Losh         break;
5828e3e3a7aSWarner Losh       }
5838e3e3a7aSWarner Losh       case ')': {  /* end capture */
5848e3e3a7aSWarner Losh         s = end_capture(ms, s, p + 1);
5858e3e3a7aSWarner Losh         break;
5868e3e3a7aSWarner Losh       }
5878e3e3a7aSWarner Losh       case '$': {
5888e3e3a7aSWarner Losh         if ((p + 1) != ms->p_end)  /* is the '$' the last char in pattern? */
5898e3e3a7aSWarner Losh           goto dflt;  /* no; go to default */
5908e3e3a7aSWarner Losh         s = (s == ms->src_end) ? s : NULL;  /* check end of string */
5918e3e3a7aSWarner Losh         break;
5928e3e3a7aSWarner Losh       }
5938e3e3a7aSWarner Losh       case L_ESC: {  /* escaped sequences not in the format class[*+?-]? */
5948e3e3a7aSWarner Losh         switch (*(p + 1)) {
5958e3e3a7aSWarner Losh           case 'b': {  /* balanced string? */
5968e3e3a7aSWarner Losh             s = matchbalance(ms, s, p + 2);
5978e3e3a7aSWarner Losh             if (s != NULL) {
5988e3e3a7aSWarner Losh               p += 4; goto init;  /* return match(ms, s, p + 4); */
5998e3e3a7aSWarner Losh             }  /* else fail (s == NULL) */
6008e3e3a7aSWarner Losh             break;
6018e3e3a7aSWarner Losh           }
6028e3e3a7aSWarner Losh           case 'f': {  /* frontier? */
6038e3e3a7aSWarner Losh             const char *ep; char previous;
6048e3e3a7aSWarner Losh             p += 2;
6058c784bb8SWarner Losh             if (l_unlikely(*p != '['))
6068e3e3a7aSWarner Losh               luaL_error(ms->L, "missing '[' after '%%f' in pattern");
6078e3e3a7aSWarner Losh             ep = classend(ms, p);  /* points to what is next */
6088e3e3a7aSWarner Losh             previous = (s == ms->src_init) ? '\0' : *(s - 1);
6098e3e3a7aSWarner Losh             if (!matchbracketclass(uchar(previous), p, ep - 1) &&
6108e3e3a7aSWarner Losh                matchbracketclass(uchar(*s), p, ep - 1)) {
6118e3e3a7aSWarner Losh               p = ep; goto init;  /* return match(ms, s, ep); */
6128e3e3a7aSWarner Losh             }
6138e3e3a7aSWarner Losh             s = NULL;  /* match failed */
6148e3e3a7aSWarner Losh             break;
6158e3e3a7aSWarner Losh           }
6168e3e3a7aSWarner Losh           case '0': case '1': case '2': case '3':
6178e3e3a7aSWarner Losh           case '4': case '5': case '6': case '7':
6188e3e3a7aSWarner Losh           case '8': case '9': {  /* capture results (%0-%9)? */
6198e3e3a7aSWarner Losh             s = match_capture(ms, s, uchar(*(p + 1)));
6208e3e3a7aSWarner Losh             if (s != NULL) {
6218e3e3a7aSWarner Losh               p += 2; goto init;  /* return match(ms, s, p + 2) */
6228e3e3a7aSWarner Losh             }
6238e3e3a7aSWarner Losh             break;
6248e3e3a7aSWarner Losh           }
6258e3e3a7aSWarner Losh           default: goto dflt;
6268e3e3a7aSWarner Losh         }
6278e3e3a7aSWarner Losh         break;
6288e3e3a7aSWarner Losh       }
6298e3e3a7aSWarner Losh       default: dflt: {  /* pattern class plus optional suffix */
6308e3e3a7aSWarner Losh         const char *ep = classend(ms, p);  /* points to optional suffix */
6318e3e3a7aSWarner Losh         /* does not match at least once? */
6328e3e3a7aSWarner Losh         if (!singlematch(ms, s, p, ep)) {
6338e3e3a7aSWarner Losh           if (*ep == '*' || *ep == '?' || *ep == '-') {  /* accept empty? */
6348e3e3a7aSWarner Losh             p = ep + 1; goto init;  /* return match(ms, s, ep + 1); */
6358e3e3a7aSWarner Losh           }
6368e3e3a7aSWarner Losh           else  /* '+' or no suffix */
6378e3e3a7aSWarner Losh             s = NULL;  /* fail */
6388e3e3a7aSWarner Losh         }
6398e3e3a7aSWarner Losh         else {  /* matched once */
6408e3e3a7aSWarner Losh           switch (*ep) {  /* handle optional suffix */
6418e3e3a7aSWarner Losh             case '?': {  /* optional */
6428e3e3a7aSWarner Losh               const char *res;
6438e3e3a7aSWarner Losh               if ((res = match(ms, s + 1, ep + 1)) != NULL)
6448e3e3a7aSWarner Losh                 s = res;
6458e3e3a7aSWarner Losh               else {
6468e3e3a7aSWarner Losh                 p = ep + 1; goto init;  /* else return match(ms, s, ep + 1); */
6478e3e3a7aSWarner Losh               }
6488e3e3a7aSWarner Losh               break;
6498e3e3a7aSWarner Losh             }
6508e3e3a7aSWarner Losh             case '+':  /* 1 or more repetitions */
6518e3e3a7aSWarner Losh               s++;  /* 1 match already done */
6528e3e3a7aSWarner Losh               /* FALLTHROUGH */
6538e3e3a7aSWarner Losh             case '*':  /* 0 or more repetitions */
6548e3e3a7aSWarner Losh               s = max_expand(ms, s, p, ep);
6558e3e3a7aSWarner Losh               break;
6568e3e3a7aSWarner Losh             case '-':  /* 0 or more repetitions (minimum) */
6578e3e3a7aSWarner Losh               s = min_expand(ms, s, p, ep);
6588e3e3a7aSWarner Losh               break;
6598e3e3a7aSWarner Losh             default:  /* no suffix */
6608e3e3a7aSWarner Losh               s++; p = ep; goto init;  /* return match(ms, s + 1, ep); */
6618e3e3a7aSWarner Losh           }
6628e3e3a7aSWarner Losh         }
6638e3e3a7aSWarner Losh         break;
6648e3e3a7aSWarner Losh       }
6658e3e3a7aSWarner Losh     }
6668e3e3a7aSWarner Losh   }
6678e3e3a7aSWarner Losh   ms->matchdepth++;
6688e3e3a7aSWarner Losh   return s;
6698e3e3a7aSWarner Losh }
6708e3e3a7aSWarner Losh 
6718e3e3a7aSWarner Losh 
6728e3e3a7aSWarner Losh 
lmemfind(const char * s1,size_t l1,const char * s2,size_t l2)6738e3e3a7aSWarner Losh static const char *lmemfind (const char *s1, size_t l1,
6748e3e3a7aSWarner Losh                                const char *s2, size_t l2) {
6758e3e3a7aSWarner Losh   if (l2 == 0) return s1;  /* empty strings are everywhere */
6768e3e3a7aSWarner Losh   else if (l2 > l1) return NULL;  /* avoids a negative 'l1' */
6778e3e3a7aSWarner Losh   else {
6788e3e3a7aSWarner Losh     const char *init;  /* to search for a '*s2' inside 's1' */
6798e3e3a7aSWarner Losh     l2--;  /* 1st char will be checked by 'memchr' */
6808e3e3a7aSWarner Losh     l1 = l1-l2;  /* 's2' cannot be found after that */
6818e3e3a7aSWarner Losh     while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
6828e3e3a7aSWarner Losh       init++;   /* 1st char is already checked */
6838e3e3a7aSWarner Losh       if (memcmp(init, s2+1, l2) == 0)
6848e3e3a7aSWarner Losh         return init-1;
6858e3e3a7aSWarner Losh       else {  /* correct 'l1' and 's1' to try again */
6868e3e3a7aSWarner Losh         l1 -= init-s1;
6878e3e3a7aSWarner Losh         s1 = init;
6888e3e3a7aSWarner Losh       }
6898e3e3a7aSWarner Losh     }
6908e3e3a7aSWarner Losh     return NULL;  /* not found */
6918e3e3a7aSWarner Losh   }
6928e3e3a7aSWarner Losh }
6938e3e3a7aSWarner Losh 
6948e3e3a7aSWarner Losh 
6950495ed39SKyle Evans /*
6960495ed39SKyle Evans ** get information about the i-th capture. If there are no captures
6970495ed39SKyle Evans ** and 'i==0', return information about the whole match, which
6980495ed39SKyle Evans ** is the range 's'..'e'. If the capture is a string, return
6990495ed39SKyle Evans ** its length and put its address in '*cap'. If it is an integer
7000495ed39SKyle Evans ** (a position), push it on the stack and return CAP_POSITION.
7010495ed39SKyle Evans */
get_onecapture(MatchState * ms,int i,const char * s,const char * e,const char ** cap)7020495ed39SKyle Evans static size_t get_onecapture (MatchState *ms, int i, const char *s,
7030495ed39SKyle Evans                               const char *e, const char **cap) {
7048e3e3a7aSWarner Losh   if (i >= ms->level) {
7058c784bb8SWarner Losh     if (l_unlikely(i != 0))
7068e3e3a7aSWarner Losh       luaL_error(ms->L, "invalid capture index %%%d", i + 1);
7070495ed39SKyle Evans     *cap = s;
7080495ed39SKyle Evans     return e - s;
7098e3e3a7aSWarner Losh   }
7108e3e3a7aSWarner Losh   else {
7110495ed39SKyle Evans     ptrdiff_t capl = ms->capture[i].len;
7120495ed39SKyle Evans     *cap = ms->capture[i].init;
7138c784bb8SWarner Losh     if (l_unlikely(capl == CAP_UNFINISHED))
7140495ed39SKyle Evans       luaL_error(ms->L, "unfinished capture");
7150495ed39SKyle Evans     else if (capl == CAP_POSITION)
7168e3e3a7aSWarner Losh       lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
7170495ed39SKyle Evans     return capl;
7188e3e3a7aSWarner Losh   }
7198e3e3a7aSWarner Losh }
7208e3e3a7aSWarner Losh 
7218e3e3a7aSWarner Losh 
7220495ed39SKyle Evans /*
7230495ed39SKyle Evans ** Push the i-th capture on the stack.
7240495ed39SKyle Evans */
push_onecapture(MatchState * ms,int i,const char * s,const char * e)7250495ed39SKyle Evans static void push_onecapture (MatchState *ms, int i, const char *s,
7260495ed39SKyle Evans                                                     const char *e) {
7270495ed39SKyle Evans   const char *cap;
7280495ed39SKyle Evans   ptrdiff_t l = get_onecapture(ms, i, s, e, &cap);
7290495ed39SKyle Evans   if (l != CAP_POSITION)
7300495ed39SKyle Evans     lua_pushlstring(ms->L, cap, l);
7310495ed39SKyle Evans   /* else position was already pushed */
7320495ed39SKyle Evans }
7330495ed39SKyle Evans 
7340495ed39SKyle Evans 
push_captures(MatchState * ms,const char * s,const char * e)7358e3e3a7aSWarner Losh static int push_captures (MatchState *ms, const char *s, const char *e) {
7368e3e3a7aSWarner Losh   int i;
7378e3e3a7aSWarner Losh   int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
7388e3e3a7aSWarner Losh   luaL_checkstack(ms->L, nlevels, "too many captures");
7398e3e3a7aSWarner Losh   for (i = 0; i < nlevels; i++)
7408e3e3a7aSWarner Losh     push_onecapture(ms, i, s, e);
7418e3e3a7aSWarner Losh   return nlevels;  /* number of strings pushed */
7428e3e3a7aSWarner Losh }
7438e3e3a7aSWarner Losh 
7448e3e3a7aSWarner Losh 
7458e3e3a7aSWarner Losh /* check whether pattern has no special characters */
nospecials(const char * p,size_t l)7468e3e3a7aSWarner Losh static int nospecials (const char *p, size_t l) {
7478e3e3a7aSWarner Losh   size_t upto = 0;
7488e3e3a7aSWarner Losh   do {
7498e3e3a7aSWarner Losh     if (strpbrk(p + upto, SPECIALS))
7508e3e3a7aSWarner Losh       return 0;  /* pattern has a special character */
7518e3e3a7aSWarner Losh     upto += strlen(p + upto) + 1;  /* may have more after \0 */
7528e3e3a7aSWarner Losh   } while (upto <= l);
7538e3e3a7aSWarner Losh   return 1;  /* no special chars found */
7548e3e3a7aSWarner Losh }
7558e3e3a7aSWarner Losh 
7568e3e3a7aSWarner Losh 
prepstate(MatchState * ms,lua_State * L,const char * s,size_t ls,const char * p,size_t lp)7578e3e3a7aSWarner Losh static void prepstate (MatchState *ms, lua_State *L,
7588e3e3a7aSWarner Losh                        const char *s, size_t ls, const char *p, size_t lp) {
7598e3e3a7aSWarner Losh   ms->L = L;
7608e3e3a7aSWarner Losh   ms->matchdepth = MAXCCALLS;
7618e3e3a7aSWarner Losh   ms->src_init = s;
7628e3e3a7aSWarner Losh   ms->src_end = s + ls;
7638e3e3a7aSWarner Losh   ms->p_end = p + lp;
7648e3e3a7aSWarner Losh }
7658e3e3a7aSWarner Losh 
7668e3e3a7aSWarner Losh 
reprepstate(MatchState * ms)7678e3e3a7aSWarner Losh static void reprepstate (MatchState *ms) {
7688e3e3a7aSWarner Losh   ms->level = 0;
7698e3e3a7aSWarner Losh   lua_assert(ms->matchdepth == MAXCCALLS);
7708e3e3a7aSWarner Losh }
7718e3e3a7aSWarner Losh 
7728e3e3a7aSWarner Losh 
str_find_aux(lua_State * L,int find)7738e3e3a7aSWarner Losh static int str_find_aux (lua_State *L, int find) {
7748e3e3a7aSWarner Losh   size_t ls, lp;
7758e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &ls);
7768e3e3a7aSWarner Losh   const char *p = luaL_checklstring(L, 2, &lp);
7770495ed39SKyle Evans   size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
7780495ed39SKyle Evans   if (init > ls) {  /* start after string's end? */
7790495ed39SKyle Evans     luaL_pushfail(L);  /* cannot find anything */
7808e3e3a7aSWarner Losh     return 1;
7818e3e3a7aSWarner Losh   }
7828e3e3a7aSWarner Losh   /* explicit request or no special characters? */
7838e3e3a7aSWarner Losh   if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
7848e3e3a7aSWarner Losh     /* do a plain search */
7850495ed39SKyle Evans     const char *s2 = lmemfind(s + init, ls - init, p, lp);
7868e3e3a7aSWarner Losh     if (s2) {
7878e3e3a7aSWarner Losh       lua_pushinteger(L, (s2 - s) + 1);
7888e3e3a7aSWarner Losh       lua_pushinteger(L, (s2 - s) + lp);
7898e3e3a7aSWarner Losh       return 2;
7908e3e3a7aSWarner Losh     }
7918e3e3a7aSWarner Losh   }
7928e3e3a7aSWarner Losh   else {
7938e3e3a7aSWarner Losh     MatchState ms;
7940495ed39SKyle Evans     const char *s1 = s + init;
7958e3e3a7aSWarner Losh     int anchor = (*p == '^');
7968e3e3a7aSWarner Losh     if (anchor) {
7978e3e3a7aSWarner Losh       p++; lp--;  /* skip anchor character */
7988e3e3a7aSWarner Losh     }
7998e3e3a7aSWarner Losh     prepstate(&ms, L, s, ls, p, lp);
8008e3e3a7aSWarner Losh     do {
8018e3e3a7aSWarner Losh       const char *res;
8028e3e3a7aSWarner Losh       reprepstate(&ms);
8038e3e3a7aSWarner Losh       if ((res=match(&ms, s1, p)) != NULL) {
8048e3e3a7aSWarner Losh         if (find) {
8058e3e3a7aSWarner Losh           lua_pushinteger(L, (s1 - s) + 1);  /* start */
8068e3e3a7aSWarner Losh           lua_pushinteger(L, res - s);   /* end */
8078e3e3a7aSWarner Losh           return push_captures(&ms, NULL, 0) + 2;
8088e3e3a7aSWarner Losh         }
8098e3e3a7aSWarner Losh         else
8108e3e3a7aSWarner Losh           return push_captures(&ms, s1, res);
8118e3e3a7aSWarner Losh       }
8128e3e3a7aSWarner Losh     } while (s1++ < ms.src_end && !anchor);
8138e3e3a7aSWarner Losh   }
8140495ed39SKyle Evans   luaL_pushfail(L);  /* not found */
8158e3e3a7aSWarner Losh   return 1;
8168e3e3a7aSWarner Losh }
8178e3e3a7aSWarner Losh 
8188e3e3a7aSWarner Losh 
str_find(lua_State * L)8198e3e3a7aSWarner Losh static int str_find (lua_State *L) {
8208e3e3a7aSWarner Losh   return str_find_aux(L, 1);
8218e3e3a7aSWarner Losh }
8228e3e3a7aSWarner Losh 
8238e3e3a7aSWarner Losh 
str_match(lua_State * L)8248e3e3a7aSWarner Losh static int str_match (lua_State *L) {
8258e3e3a7aSWarner Losh   return str_find_aux(L, 0);
8268e3e3a7aSWarner Losh }
8278e3e3a7aSWarner Losh 
8288e3e3a7aSWarner Losh 
8298e3e3a7aSWarner Losh /* state for 'gmatch' */
8308e3e3a7aSWarner Losh typedef struct GMatchState {
8318e3e3a7aSWarner Losh   const char *src;  /* current position */
8328e3e3a7aSWarner Losh   const char *p;  /* pattern */
8338e3e3a7aSWarner Losh   const char *lastmatch;  /* end of last match */
8348e3e3a7aSWarner Losh   MatchState ms;  /* match state */
8358e3e3a7aSWarner Losh } GMatchState;
8368e3e3a7aSWarner Losh 
8378e3e3a7aSWarner Losh 
gmatch_aux(lua_State * L)8388e3e3a7aSWarner Losh static int gmatch_aux (lua_State *L) {
8398e3e3a7aSWarner Losh   GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3));
8408e3e3a7aSWarner Losh   const char *src;
8418e3e3a7aSWarner Losh   gm->ms.L = L;
8428e3e3a7aSWarner Losh   for (src = gm->src; src <= gm->ms.src_end; src++) {
8438e3e3a7aSWarner Losh     const char *e;
8448e3e3a7aSWarner Losh     reprepstate(&gm->ms);
8458e3e3a7aSWarner Losh     if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) {
8468e3e3a7aSWarner Losh       gm->src = gm->lastmatch = e;
8478e3e3a7aSWarner Losh       return push_captures(&gm->ms, src, e);
8488e3e3a7aSWarner Losh     }
8498e3e3a7aSWarner Losh   }
8508e3e3a7aSWarner Losh   return 0;  /* not found */
8518e3e3a7aSWarner Losh }
8528e3e3a7aSWarner Losh 
8538e3e3a7aSWarner Losh 
gmatch(lua_State * L)8548e3e3a7aSWarner Losh static int gmatch (lua_State *L) {
8558e3e3a7aSWarner Losh   size_t ls, lp;
8568e3e3a7aSWarner Losh   const char *s = luaL_checklstring(L, 1, &ls);
8578e3e3a7aSWarner Losh   const char *p = luaL_checklstring(L, 2, &lp);
8580495ed39SKyle Evans   size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
8598e3e3a7aSWarner Losh   GMatchState *gm;
8600495ed39SKyle Evans   lua_settop(L, 2);  /* keep strings on closure to avoid being collected */
8610495ed39SKyle Evans   gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0);
8620495ed39SKyle Evans   if (init > ls)  /* start after string's end? */
8630495ed39SKyle Evans     init = ls + 1;  /* avoid overflows in 's + init' */
8648e3e3a7aSWarner Losh   prepstate(&gm->ms, L, s, ls, p, lp);
8650495ed39SKyle Evans   gm->src = s + init; gm->p = p; gm->lastmatch = NULL;
8668e3e3a7aSWarner Losh   lua_pushcclosure(L, gmatch_aux, 3);
8678e3e3a7aSWarner Losh   return 1;
8688e3e3a7aSWarner Losh }
8698e3e3a7aSWarner Losh 
8708e3e3a7aSWarner Losh 
add_s(MatchState * ms,luaL_Buffer * b,const char * s,const char * e)8718e3e3a7aSWarner Losh static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
8728e3e3a7aSWarner Losh                                                    const char *e) {
8730495ed39SKyle Evans   size_t l;
8748e3e3a7aSWarner Losh   lua_State *L = ms->L;
8758e3e3a7aSWarner Losh   const char *news = lua_tolstring(L, 3, &l);
8760495ed39SKyle Evans   const char *p;
8770495ed39SKyle Evans   while ((p = (char *)memchr(news, L_ESC, l)) != NULL) {
8780495ed39SKyle Evans     luaL_addlstring(b, news, p - news);
8790495ed39SKyle Evans     p++;  /* skip ESC */
8800495ed39SKyle Evans     if (*p == L_ESC)  /* '%%' */
8810495ed39SKyle Evans       luaL_addchar(b, *p);
8820495ed39SKyle Evans     else if (*p == '0')  /* '%0' */
8838e3e3a7aSWarner Losh         luaL_addlstring(b, s, e - s);
8840495ed39SKyle Evans     else if (isdigit(uchar(*p))) {  /* '%n' */
8850495ed39SKyle Evans       const char *cap;
8860495ed39SKyle Evans       ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
8870495ed39SKyle Evans       if (resl == CAP_POSITION)
8880495ed39SKyle Evans         luaL_addvalue(b);  /* add position to accumulated result */
8890495ed39SKyle Evans       else
8900495ed39SKyle Evans         luaL_addlstring(b, cap, resl);
8918e3e3a7aSWarner Losh     }
8920495ed39SKyle Evans     else
8930495ed39SKyle Evans       luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
8940495ed39SKyle Evans     l -= p + 1 - news;
8950495ed39SKyle Evans     news = p + 1;
8968e3e3a7aSWarner Losh   }
8970495ed39SKyle Evans   luaL_addlstring(b, news, l);
8988e3e3a7aSWarner Losh }
8998e3e3a7aSWarner Losh 
9008e3e3a7aSWarner Losh 
9010495ed39SKyle Evans /*
9020495ed39SKyle Evans ** Add the replacement value to the string buffer 'b'.
9030495ed39SKyle Evans ** Return true if the original string was changed. (Function calls and
9040495ed39SKyle Evans ** table indexing resulting in nil or false do not change the subject.)
9050495ed39SKyle Evans */
add_value(MatchState * ms,luaL_Buffer * b,const char * s,const char * e,int tr)9060495ed39SKyle Evans static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
9078e3e3a7aSWarner Losh                                       const char *e, int tr) {
9088e3e3a7aSWarner Losh   lua_State *L = ms->L;
9098e3e3a7aSWarner Losh   switch (tr) {
9100495ed39SKyle Evans     case LUA_TFUNCTION: {  /* call the function */
9118e3e3a7aSWarner Losh       int n;
9120495ed39SKyle Evans       lua_pushvalue(L, 3);  /* push the function */
9130495ed39SKyle Evans       n = push_captures(ms, s, e);  /* all captures as arguments */
9140495ed39SKyle Evans       lua_call(L, n, 1);  /* call it */
9158e3e3a7aSWarner Losh       break;
9168e3e3a7aSWarner Losh     }
9170495ed39SKyle Evans     case LUA_TTABLE: {  /* index the table */
9180495ed39SKyle Evans       push_onecapture(ms, 0, s, e);  /* first capture is the index */
9198e3e3a7aSWarner Losh       lua_gettable(L, 3);
9208e3e3a7aSWarner Losh       break;
9218e3e3a7aSWarner Losh     }
9228e3e3a7aSWarner Losh     default: {  /* LUA_TNUMBER or LUA_TSTRING */
9230495ed39SKyle Evans       add_s(ms, b, s, e);  /* add value to the buffer */
9240495ed39SKyle Evans       return 1;  /* something changed */
9258e3e3a7aSWarner Losh     }
9268e3e3a7aSWarner Losh   }
9278e3e3a7aSWarner Losh   if (!lua_toboolean(L, -1)) {  /* nil or false? */
9280495ed39SKyle Evans     lua_pop(L, 1);  /* remove value */
9290495ed39SKyle Evans     luaL_addlstring(b, s, e - s);  /* keep original text */
9300495ed39SKyle Evans     return 0;  /* no changes */
9318e3e3a7aSWarner Losh   }
9328c784bb8SWarner Losh   else if (l_unlikely(!lua_isstring(L, -1)))
9330495ed39SKyle Evans     return luaL_error(L, "invalid replacement value (a %s)",
9340495ed39SKyle Evans                          luaL_typename(L, -1));
9350495ed39SKyle Evans   else {
9368e3e3a7aSWarner Losh     luaL_addvalue(b);  /* add result to accumulator */
9370495ed39SKyle Evans     return 1;  /* something changed */
9380495ed39SKyle Evans   }
9398e3e3a7aSWarner Losh }
9408e3e3a7aSWarner Losh 
9418e3e3a7aSWarner Losh 
str_gsub(lua_State * L)9428e3e3a7aSWarner Losh static int str_gsub (lua_State *L) {
9438e3e3a7aSWarner Losh   size_t srcl, lp;
9448e3e3a7aSWarner Losh   const char *src = luaL_checklstring(L, 1, &srcl);  /* subject */
9458e3e3a7aSWarner Losh   const char *p = luaL_checklstring(L, 2, &lp);  /* pattern */
9468e3e3a7aSWarner Losh   const char *lastmatch = NULL;  /* end of last match */
9478e3e3a7aSWarner Losh   int tr = lua_type(L, 3);  /* replacement type */
9488e3e3a7aSWarner Losh   lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1);  /* max replacements */
9498e3e3a7aSWarner Losh   int anchor = (*p == '^');
9508e3e3a7aSWarner Losh   lua_Integer n = 0;  /* replacement count */
9510495ed39SKyle Evans   int changed = 0;  /* change flag */
9528e3e3a7aSWarner Losh   MatchState ms;
9538e3e3a7aSWarner Losh   luaL_Buffer b;
9540495ed39SKyle Evans   luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
9558e3e3a7aSWarner Losh                    tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
9560495ed39SKyle Evans                       "string/function/table");
9578e3e3a7aSWarner Losh   luaL_buffinit(L, &b);
9588e3e3a7aSWarner Losh   if (anchor) {
9598e3e3a7aSWarner Losh     p++; lp--;  /* skip anchor character */
9608e3e3a7aSWarner Losh   }
9618e3e3a7aSWarner Losh   prepstate(&ms, L, src, srcl, p, lp);
9628e3e3a7aSWarner Losh   while (n < max_s) {
9638e3e3a7aSWarner Losh     const char *e;
9648e3e3a7aSWarner Losh     reprepstate(&ms);  /* (re)prepare state for new match */
9658e3e3a7aSWarner Losh     if ((e = match(&ms, src, p)) != NULL && e != lastmatch) {  /* match? */
9668e3e3a7aSWarner Losh       n++;
9670495ed39SKyle Evans       changed = add_value(&ms, &b, src, e, tr) | changed;
9688e3e3a7aSWarner Losh       src = lastmatch = e;
9698e3e3a7aSWarner Losh     }
9708e3e3a7aSWarner Losh     else if (src < ms.src_end)  /* otherwise, skip one character */
9718e3e3a7aSWarner Losh       luaL_addchar(&b, *src++);
9728e3e3a7aSWarner Losh     else break;  /* end of subject */
9738e3e3a7aSWarner Losh     if (anchor) break;
9748e3e3a7aSWarner Losh   }
9750495ed39SKyle Evans   if (!changed)  /* no changes? */
9760495ed39SKyle Evans     lua_pushvalue(L, 1);  /* return original string */
9770495ed39SKyle Evans   else {  /* something changed */
9788e3e3a7aSWarner Losh     luaL_addlstring(&b, src, ms.src_end-src);
9790495ed39SKyle Evans     luaL_pushresult(&b);  /* create and return new string */
9800495ed39SKyle Evans   }
9818e3e3a7aSWarner Losh   lua_pushinteger(L, n);  /* number of substitutions */
9828e3e3a7aSWarner Losh   return 2;
9838e3e3a7aSWarner Losh }
9848e3e3a7aSWarner Losh 
9858e3e3a7aSWarner Losh /* }====================================================== */
9868e3e3a7aSWarner Losh 
9878e3e3a7aSWarner Losh 
9888e3e3a7aSWarner Losh 
9898e3e3a7aSWarner Losh /*
9908e3e3a7aSWarner Losh ** {======================================================
9918e3e3a7aSWarner Losh ** STRING FORMAT
9928e3e3a7aSWarner Losh ** =======================================================
9938e3e3a7aSWarner Losh */
9948e3e3a7aSWarner Losh 
9958e3e3a7aSWarner Losh #if !defined(lua_number2strx)	/* { */
9968e3e3a7aSWarner Losh 
9978e3e3a7aSWarner Losh /*
9988e3e3a7aSWarner Losh ** Hexadecimal floating-point formatter
9998e3e3a7aSWarner Losh */
10008e3e3a7aSWarner Losh 
10018e3e3a7aSWarner Losh #define SIZELENMOD	(sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
10028e3e3a7aSWarner Losh 
10038e3e3a7aSWarner Losh 
10048e3e3a7aSWarner Losh /*
10058e3e3a7aSWarner Losh ** Number of bits that goes into the first digit. It can be any value
10068e3e3a7aSWarner Losh ** between 1 and 4; the following definition tries to align the number
10078e3e3a7aSWarner Losh ** to nibble boundaries by making what is left after that first digit a
10088e3e3a7aSWarner Losh ** multiple of 4.
10098e3e3a7aSWarner Losh */
10100495ed39SKyle Evans #define L_NBFD		((l_floatatt(MANT_DIG) - 1)%4 + 1)
10118e3e3a7aSWarner Losh 
10128e3e3a7aSWarner Losh 
10138e3e3a7aSWarner Losh /*
10148e3e3a7aSWarner Losh ** Add integer part of 'x' to buffer and return new 'x'
10158e3e3a7aSWarner Losh */
adddigit(char * buff,int n,lua_Number x)10168e3e3a7aSWarner Losh static lua_Number adddigit (char *buff, int n, lua_Number x) {
10178e3e3a7aSWarner Losh   lua_Number dd = l_mathop(floor)(x);  /* get integer part from 'x' */
10188e3e3a7aSWarner Losh   int d = (int)dd;
10198e3e3a7aSWarner Losh   buff[n] = (d < 10 ? d + '0' : d - 10 + 'a');  /* add to buffer */
10208e3e3a7aSWarner Losh   return x - dd;  /* return what is left */
10218e3e3a7aSWarner Losh }
10228e3e3a7aSWarner Losh 
10238e3e3a7aSWarner Losh 
num2straux(char * buff,int sz,lua_Number x)10248e3e3a7aSWarner Losh static int num2straux (char *buff, int sz, lua_Number x) {
10258e3e3a7aSWarner Losh   /* if 'inf' or 'NaN', format it like '%g' */
10268e3e3a7aSWarner Losh   if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
10278e3e3a7aSWarner Losh     return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
10288e3e3a7aSWarner Losh   else if (x == 0) {  /* can be -0... */
10298e3e3a7aSWarner Losh     /* create "0" or "-0" followed by exponent */
10308e3e3a7aSWarner Losh     return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x);
10318e3e3a7aSWarner Losh   }
10328e3e3a7aSWarner Losh   else {
10338e3e3a7aSWarner Losh     int e;
10348e3e3a7aSWarner Losh     lua_Number m = l_mathop(frexp)(x, &e);  /* 'x' fraction and exponent */
10358e3e3a7aSWarner Losh     int n = 0;  /* character count */
10368e3e3a7aSWarner Losh     if (m < 0) {  /* is number negative? */
10370495ed39SKyle Evans       buff[n++] = '-';  /* add sign */
10388e3e3a7aSWarner Losh       m = -m;  /* make it positive */
10398e3e3a7aSWarner Losh     }
10408e3e3a7aSWarner Losh     buff[n++] = '0'; buff[n++] = 'x';  /* add "0x" */
10418e3e3a7aSWarner Losh     m = adddigit(buff, n++, m * (1 << L_NBFD));  /* add first digit */
10428e3e3a7aSWarner Losh     e -= L_NBFD;  /* this digit goes before the radix point */
10438e3e3a7aSWarner Losh     if (m > 0) {  /* more digits? */
10448e3e3a7aSWarner Losh       buff[n++] = lua_getlocaledecpoint();  /* add radix point */
10458e3e3a7aSWarner Losh       do {  /* add as many digits as needed */
10468e3e3a7aSWarner Losh         m = adddigit(buff, n++, m * 16);
10478e3e3a7aSWarner Losh       } while (m > 0);
10488e3e3a7aSWarner Losh     }
10498e3e3a7aSWarner Losh     n += l_sprintf(buff + n, sz - n, "p%+d", e);  /* add exponent */
10508e3e3a7aSWarner Losh     lua_assert(n < sz);
10518e3e3a7aSWarner Losh     return n;
10528e3e3a7aSWarner Losh   }
10538e3e3a7aSWarner Losh }
10548e3e3a7aSWarner Losh 
10558e3e3a7aSWarner Losh 
lua_number2strx(lua_State * L,char * buff,int sz,const char * fmt,lua_Number x)10568e3e3a7aSWarner Losh static int lua_number2strx (lua_State *L, char *buff, int sz,
10578e3e3a7aSWarner Losh                             const char *fmt, lua_Number x) {
10588e3e3a7aSWarner Losh   int n = num2straux(buff, sz, x);
10598e3e3a7aSWarner Losh   if (fmt[SIZELENMOD] == 'A') {
10608e3e3a7aSWarner Losh     int i;
10618e3e3a7aSWarner Losh     for (i = 0; i < n; i++)
10628e3e3a7aSWarner Losh       buff[i] = toupper(uchar(buff[i]));
10638e3e3a7aSWarner Losh   }
10648c784bb8SWarner Losh   else if (l_unlikely(fmt[SIZELENMOD] != 'a'))
1065e112e9d2SKyle Evans     return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
10668e3e3a7aSWarner Losh   return n;
10678e3e3a7aSWarner Losh }
10688e3e3a7aSWarner Losh 
10698e3e3a7aSWarner Losh #endif				/* } */
10708e3e3a7aSWarner Losh 
10718e3e3a7aSWarner Losh 
10728e3e3a7aSWarner Losh /*
10730495ed39SKyle Evans ** Maximum size for items formatted with '%f'. This size is produced
10748e3e3a7aSWarner Losh ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.',
10758e3e3a7aSWarner Losh ** and '\0') + number of decimal digits to represent maxfloat (which
10760495ed39SKyle Evans ** is maximum exponent + 1). (99+3+1, adding some extra, 110)
10778e3e3a7aSWarner Losh */
10780495ed39SKyle Evans #define MAX_ITEMF	(110 + l_floatatt(MAX_10_EXP))
10790495ed39SKyle Evans 
10800495ed39SKyle Evans 
10810495ed39SKyle Evans /*
10820495ed39SKyle Evans ** All formats except '%f' do not need that large limit.  The other
10830495ed39SKyle Evans ** float formats use exponents, so that they fit in the 99 limit for
10840495ed39SKyle Evans ** significant digits; 's' for large strings and 'q' add items directly
10850495ed39SKyle Evans ** to the buffer; all integer formats also fit in the 99 limit.  The
10860495ed39SKyle Evans ** worst case are floats: they may need 99 significant digits, plus
10870495ed39SKyle Evans ** '0x', '-', '.', 'e+XXXX', and '\0'. Adding some extra, 120.
10880495ed39SKyle Evans */
10890495ed39SKyle Evans #define MAX_ITEM	120
10908e3e3a7aSWarner Losh 
10918e3e3a7aSWarner Losh 
10928e3e3a7aSWarner Losh /* valid flags in a format specification */
10938c784bb8SWarner Losh #if !defined(L_FMTFLAGSF)
10948c784bb8SWarner Losh 
10958c784bb8SWarner Losh /* valid flags for a, A, e, E, f, F, g, and G conversions */
10968c784bb8SWarner Losh #define L_FMTFLAGSF	"-+#0 "
10978c784bb8SWarner Losh 
10988c784bb8SWarner Losh /* valid flags for o, x, and X conversions */
10998c784bb8SWarner Losh #define L_FMTFLAGSX	"-#0"
11008c784bb8SWarner Losh 
11018c784bb8SWarner Losh /* valid flags for d and i conversions */
11028c784bb8SWarner Losh #define L_FMTFLAGSI	"-+0 "
11038c784bb8SWarner Losh 
11048c784bb8SWarner Losh /* valid flags for u conversions */
11058c784bb8SWarner Losh #define L_FMTFLAGSU	"-0"
11068c784bb8SWarner Losh 
11078c784bb8SWarner Losh /* valid flags for c, p, and s conversions */
11088c784bb8SWarner Losh #define L_FMTFLAGSC	"-"
11098c784bb8SWarner Losh 
11100495ed39SKyle Evans #endif
11110495ed39SKyle Evans 
11128e3e3a7aSWarner Losh 
11138e3e3a7aSWarner Losh /*
11148c784bb8SWarner Losh ** Maximum size of each format specification (such as "%-099.99d"):
11158c784bb8SWarner Losh ** Initial '%', flags (up to 5), width (2), period, precision (2),
11168c784bb8SWarner Losh ** length modifier (8), conversion specifier, and final '\0', plus some
11178c784bb8SWarner Losh ** extra.
11188e3e3a7aSWarner Losh */
11198e3e3a7aSWarner Losh #define MAX_FORMAT	32
11208e3e3a7aSWarner Losh 
11218e3e3a7aSWarner Losh 
addquoted(luaL_Buffer * b,const char * s,size_t len)11228e3e3a7aSWarner Losh static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
11238e3e3a7aSWarner Losh   luaL_addchar(b, '"');
11248e3e3a7aSWarner Losh   while (len--) {
11258e3e3a7aSWarner Losh     if (*s == '"' || *s == '\\' || *s == '\n') {
11268e3e3a7aSWarner Losh       luaL_addchar(b, '\\');
11278e3e3a7aSWarner Losh       luaL_addchar(b, *s);
11288e3e3a7aSWarner Losh     }
11298e3e3a7aSWarner Losh     else if (iscntrl(uchar(*s))) {
11308e3e3a7aSWarner Losh       char buff[10];
11318e3e3a7aSWarner Losh       if (!isdigit(uchar(*(s+1))))
11328e3e3a7aSWarner Losh         l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
11338e3e3a7aSWarner Losh       else
11348e3e3a7aSWarner Losh         l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
11358e3e3a7aSWarner Losh       luaL_addstring(b, buff);
11368e3e3a7aSWarner Losh     }
11378e3e3a7aSWarner Losh     else
11388e3e3a7aSWarner Losh       luaL_addchar(b, *s);
11398e3e3a7aSWarner Losh     s++;
11408e3e3a7aSWarner Losh   }
11418e3e3a7aSWarner Losh   luaL_addchar(b, '"');
11428e3e3a7aSWarner Losh }
11438e3e3a7aSWarner Losh 
11448e3e3a7aSWarner Losh 
11458c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
11468e3e3a7aSWarner Losh /*
11470495ed39SKyle Evans ** Serialize a floating-point number in such a way that it can be
11480495ed39SKyle Evans ** scanned back by Lua. Use hexadecimal format for "common" numbers
11490495ed39SKyle Evans ** (to preserve precision); inf, -inf, and NaN are handled separately.
11500495ed39SKyle Evans ** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.)
11518e3e3a7aSWarner Losh */
quotefloat(lua_State * L,char * buff,lua_Number n)11520495ed39SKyle Evans static int quotefloat (lua_State *L, char *buff, lua_Number n) {
11530495ed39SKyle Evans   const char *s;  /* for the fixed representations */
11540495ed39SKyle Evans   if (n == (lua_Number)HUGE_VAL)  /* inf? */
11550495ed39SKyle Evans     s = "1e9999";
11560495ed39SKyle Evans   else if (n == -(lua_Number)HUGE_VAL)  /* -inf? */
11570495ed39SKyle Evans     s = "-1e9999";
11580495ed39SKyle Evans   else if (n != n)  /* NaN? */
11590495ed39SKyle Evans     s = "(0/0)";
11600495ed39SKyle Evans   else {  /* format number as hexadecimal */
11610495ed39SKyle Evans     int  nb = lua_number2strx(L, buff, MAX_ITEM,
11620495ed39SKyle Evans                                  "%" LUA_NUMBER_FRMLEN "a", n);
11630495ed39SKyle Evans     /* ensures that 'buff' string uses a dot as the radix character */
11648e3e3a7aSWarner Losh     if (memchr(buff, '.', nb) == NULL) {  /* no dot? */
11658e3e3a7aSWarner Losh       char point = lua_getlocaledecpoint();  /* try locale point */
11668e3e3a7aSWarner Losh       char *ppoint = (char *)memchr(buff, point, nb);
11678e3e3a7aSWarner Losh       if (ppoint) *ppoint = '.';  /* change it to a dot */
11688e3e3a7aSWarner Losh     }
11690495ed39SKyle Evans     return nb;
11700495ed39SKyle Evans   }
11710495ed39SKyle Evans   /* for the fixed representations */
11720495ed39SKyle Evans   return l_sprintf(buff, MAX_ITEM, "%s", s);
11738e3e3a7aSWarner Losh }
117407d397d7SWarner Losh #endif
11758e3e3a7aSWarner Losh 
11768c784bb8SWarner Losh 
addliteral(lua_State * L,luaL_Buffer * b,int arg)11778e3e3a7aSWarner Losh static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
11788e3e3a7aSWarner Losh   switch (lua_type(L, arg)) {
11798e3e3a7aSWarner Losh     case LUA_TSTRING: {
11808e3e3a7aSWarner Losh       size_t len;
11818e3e3a7aSWarner Losh       const char *s = lua_tolstring(L, arg, &len);
11828e3e3a7aSWarner Losh       addquoted(b, s, len);
11838e3e3a7aSWarner Losh       break;
11848e3e3a7aSWarner Losh     }
11858e3e3a7aSWarner Losh     case LUA_TNUMBER: {
11868e3e3a7aSWarner Losh       char *buff = luaL_prepbuffsize(b, MAX_ITEM);
11878e3e3a7aSWarner Losh       int nb;
11888c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
11890495ed39SKyle Evans       if (!lua_isinteger(L, arg))  /* float? */
11900495ed39SKyle Evans         nb = quotefloat(L, buff, lua_tonumber(L, arg));
11918e3e3a7aSWarner Losh       else {  /* integers */
1192919cf86cSWarner Losh #else
1193919cf86cSWarner Losh       {
1194919cf86cSWarner Losh #endif
11958e3e3a7aSWarner Losh         lua_Integer n = lua_tointeger(L, arg);
11968e3e3a7aSWarner Losh         const char *format = (n == LUA_MININTEGER)  /* corner case? */
11970495ed39SKyle Evans                            ? "0x%" LUA_INTEGER_FRMLEN "x"  /* use hex */
11988e3e3a7aSWarner Losh                            : LUA_INTEGER_FMT;  /* else use default format */
11998e3e3a7aSWarner Losh         nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
12008e3e3a7aSWarner Losh       }
12018e3e3a7aSWarner Losh       luaL_addsize(b, nb);
12028e3e3a7aSWarner Losh       break;
12038e3e3a7aSWarner Losh     }
12048e3e3a7aSWarner Losh     case LUA_TNIL: case LUA_TBOOLEAN: {
12058e3e3a7aSWarner Losh       luaL_tolstring(L, arg, NULL);
12068e3e3a7aSWarner Losh       luaL_addvalue(b);
12078e3e3a7aSWarner Losh       break;
12088e3e3a7aSWarner Losh     }
12098e3e3a7aSWarner Losh     default: {
12108e3e3a7aSWarner Losh       luaL_argerror(L, arg, "value has no literal form");
12118e3e3a7aSWarner Losh     }
12128e3e3a7aSWarner Losh   }
12138e3e3a7aSWarner Losh }
12148e3e3a7aSWarner Losh 
12158e3e3a7aSWarner Losh 
12168c784bb8SWarner Losh static const char *get2digits (const char *s) {
12178c784bb8SWarner Losh   if (isdigit(uchar(*s))) {
12188c784bb8SWarner Losh     s++;
12198c784bb8SWarner Losh     if (isdigit(uchar(*s))) s++;  /* (2 digits at most) */
12208e3e3a7aSWarner Losh   }
12218c784bb8SWarner Losh   return s;
12228c784bb8SWarner Losh }
12238c784bb8SWarner Losh 
12248c784bb8SWarner Losh 
12258c784bb8SWarner Losh /*
12268c784bb8SWarner Losh ** Check whether a conversion specification is valid. When called,
12278c784bb8SWarner Losh ** first character in 'form' must be '%' and last character must
12288c784bb8SWarner Losh ** be a valid conversion specifier. 'flags' are the accepted flags;
12298c784bb8SWarner Losh ** 'precision' signals whether to accept a precision.
12308c784bb8SWarner Losh */
12318c784bb8SWarner Losh static void checkformat (lua_State *L, const char *form, const char *flags,
12328c784bb8SWarner Losh                                        int precision) {
12338c784bb8SWarner Losh   const char *spec = form + 1;  /* skip '%' */
12348c784bb8SWarner Losh   spec += strspn(spec, flags);  /* skip flags */
12358c784bb8SWarner Losh   if (*spec != '0') {  /* a width cannot start with '0' */
12368c784bb8SWarner Losh     spec = get2digits(spec);  /* skip width */
12378c784bb8SWarner Losh     if (*spec == '.' && precision) {
12388c784bb8SWarner Losh       spec++;
12398c784bb8SWarner Losh       spec = get2digits(spec);  /* skip precision */
12408c784bb8SWarner Losh     }
12418c784bb8SWarner Losh   }
12428c784bb8SWarner Losh   if (!isalpha(uchar(*spec)))  /* did not go to the end? */
12438c784bb8SWarner Losh     luaL_error(L, "invalid conversion specification: '%s'", form);
12448c784bb8SWarner Losh }
12458c784bb8SWarner Losh 
12468c784bb8SWarner Losh 
12478c784bb8SWarner Losh /*
12488c784bb8SWarner Losh ** Get a conversion specification and copy it to 'form'.
12498c784bb8SWarner Losh ** Return the address of its last character.
12508c784bb8SWarner Losh */
12518c784bb8SWarner Losh static const char *getformat (lua_State *L, const char *strfrmt,
12528c784bb8SWarner Losh                                             char *form) {
12538c784bb8SWarner Losh   /* spans flags, width, and precision ('0' is included as a flag) */
12548c784bb8SWarner Losh   size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789.");
12558c784bb8SWarner Losh   len++;  /* adds following character (should be the specifier) */
12568c784bb8SWarner Losh   /* still needs space for '%', '\0', plus a length modifier */
12578c784bb8SWarner Losh   if (len >= MAX_FORMAT - 10)
12588c784bb8SWarner Losh     luaL_error(L, "invalid format (too long)");
12598e3e3a7aSWarner Losh   *(form++) = '%';
12608c784bb8SWarner Losh   memcpy(form, strfrmt, len * sizeof(char));
12618c784bb8SWarner Losh   *(form + len) = '\0';
12628c784bb8SWarner Losh   return strfrmt + len - 1;
12638e3e3a7aSWarner Losh }
12648e3e3a7aSWarner Losh 
12658e3e3a7aSWarner Losh 
12668e3e3a7aSWarner Losh /*
12678e3e3a7aSWarner Losh ** add length modifier into formats
12688e3e3a7aSWarner Losh */
12698e3e3a7aSWarner Losh static void addlenmod (char *form, const char *lenmod) {
12708e3e3a7aSWarner Losh   size_t l = strlen(form);
12718e3e3a7aSWarner Losh   size_t lm = strlen(lenmod);
12728e3e3a7aSWarner Losh   char spec = form[l - 1];
12738e3e3a7aSWarner Losh   strcpy(form + l - 1, lenmod);
12748e3e3a7aSWarner Losh   form[l + lm - 1] = spec;
12758e3e3a7aSWarner Losh   form[l + lm] = '\0';
12768e3e3a7aSWarner Losh }
12778e3e3a7aSWarner Losh 
12788e3e3a7aSWarner Losh 
12798e3e3a7aSWarner Losh static int str_format (lua_State *L) {
12808e3e3a7aSWarner Losh   int top = lua_gettop(L);
12818e3e3a7aSWarner Losh   int arg = 1;
12828e3e3a7aSWarner Losh   size_t sfl;
12838e3e3a7aSWarner Losh   const char *strfrmt = luaL_checklstring(L, arg, &sfl);
12848e3e3a7aSWarner Losh   const char *strfrmt_end = strfrmt+sfl;
12858c784bb8SWarner Losh   const char *flags;
12868e3e3a7aSWarner Losh   luaL_Buffer b;
12878e3e3a7aSWarner Losh   luaL_buffinit(L, &b);
12888e3e3a7aSWarner Losh   while (strfrmt < strfrmt_end) {
12898e3e3a7aSWarner Losh     if (*strfrmt != L_ESC)
12908e3e3a7aSWarner Losh       luaL_addchar(&b, *strfrmt++);
12918e3e3a7aSWarner Losh     else if (*++strfrmt == L_ESC)
12928e3e3a7aSWarner Losh       luaL_addchar(&b, *strfrmt++);  /* %% */
12938e3e3a7aSWarner Losh     else { /* format item */
12948e3e3a7aSWarner Losh       char form[MAX_FORMAT];  /* to store the format ('%...') */
12958c784bb8SWarner Losh       int maxitem = MAX_ITEM;  /* maximum length for the result */
12968c784bb8SWarner Losh       char *buff = luaL_prepbuffsize(&b, maxitem);  /* to put result */
12978c784bb8SWarner Losh       int nb = 0;  /* number of bytes in result */
12988e3e3a7aSWarner Losh       if (++arg > top)
12990495ed39SKyle Evans         return luaL_argerror(L, arg, "no value");
13008c784bb8SWarner Losh       strfrmt = getformat(L, strfrmt, form);
13018e3e3a7aSWarner Losh       switch (*strfrmt++) {
13028e3e3a7aSWarner Losh         case 'c': {
13038c784bb8SWarner Losh           checkformat(L, form, L_FMTFLAGSC, 0);
13040495ed39SKyle Evans           nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg));
13058e3e3a7aSWarner Losh           break;
13068e3e3a7aSWarner Losh         }
13078e3e3a7aSWarner Losh         case 'd': case 'i':
13088c784bb8SWarner Losh           flags = L_FMTFLAGSI;
13098c784bb8SWarner Losh           goto intcase;
13108c784bb8SWarner Losh         case 'u':
13118c784bb8SWarner Losh           flags = L_FMTFLAGSU;
13128c784bb8SWarner Losh           goto intcase;
13138c784bb8SWarner Losh         case 'o': case 'x': case 'X':
13148c784bb8SWarner Losh           flags = L_FMTFLAGSX;
13158c784bb8SWarner Losh          intcase: {
13168e3e3a7aSWarner Losh           lua_Integer n = luaL_checkinteger(L, arg);
13178c784bb8SWarner Losh           checkformat(L, form, flags, 1);
13188e3e3a7aSWarner Losh           addlenmod(form, LUA_INTEGER_FRMLEN);
13190495ed39SKyle Evans           nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n);
13208e3e3a7aSWarner Losh           break;
13218e3e3a7aSWarner Losh         }
13228e3e3a7aSWarner Losh         case 'a': case 'A':
13238c784bb8SWarner Losh           checkformat(L, form, L_FMTFLAGSF, 1);
13248e3e3a7aSWarner Losh           addlenmod(form, LUA_NUMBER_FRMLEN);
13250495ed39SKyle Evans           nb = lua_number2strx(L, buff, maxitem, form,
13268e3e3a7aSWarner Losh                                   luaL_checknumber(L, arg));
13278e3e3a7aSWarner Losh           break;
13288c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
13290495ed39SKyle Evans         case 'f':
13300495ed39SKyle Evans           maxitem = MAX_ITEMF;  /* extra space for '%f' */
13310495ed39SKyle Evans           buff = luaL_prepbuffsize(&b, maxitem);
13320495ed39SKyle Evans           /* FALLTHROUGH */
13330495ed39SKyle Evans #endif
13340495ed39SKyle Evans         case 'e': case 'E': case 'g': case 'G': {
13358e3e3a7aSWarner Losh           lua_Number n = luaL_checknumber(L, arg);
13368c784bb8SWarner Losh           checkformat(L, form, L_FMTFLAGSF, 1);
13378e3e3a7aSWarner Losh           addlenmod(form, LUA_NUMBER_FRMLEN);
13380495ed39SKyle Evans           nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
13390495ed39SKyle Evans           break;
13400495ed39SKyle Evans         }
13410495ed39SKyle Evans         case 'p': {
13420495ed39SKyle Evans           const void *p = lua_topointer(L, arg);
13438c784bb8SWarner Losh           checkformat(L, form, L_FMTFLAGSC, 0);
13440495ed39SKyle Evans           if (p == NULL) {  /* avoid calling 'printf' with argument NULL */
13450495ed39SKyle Evans             p = "(null)";  /* result */
13460495ed39SKyle Evans             form[strlen(form) - 1] = 's';  /* format it as a string */
13470495ed39SKyle Evans           }
13480495ed39SKyle Evans           nb = l_sprintf(buff, maxitem, form, p);
13498e3e3a7aSWarner Losh           break;
13508e3e3a7aSWarner Losh         }
13518e3e3a7aSWarner Losh         case 'q': {
13520495ed39SKyle Evans           if (form[2] != '\0')  /* modifiers? */
13530495ed39SKyle Evans             return luaL_error(L, "specifier '%%q' cannot have modifiers");
13548e3e3a7aSWarner Losh           addliteral(L, &b, arg);
13558e3e3a7aSWarner Losh           break;
13568e3e3a7aSWarner Losh         }
13578e3e3a7aSWarner Losh         case 's': {
13588e3e3a7aSWarner Losh           size_t l;
13598e3e3a7aSWarner Losh           const char *s = luaL_tolstring(L, arg, &l);
13608e3e3a7aSWarner Losh           if (form[2] == '\0')  /* no modifiers? */
13618e3e3a7aSWarner Losh             luaL_addvalue(&b);  /* keep entire string */
13628e3e3a7aSWarner Losh           else {
13638e3e3a7aSWarner Losh             luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
13648c784bb8SWarner Losh             checkformat(L, form, L_FMTFLAGSC, 1);
13658c784bb8SWarner Losh             if (strchr(form, '.') == NULL && l >= 100) {
13668e3e3a7aSWarner Losh               /* no precision and string is too long to be formatted */
13678e3e3a7aSWarner Losh               luaL_addvalue(&b);  /* keep entire string */
13688e3e3a7aSWarner Losh             }
13698e3e3a7aSWarner Losh             else {  /* format the string into 'buff' */
13700495ed39SKyle Evans               nb = l_sprintf(buff, maxitem, form, s);
13718e3e3a7aSWarner Losh               lua_pop(L, 1);  /* remove result from 'luaL_tolstring' */
13728e3e3a7aSWarner Losh             }
13738e3e3a7aSWarner Losh           }
13748e3e3a7aSWarner Losh           break;
13758e3e3a7aSWarner Losh         }
13768e3e3a7aSWarner Losh         default: {  /* also treat cases 'pnLlh' */
13770495ed39SKyle Evans           return luaL_error(L, "invalid conversion '%s' to 'format'", form);
13788e3e3a7aSWarner Losh         }
13798e3e3a7aSWarner Losh       }
13800495ed39SKyle Evans       lua_assert(nb < maxitem);
13818e3e3a7aSWarner Losh       luaL_addsize(&b, nb);
13828e3e3a7aSWarner Losh     }
13838e3e3a7aSWarner Losh   }
13848e3e3a7aSWarner Losh   luaL_pushresult(&b);
13858e3e3a7aSWarner Losh   return 1;
13868e3e3a7aSWarner Losh }
13878e3e3a7aSWarner Losh 
13888e3e3a7aSWarner Losh /* }====================================================== */
13898e3e3a7aSWarner Losh 
13908e3e3a7aSWarner Losh 
13918e3e3a7aSWarner Losh /*
13928e3e3a7aSWarner Losh ** {======================================================
13938e3e3a7aSWarner Losh ** PACK/UNPACK
13948e3e3a7aSWarner Losh ** =======================================================
13958e3e3a7aSWarner Losh */
13968e3e3a7aSWarner Losh 
13978e3e3a7aSWarner Losh 
13988e3e3a7aSWarner Losh /* value used for padding */
13998e3e3a7aSWarner Losh #if !defined(LUAL_PACKPADBYTE)
14008e3e3a7aSWarner Losh #define LUAL_PACKPADBYTE		0x00
14018e3e3a7aSWarner Losh #endif
14028e3e3a7aSWarner Losh 
14038e3e3a7aSWarner Losh /* maximum size for the binary representation of an integer */
14048e3e3a7aSWarner Losh #define MAXINTSIZE	16
14058e3e3a7aSWarner Losh 
14068e3e3a7aSWarner Losh /* number of bits in a character */
14078e3e3a7aSWarner Losh #define NB	CHAR_BIT
14088e3e3a7aSWarner Losh 
14098e3e3a7aSWarner Losh /* mask for one character (NB 1's) */
14108e3e3a7aSWarner Losh #define MC	((1 << NB) - 1)
14118e3e3a7aSWarner Losh 
14128e3e3a7aSWarner Losh /* size of a lua_Integer */
14138e3e3a7aSWarner Losh #define SZINT	((int)sizeof(lua_Integer))
14148e3e3a7aSWarner Losh 
14158e3e3a7aSWarner Losh 
14168e3e3a7aSWarner Losh /* dummy union to get native endianness */
14178e3e3a7aSWarner Losh static const union {
14188e3e3a7aSWarner Losh   int dummy;
14198e3e3a7aSWarner Losh   char little;  /* true iff machine is little endian */
14208e3e3a7aSWarner Losh } nativeendian = {1};
14218e3e3a7aSWarner Losh 
14228e3e3a7aSWarner Losh 
14238e3e3a7aSWarner Losh /*
14248e3e3a7aSWarner Losh ** information to pack/unpack stuff
14258e3e3a7aSWarner Losh */
14268e3e3a7aSWarner Losh typedef struct Header {
14278e3e3a7aSWarner Losh   lua_State *L;
14288e3e3a7aSWarner Losh   int islittle;
14298e3e3a7aSWarner Losh   int maxalign;
14308e3e3a7aSWarner Losh } Header;
14318e3e3a7aSWarner Losh 
14328e3e3a7aSWarner Losh 
14338e3e3a7aSWarner Losh /*
14348e3e3a7aSWarner Losh ** options for pack/unpack
14358e3e3a7aSWarner Losh */
14368e3e3a7aSWarner Losh typedef enum KOption {
14378e3e3a7aSWarner Losh   Kint,		/* signed integers */
14388e3e3a7aSWarner Losh   Kuint,	/* unsigned integers */
14398c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
14408c784bb8SWarner Losh   Kfloat,	/* single-precision floating-point numbers */
14418c784bb8SWarner Losh   Kdouble,	/* double-precision floating-point numbers */
14428c784bb8SWarner Losh #endif
14438c784bb8SWarner Losh   Knumber,	/* Lua "native" floating-point numbers */
14448e3e3a7aSWarner Losh   Kchar,	/* fixed-length strings */
14458e3e3a7aSWarner Losh   Kstring,	/* strings with prefixed length */
14468e3e3a7aSWarner Losh   Kzstr,	/* zero-terminated strings */
14478e3e3a7aSWarner Losh   Kpadding,	/* padding */
14488e3e3a7aSWarner Losh   Kpaddalign,	/* padding for alignment */
14498e3e3a7aSWarner Losh   Knop		/* no-op (configuration or spaces) */
14508e3e3a7aSWarner Losh } KOption;
14518e3e3a7aSWarner Losh 
14528e3e3a7aSWarner Losh 
14538e3e3a7aSWarner Losh /*
14548e3e3a7aSWarner Losh ** Read an integer numeral from string 'fmt' or return 'df' if
14558e3e3a7aSWarner Losh ** there is no numeral
14568e3e3a7aSWarner Losh */
14578e3e3a7aSWarner Losh static int digit (int c) { return '0' <= c && c <= '9'; }
14588e3e3a7aSWarner Losh 
14598e3e3a7aSWarner Losh static int getnum (const char **fmt, int df) {
14608e3e3a7aSWarner Losh   if (!digit(**fmt))  /* no number? */
14618e3e3a7aSWarner Losh     return df;  /* return default value */
14628e3e3a7aSWarner Losh   else {
14638e3e3a7aSWarner Losh     int a = 0;
14648e3e3a7aSWarner Losh     do {
14658e3e3a7aSWarner Losh       a = a*10 + (*((*fmt)++) - '0');
14668e3e3a7aSWarner Losh     } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10);
14678e3e3a7aSWarner Losh     return a;
14688e3e3a7aSWarner Losh   }
14698e3e3a7aSWarner Losh }
14708e3e3a7aSWarner Losh 
14718e3e3a7aSWarner Losh 
14728e3e3a7aSWarner Losh /*
14738e3e3a7aSWarner Losh ** Read an integer numeral and raises an error if it is larger
14748e3e3a7aSWarner Losh ** than the maximum size for integers.
14758e3e3a7aSWarner Losh */
14768e3e3a7aSWarner Losh static int getnumlimit (Header *h, const char **fmt, int df) {
14778e3e3a7aSWarner Losh   int sz = getnum(fmt, df);
14788c784bb8SWarner Losh   if (l_unlikely(sz > MAXINTSIZE || sz <= 0))
1479e112e9d2SKyle Evans     return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
14808e3e3a7aSWarner Losh                             sz, MAXINTSIZE);
14818e3e3a7aSWarner Losh   return sz;
14828e3e3a7aSWarner Losh }
14838e3e3a7aSWarner Losh 
14848e3e3a7aSWarner Losh 
14858e3e3a7aSWarner Losh /*
14868e3e3a7aSWarner Losh ** Initialize Header
14878e3e3a7aSWarner Losh */
14888e3e3a7aSWarner Losh static void initheader (lua_State *L, Header *h) {
14898e3e3a7aSWarner Losh   h->L = L;
14908e3e3a7aSWarner Losh   h->islittle = nativeendian.little;
14918e3e3a7aSWarner Losh   h->maxalign = 1;
14928e3e3a7aSWarner Losh }
14938e3e3a7aSWarner Losh 
14948e3e3a7aSWarner Losh 
14958e3e3a7aSWarner Losh /*
14968e3e3a7aSWarner Losh ** Read and classify next option. 'size' is filled with option's size.
14978e3e3a7aSWarner Losh */
14988e3e3a7aSWarner Losh static KOption getoption (Header *h, const char **fmt, int *size) {
14998c784bb8SWarner Losh   /* dummy structure to get native alignment requirements */
15008c784bb8SWarner Losh   struct cD { char c; union { LUAI_MAXALIGN; } u; };
15018e3e3a7aSWarner Losh   int opt = *((*fmt)++);
15028e3e3a7aSWarner Losh   *size = 0;  /* default */
15038e3e3a7aSWarner Losh   switch (opt) {
15048e3e3a7aSWarner Losh     case 'b': *size = sizeof(char); return Kint;
15058e3e3a7aSWarner Losh     case 'B': *size = sizeof(char); return Kuint;
15068e3e3a7aSWarner Losh     case 'h': *size = sizeof(short); return Kint;
15078e3e3a7aSWarner Losh     case 'H': *size = sizeof(short); return Kuint;
15088e3e3a7aSWarner Losh     case 'l': *size = sizeof(long); return Kint;
15098e3e3a7aSWarner Losh     case 'L': *size = sizeof(long); return Kuint;
15108e3e3a7aSWarner Losh     case 'j': *size = sizeof(lua_Integer); return Kint;
15118e3e3a7aSWarner Losh     case 'J': *size = sizeof(lua_Integer); return Kuint;
15128e3e3a7aSWarner Losh     case 'T': *size = sizeof(size_t); return Kuint;
15138c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
15148e3e3a7aSWarner Losh     case 'f': *size = sizeof(float); return Kfloat;
15158c784bb8SWarner Losh     case 'd': *size = sizeof(double); return Kdouble;
151607c17b2bSWarner Losh #endif
15178c784bb8SWarner Losh     case 'n': *size = sizeof(lua_Number); return Knumber;
15188e3e3a7aSWarner Losh     case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
15198e3e3a7aSWarner Losh     case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
15208e3e3a7aSWarner Losh     case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
15218e3e3a7aSWarner Losh     case 'c':
15228e3e3a7aSWarner Losh       *size = getnum(fmt, -1);
15238c784bb8SWarner Losh       if (l_unlikely(*size == -1))
15248e3e3a7aSWarner Losh         luaL_error(h->L, "missing size for format option 'c'");
15258e3e3a7aSWarner Losh       return Kchar;
15268e3e3a7aSWarner Losh     case 'z': return Kzstr;
15278e3e3a7aSWarner Losh     case 'x': *size = 1; return Kpadding;
15288e3e3a7aSWarner Losh     case 'X': return Kpaddalign;
15298e3e3a7aSWarner Losh     case ' ': break;
15308e3e3a7aSWarner Losh     case '<': h->islittle = 1; break;
15318e3e3a7aSWarner Losh     case '>': h->islittle = 0; break;
15328e3e3a7aSWarner Losh     case '=': h->islittle = nativeendian.little; break;
15338c784bb8SWarner Losh     case '!': {
15348c784bb8SWarner Losh       const int maxalign = offsetof(struct cD, u);
15358c784bb8SWarner Losh       h->maxalign = getnumlimit(h, fmt, maxalign);
15368c784bb8SWarner Losh       break;
15378c784bb8SWarner Losh     }
15388e3e3a7aSWarner Losh     default: luaL_error(h->L, "invalid format option '%c'", opt);
15398e3e3a7aSWarner Losh   }
15408e3e3a7aSWarner Losh   return Knop;
15418e3e3a7aSWarner Losh }
15428e3e3a7aSWarner Losh 
15438e3e3a7aSWarner Losh 
15448e3e3a7aSWarner Losh /*
15458e3e3a7aSWarner Losh ** Read, classify, and fill other details about the next option.
15468e3e3a7aSWarner Losh ** 'psize' is filled with option's size, 'notoalign' with its
15478e3e3a7aSWarner Losh ** alignment requirements.
15488e3e3a7aSWarner Losh ** Local variable 'size' gets the size to be aligned. (Kpadal option
15498e3e3a7aSWarner Losh ** always gets its full alignment, other options are limited by
15508e3e3a7aSWarner Losh ** the maximum alignment ('maxalign'). Kchar option needs no alignment
15518e3e3a7aSWarner Losh ** despite its size.
15528e3e3a7aSWarner Losh */
15538e3e3a7aSWarner Losh static KOption getdetails (Header *h, size_t totalsize,
15548e3e3a7aSWarner Losh                            const char **fmt, int *psize, int *ntoalign) {
15558e3e3a7aSWarner Losh   KOption opt = getoption(h, fmt, psize);
15568e3e3a7aSWarner Losh   int align = *psize;  /* usually, alignment follows size */
15578e3e3a7aSWarner Losh   if (opt == Kpaddalign) {  /* 'X' gets alignment from following option */
15588e3e3a7aSWarner Losh     if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
15598e3e3a7aSWarner Losh       luaL_argerror(h->L, 1, "invalid next option for option 'X'");
15608e3e3a7aSWarner Losh   }
15618e3e3a7aSWarner Losh   if (align <= 1 || opt == Kchar)  /* need no alignment? */
15628e3e3a7aSWarner Losh     *ntoalign = 0;
15638e3e3a7aSWarner Losh   else {
15648e3e3a7aSWarner Losh     if (align > h->maxalign)  /* enforce maximum alignment */
15658e3e3a7aSWarner Losh       align = h->maxalign;
15668c784bb8SWarner Losh     if (l_unlikely((align & (align - 1)) != 0))  /* not a power of 2? */
15678e3e3a7aSWarner Losh       luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
15688e3e3a7aSWarner Losh     *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
15698e3e3a7aSWarner Losh   }
15708e3e3a7aSWarner Losh   return opt;
15718e3e3a7aSWarner Losh }
15728e3e3a7aSWarner Losh 
15738e3e3a7aSWarner Losh 
15748e3e3a7aSWarner Losh /*
15758e3e3a7aSWarner Losh ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
15768e3e3a7aSWarner Losh ** The final 'if' handles the case when 'size' is larger than
15778e3e3a7aSWarner Losh ** the size of a Lua integer, correcting the extra sign-extension
15788e3e3a7aSWarner Losh ** bytes if necessary (by default they would be zeros).
15798e3e3a7aSWarner Losh */
15808e3e3a7aSWarner Losh static void packint (luaL_Buffer *b, lua_Unsigned n,
15818e3e3a7aSWarner Losh                      int islittle, int size, int neg) {
15828e3e3a7aSWarner Losh   char *buff = luaL_prepbuffsize(b, size);
15838e3e3a7aSWarner Losh   int i;
15848e3e3a7aSWarner Losh   buff[islittle ? 0 : size - 1] = (char)(n & MC);  /* first byte */
15858e3e3a7aSWarner Losh   for (i = 1; i < size; i++) {
15868e3e3a7aSWarner Losh     n >>= NB;
15878e3e3a7aSWarner Losh     buff[islittle ? i : size - 1 - i] = (char)(n & MC);
15888e3e3a7aSWarner Losh   }
15898e3e3a7aSWarner Losh   if (neg && size > SZINT) {  /* negative number need sign extension? */
15908e3e3a7aSWarner Losh     for (i = SZINT; i < size; i++)  /* correct extra bytes */
15918e3e3a7aSWarner Losh       buff[islittle ? i : size - 1 - i] = (char)MC;
15928e3e3a7aSWarner Losh   }
15938e3e3a7aSWarner Losh   luaL_addsize(b, size);  /* add result to buffer */
15948e3e3a7aSWarner Losh }
15958e3e3a7aSWarner Losh 
15968e3e3a7aSWarner Losh 
15978e3e3a7aSWarner Losh /*
15988e3e3a7aSWarner Losh ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
15998e3e3a7aSWarner Losh ** given 'islittle' is different from native endianness.
16008e3e3a7aSWarner Losh */
16010495ed39SKyle Evans static void copywithendian (char *dest, const char *src,
16028e3e3a7aSWarner Losh                             int size, int islittle) {
16030495ed39SKyle Evans   if (islittle == nativeendian.little)
16040495ed39SKyle Evans     memcpy(dest, src, size);
16058e3e3a7aSWarner Losh   else {
16068e3e3a7aSWarner Losh     dest += size - 1;
16078e3e3a7aSWarner Losh     while (size-- != 0)
16088e3e3a7aSWarner Losh       *(dest--) = *(src++);
16098e3e3a7aSWarner Losh   }
16108e3e3a7aSWarner Losh }
16118e3e3a7aSWarner Losh 
16128e3e3a7aSWarner Losh 
16138e3e3a7aSWarner Losh static int str_pack (lua_State *L) {
16148e3e3a7aSWarner Losh   luaL_Buffer b;
16158e3e3a7aSWarner Losh   Header h;
16168e3e3a7aSWarner Losh   const char *fmt = luaL_checkstring(L, 1);  /* format string */
16178e3e3a7aSWarner Losh   int arg = 1;  /* current argument to pack */
16188e3e3a7aSWarner Losh   size_t totalsize = 0;  /* accumulate total size of result */
16198e3e3a7aSWarner Losh   initheader(L, &h);
16208e3e3a7aSWarner Losh   lua_pushnil(L);  /* mark to separate arguments from string buffer */
16218e3e3a7aSWarner Losh   luaL_buffinit(L, &b);
16228e3e3a7aSWarner Losh   while (*fmt != '\0') {
16238e3e3a7aSWarner Losh     int size, ntoalign;
16248e3e3a7aSWarner Losh     KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
16258e3e3a7aSWarner Losh     totalsize += ntoalign + size;
16268e3e3a7aSWarner Losh     while (ntoalign-- > 0)
16278e3e3a7aSWarner Losh      luaL_addchar(&b, LUAL_PACKPADBYTE);  /* fill alignment */
16288e3e3a7aSWarner Losh     arg++;
16298e3e3a7aSWarner Losh     switch (opt) {
16308e3e3a7aSWarner Losh       case Kint: {  /* signed integers */
16318e3e3a7aSWarner Losh         lua_Integer n = luaL_checkinteger(L, arg);
16328e3e3a7aSWarner Losh         if (size < SZINT) {  /* need overflow check? */
16338e3e3a7aSWarner Losh           lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
16348e3e3a7aSWarner Losh           luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
16358e3e3a7aSWarner Losh         }
16368e3e3a7aSWarner Losh         packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
16378e3e3a7aSWarner Losh         break;
16388e3e3a7aSWarner Losh       }
16398e3e3a7aSWarner Losh       case Kuint: {  /* unsigned integers */
16408e3e3a7aSWarner Losh         lua_Integer n = luaL_checkinteger(L, arg);
16418e3e3a7aSWarner Losh         if (size < SZINT)  /* need overflow check? */
16428e3e3a7aSWarner Losh           luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
16438e3e3a7aSWarner Losh                            arg, "unsigned overflow");
16448e3e3a7aSWarner Losh         packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
16458e3e3a7aSWarner Losh         break;
16468e3e3a7aSWarner Losh       }
16478c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
16488c784bb8SWarner Losh       case Kfloat: {  /* C float */
16498c784bb8SWarner Losh         float f = (float)luaL_checknumber(L, arg);  /* get argument */
16508c784bb8SWarner Losh         char *buff = luaL_prepbuffsize(&b, sizeof(f));
16518c784bb8SWarner Losh         /* move 'f' to final result, correcting endianness if needed */
16528c784bb8SWarner Losh         copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
16538e3e3a7aSWarner Losh         luaL_addsize(&b, size);
16548e3e3a7aSWarner Losh         break;
16558e3e3a7aSWarner Losh       }
16568c784bb8SWarner Losh #endif
16578c784bb8SWarner Losh       case Knumber: {  /* Lua float */
16588c784bb8SWarner Losh         lua_Number f = luaL_checknumber(L, arg);  /* get argument */
16598c784bb8SWarner Losh         char *buff = luaL_prepbuffsize(&b, sizeof(f));
16608c784bb8SWarner Losh         /* move 'f' to final result, correcting endianness if needed */
16618c784bb8SWarner Losh         copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
16628c784bb8SWarner Losh         luaL_addsize(&b, size);
16638c784bb8SWarner Losh         break;
16648c784bb8SWarner Losh       }
16658c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
16668c784bb8SWarner Losh       case Kdouble: {  /* C double */
16678c784bb8SWarner Losh         double f = (double)luaL_checknumber(L, arg);  /* get argument */
16688c784bb8SWarner Losh         char *buff = luaL_prepbuffsize(&b, sizeof(f));
16698c784bb8SWarner Losh         /* move 'f' to final result, correcting endianness if needed */
16708c784bb8SWarner Losh         copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
16718c784bb8SWarner Losh         luaL_addsize(&b, size);
16728c784bb8SWarner Losh         break;
16738c784bb8SWarner Losh       }
16748c784bb8SWarner Losh #endif
16758e3e3a7aSWarner Losh       case Kchar: {  /* fixed-size string */
16768e3e3a7aSWarner Losh         size_t len;
16778e3e3a7aSWarner Losh         const char *s = luaL_checklstring(L, arg, &len);
16788e3e3a7aSWarner Losh         luaL_argcheck(L, len <= (size_t)size, arg,
16798e3e3a7aSWarner Losh                          "string longer than given size");
16808e3e3a7aSWarner Losh         luaL_addlstring(&b, s, len);  /* add string */
16818e3e3a7aSWarner Losh         while (len++ < (size_t)size)  /* pad extra space */
16828e3e3a7aSWarner Losh           luaL_addchar(&b, LUAL_PACKPADBYTE);
16838e3e3a7aSWarner Losh         break;
16848e3e3a7aSWarner Losh       }
16858e3e3a7aSWarner Losh       case Kstring: {  /* strings with length count */
16868e3e3a7aSWarner Losh         size_t len;
16878e3e3a7aSWarner Losh         const char *s = luaL_checklstring(L, arg, &len);
16888e3e3a7aSWarner Losh         luaL_argcheck(L, size >= (int)sizeof(size_t) ||
16898e3e3a7aSWarner Losh                          len < ((size_t)1 << (size * NB)),
16908e3e3a7aSWarner Losh                          arg, "string length does not fit in given size");
16918e3e3a7aSWarner Losh         packint(&b, (lua_Unsigned)len, h.islittle, size, 0);  /* pack length */
16928e3e3a7aSWarner Losh         luaL_addlstring(&b, s, len);
16938e3e3a7aSWarner Losh         totalsize += len;
16948e3e3a7aSWarner Losh         break;
16958e3e3a7aSWarner Losh       }
16968e3e3a7aSWarner Losh       case Kzstr: {  /* zero-terminated string */
16978e3e3a7aSWarner Losh         size_t len;
16988e3e3a7aSWarner Losh         const char *s = luaL_checklstring(L, arg, &len);
16998e3e3a7aSWarner Losh         luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
17008e3e3a7aSWarner Losh         luaL_addlstring(&b, s, len);
17018e3e3a7aSWarner Losh         luaL_addchar(&b, '\0');  /* add zero at the end */
17028e3e3a7aSWarner Losh         totalsize += len + 1;
17038e3e3a7aSWarner Losh         break;
17048e3e3a7aSWarner Losh       }
17058e3e3a7aSWarner Losh       case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE);  /* FALLTHROUGH */
17068e3e3a7aSWarner Losh       case Kpaddalign: case Knop:
17078e3e3a7aSWarner Losh         arg--;  /* undo increment */
17088e3e3a7aSWarner Losh         break;
17098e3e3a7aSWarner Losh     }
17108e3e3a7aSWarner Losh   }
17118e3e3a7aSWarner Losh   luaL_pushresult(&b);
17128e3e3a7aSWarner Losh   return 1;
17138e3e3a7aSWarner Losh }
17148e3e3a7aSWarner Losh 
17158e3e3a7aSWarner Losh 
17168e3e3a7aSWarner Losh static int str_packsize (lua_State *L) {
17178e3e3a7aSWarner Losh   Header h;
17188e3e3a7aSWarner Losh   const char *fmt = luaL_checkstring(L, 1);  /* format string */
17198e3e3a7aSWarner Losh   size_t totalsize = 0;  /* accumulate total size of result */
17208e3e3a7aSWarner Losh   initheader(L, &h);
17218e3e3a7aSWarner Losh   while (*fmt != '\0') {
17228e3e3a7aSWarner Losh     int size, ntoalign;
17238e3e3a7aSWarner Losh     KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
17240495ed39SKyle Evans     luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
17250495ed39SKyle Evans                      "variable-length format");
17268e3e3a7aSWarner Losh     size += ntoalign;  /* total space used by option */
17278e3e3a7aSWarner Losh     luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
17288e3e3a7aSWarner Losh                      "format result too large");
17298e3e3a7aSWarner Losh     totalsize += size;
17308e3e3a7aSWarner Losh   }
17318e3e3a7aSWarner Losh   lua_pushinteger(L, (lua_Integer)totalsize);
17328e3e3a7aSWarner Losh   return 1;
17338e3e3a7aSWarner Losh }
17348e3e3a7aSWarner Losh 
17358e3e3a7aSWarner Losh 
17368e3e3a7aSWarner Losh /*
17378e3e3a7aSWarner Losh ** Unpack an integer with 'size' bytes and 'islittle' endianness.
17388e3e3a7aSWarner Losh ** If size is smaller than the size of a Lua integer and integer
17398e3e3a7aSWarner Losh ** is signed, must do sign extension (propagating the sign to the
17408e3e3a7aSWarner Losh ** higher bits); if size is larger than the size of a Lua integer,
17418e3e3a7aSWarner Losh ** it must check the unread bytes to see whether they do not cause an
17428e3e3a7aSWarner Losh ** overflow.
17438e3e3a7aSWarner Losh */
17448e3e3a7aSWarner Losh static lua_Integer unpackint (lua_State *L, const char *str,
17458e3e3a7aSWarner Losh                               int islittle, int size, int issigned) {
17468e3e3a7aSWarner Losh   lua_Unsigned res = 0;
17478e3e3a7aSWarner Losh   int i;
17488e3e3a7aSWarner Losh   int limit = (size  <= SZINT) ? size : SZINT;
17498e3e3a7aSWarner Losh   for (i = limit - 1; i >= 0; i--) {
17508e3e3a7aSWarner Losh     res <<= NB;
17518e3e3a7aSWarner Losh     res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i];
17528e3e3a7aSWarner Losh   }
17538e3e3a7aSWarner Losh   if (size < SZINT) {  /* real size smaller than lua_Integer? */
17548e3e3a7aSWarner Losh     if (issigned) {  /* needs sign extension? */
17558e3e3a7aSWarner Losh       lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
17568e3e3a7aSWarner Losh       res = ((res ^ mask) - mask);  /* do sign extension */
17578e3e3a7aSWarner Losh     }
17588e3e3a7aSWarner Losh   }
17598e3e3a7aSWarner Losh   else if (size > SZINT) {  /* must check unread bytes */
17608e3e3a7aSWarner Losh     int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
17618e3e3a7aSWarner Losh     for (i = limit; i < size; i++) {
17628c784bb8SWarner Losh       if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask))
17638e3e3a7aSWarner Losh         luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
17648e3e3a7aSWarner Losh     }
17658e3e3a7aSWarner Losh   }
17668e3e3a7aSWarner Losh   return (lua_Integer)res;
17678e3e3a7aSWarner Losh }
17688e3e3a7aSWarner Losh 
17698e3e3a7aSWarner Losh 
17708e3e3a7aSWarner Losh static int str_unpack (lua_State *L) {
17718e3e3a7aSWarner Losh   Header h;
17728e3e3a7aSWarner Losh   const char *fmt = luaL_checkstring(L, 1);
17738e3e3a7aSWarner Losh   size_t ld;
17748e3e3a7aSWarner Losh   const char *data = luaL_checklstring(L, 2, &ld);
17750495ed39SKyle Evans   size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1;
17768e3e3a7aSWarner Losh   int n = 0;  /* number of results */
17778e3e3a7aSWarner Losh   luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
17788e3e3a7aSWarner Losh   initheader(L, &h);
17798e3e3a7aSWarner Losh   while (*fmt != '\0') {
17808e3e3a7aSWarner Losh     int size, ntoalign;
17818e3e3a7aSWarner Losh     KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
17820495ed39SKyle Evans     luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2,
17830495ed39SKyle Evans                     "data string too short");
17848e3e3a7aSWarner Losh     pos += ntoalign;  /* skip alignment */
17858e3e3a7aSWarner Losh     /* stack space for item + next position */
17868e3e3a7aSWarner Losh     luaL_checkstack(L, 2, "too many results");
17878e3e3a7aSWarner Losh     n++;
17888e3e3a7aSWarner Losh     switch (opt) {
17898e3e3a7aSWarner Losh       case Kint:
17908e3e3a7aSWarner Losh       case Kuint: {
17918e3e3a7aSWarner Losh         lua_Integer res = unpackint(L, data + pos, h.islittle, size,
17928e3e3a7aSWarner Losh                                        (opt == Kint));
17938e3e3a7aSWarner Losh         lua_pushinteger(L, res);
17948e3e3a7aSWarner Losh         break;
17958e3e3a7aSWarner Losh       }
17968c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
17978e3e3a7aSWarner Losh       case Kfloat: {
17988c784bb8SWarner Losh         float f;
17998c784bb8SWarner Losh         copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
18008c784bb8SWarner Losh         lua_pushnumber(L, (lua_Number)f);
18018e3e3a7aSWarner Losh         break;
18028e3e3a7aSWarner Losh       }
18038c784bb8SWarner Losh #endif
18048c784bb8SWarner Losh       case Knumber: {
18058c784bb8SWarner Losh         lua_Number f;
18068c784bb8SWarner Losh         copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
18078c784bb8SWarner Losh         lua_pushnumber(L, f);
18088c784bb8SWarner Losh         break;
18098c784bb8SWarner Losh       }
18108c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
18118c784bb8SWarner Losh       case Kdouble: {
18128c784bb8SWarner Losh         double f;
18138c784bb8SWarner Losh         copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
18148c784bb8SWarner Losh         lua_pushnumber(L, (lua_Number)f);
18158c784bb8SWarner Losh         break;
18168c784bb8SWarner Losh       }
18178c784bb8SWarner Losh #endif
18188e3e3a7aSWarner Losh       case Kchar: {
18198e3e3a7aSWarner Losh         lua_pushlstring(L, data + pos, size);
18208e3e3a7aSWarner Losh         break;
18218e3e3a7aSWarner Losh       }
18228e3e3a7aSWarner Losh       case Kstring: {
18238e3e3a7aSWarner Losh         size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
18240495ed39SKyle Evans         luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
18258e3e3a7aSWarner Losh         lua_pushlstring(L, data + pos + size, len);
18268e3e3a7aSWarner Losh         pos += len;  /* skip string */
18278e3e3a7aSWarner Losh         break;
18288e3e3a7aSWarner Losh       }
18298e3e3a7aSWarner Losh       case Kzstr: {
18300495ed39SKyle Evans         size_t len = strlen(data + pos);
18310495ed39SKyle Evans         luaL_argcheck(L, pos + len < ld, 2,
18320495ed39SKyle Evans                          "unfinished string for format 'z'");
18338e3e3a7aSWarner Losh         lua_pushlstring(L, data + pos, len);
18348e3e3a7aSWarner Losh         pos += len + 1;  /* skip string plus final '\0' */
18358e3e3a7aSWarner Losh         break;
18368e3e3a7aSWarner Losh       }
18378e3e3a7aSWarner Losh       case Kpaddalign: case Kpadding: case Knop:
18388e3e3a7aSWarner Losh         n--;  /* undo increment */
18398e3e3a7aSWarner Losh         break;
18408e3e3a7aSWarner Losh     }
18418e3e3a7aSWarner Losh     pos += size;
18428e3e3a7aSWarner Losh   }
18438e3e3a7aSWarner Losh   lua_pushinteger(L, pos + 1);  /* next position */
18448e3e3a7aSWarner Losh   return n + 1;
18458e3e3a7aSWarner Losh }
18468e3e3a7aSWarner Losh 
18478e3e3a7aSWarner Losh /* }====================================================== */
18488e3e3a7aSWarner Losh 
18498e3e3a7aSWarner Losh 
18508e3e3a7aSWarner Losh static const luaL_Reg strlib[] = {
18518e3e3a7aSWarner Losh   {"byte", str_byte},
18528e3e3a7aSWarner Losh   {"char", str_char},
18538e3e3a7aSWarner Losh   {"dump", str_dump},
18548e3e3a7aSWarner Losh   {"find", str_find},
18558e3e3a7aSWarner Losh   {"format", str_format},
18568e3e3a7aSWarner Losh   {"gmatch", gmatch},
18578e3e3a7aSWarner Losh   {"gsub", str_gsub},
18588e3e3a7aSWarner Losh   {"len", str_len},
18598e3e3a7aSWarner Losh   {"lower", str_lower},
18608e3e3a7aSWarner Losh   {"match", str_match},
18618e3e3a7aSWarner Losh   {"rep", str_rep},
18628e3e3a7aSWarner Losh   {"reverse", str_reverse},
18638e3e3a7aSWarner Losh   {"sub", str_sub},
18648e3e3a7aSWarner Losh   {"upper", str_upper},
18658e3e3a7aSWarner Losh   {"pack", str_pack},
18668e3e3a7aSWarner Losh   {"packsize", str_packsize},
18678e3e3a7aSWarner Losh   {"unpack", str_unpack},
18688e3e3a7aSWarner Losh   {NULL, NULL}
18698e3e3a7aSWarner Losh };
18708e3e3a7aSWarner Losh 
18718e3e3a7aSWarner Losh 
18728e3e3a7aSWarner Losh static void createmetatable (lua_State *L) {
18730495ed39SKyle Evans   /* table to be metatable for strings */
18740495ed39SKyle Evans   luaL_newlibtable(L, stringmetamethods);
18750495ed39SKyle Evans   luaL_setfuncs(L, stringmetamethods, 0);
18768e3e3a7aSWarner Losh   lua_pushliteral(L, "");  /* dummy string */
18778e3e3a7aSWarner Losh   lua_pushvalue(L, -2);  /* copy table */
18788e3e3a7aSWarner Losh   lua_setmetatable(L, -2);  /* set table as metatable for strings */
18798e3e3a7aSWarner Losh   lua_pop(L, 1);  /* pop dummy string */
18808e3e3a7aSWarner Losh   lua_pushvalue(L, -2);  /* get string library */
18818e3e3a7aSWarner Losh   lua_setfield(L, -2, "__index");  /* metatable.__index = string */
18828e3e3a7aSWarner Losh   lua_pop(L, 1);  /* pop metatable */
18838e3e3a7aSWarner Losh }
18848e3e3a7aSWarner Losh 
18858e3e3a7aSWarner Losh 
18868e3e3a7aSWarner Losh /*
18878e3e3a7aSWarner Losh ** Open string library
18888e3e3a7aSWarner Losh */
18898e3e3a7aSWarner Losh LUAMOD_API int luaopen_string (lua_State *L) {
18908e3e3a7aSWarner Losh   luaL_newlib(L, strlib);
18918e3e3a7aSWarner Losh   createmetatable(L);
18928e3e3a7aSWarner Losh   return 1;
18938e3e3a7aSWarner Losh }
18948e3e3a7aSWarner Losh 
1895