]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Lua/src/lstrlib.c
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / AppPkg / Applications / Lua / src / lstrlib.c
diff --git a/AppPkg/Applications/Lua/src/lstrlib.c b/AppPkg/Applications/Lua/src/lstrlib.c
deleted file mode 100644 (file)
index d0e66aa..0000000
+++ /dev/null
@@ -1,1019 +0,0 @@
-/*\r
-** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $\r
-** Standard library for string operations and pattern-matching\r
-** See Copyright Notice in lua.h\r
-*/\r
-\r
-\r
-#include <ctype.h>\r
-#include <stddef.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-#define lstrlib_c\r
-#define LUA_LIB\r
-\r
-#include "lua.h"\r
-\r
-#include "lauxlib.h"\r
-#include "lualib.h"\r
-\r
-\r
-/*\r
-** maximum number of captures that a pattern can do during\r
-** pattern-matching. This limit is arbitrary.\r
-*/\r
-#if !defined(LUA_MAXCAPTURES)\r
-#define LUA_MAXCAPTURES   32\r
-#endif\r
-\r
-\r
-/* macro to `unsign' a character */\r
-#define uchar(c)  ((unsigned char)(c))\r
-\r
-\r
-\r
-static int str_len (lua_State *L) {\r
-  size_t l;\r
-  luaL_checklstring(L, 1, &l);\r
-  lua_pushinteger(L, (lua_Integer)l);\r
-  return 1;\r
-}\r
-\r
-\r
-/* translate a relative string position: negative means back from end */\r
-static size_t posrelat (ptrdiff_t pos, size_t len) {\r
-  if (pos >= 0) return (size_t)pos;\r
-  else if (0u - (size_t)pos > len) return 0;\r
-  else return len - ((size_t)-pos) + 1;\r
-}\r
-\r
-\r
-static int str_sub (lua_State *L) {\r
-  size_t l;\r
-  const char *s = luaL_checklstring(L, 1, &l);\r
-  size_t start = posrelat(luaL_checkinteger(L, 2), l);\r
-  size_t end = posrelat(luaL_optinteger(L, 3, -1), l);\r
-  if (start < 1) start = 1;\r
-  if (end > l) end = l;\r
-  if (start <= end)\r
-    lua_pushlstring(L, s + start - 1, end - start + 1);\r
-  else lua_pushliteral(L, "");\r
-  return 1;\r
-}\r
-\r
-\r
-static int str_reverse (lua_State *L) {\r
-  size_t l, i;\r
-  luaL_Buffer b;\r
-  const char *s = luaL_checklstring(L, 1, &l);\r
-  char *p = luaL_buffinitsize(L, &b, l);\r
-  for (i = 0; i < l; i++)\r
-    p[i] = s[l - i - 1];\r
-  luaL_pushresultsize(&b, l);\r
-  return 1;\r
-}\r
-\r
-\r
-static int str_lower (lua_State *L) {\r
-  size_t l;\r
-  size_t i;\r
-  luaL_Buffer b;\r
-  const char *s = luaL_checklstring(L, 1, &l);\r
-  char *p = luaL_buffinitsize(L, &b, l);\r
-  for (i=0; i<l; i++)\r
-    p[i] = tolower(uchar(s[i]));\r
-  luaL_pushresultsize(&b, l);\r
-  return 1;\r
-}\r
-\r
-\r
-static int str_upper (lua_State *L) {\r
-  size_t l;\r
-  size_t i;\r
-  luaL_Buffer b;\r
-  const char *s = luaL_checklstring(L, 1, &l);\r
-  char *p = luaL_buffinitsize(L, &b, l);\r
-  for (i=0; i<l; i++)\r
-    p[i] = toupper(uchar(s[i]));\r
-  luaL_pushresultsize(&b, l);\r
-  return 1;\r
-}\r
-\r
-\r
-/* reasonable limit to avoid arithmetic overflow */\r
-#define MAXSIZE   ((~(size_t)0) >> 1)\r
-\r
-static int str_rep (lua_State *L) {\r
-  size_t l, lsep;\r
-  const char *s = luaL_checklstring(L, 1, &l);\r
-  int n = luaL_checkint(L, 2);\r
-  const char *sep = luaL_optlstring(L, 3, "", &lsep);\r
-  if (n <= 0) lua_pushliteral(L, "");\r
-  else if (l + lsep < l || l + lsep >= MAXSIZE / n)  /* may overflow? */\r
-    return luaL_error(L, "resulting string too large");\r
-  else {\r
-    size_t totallen = n * l + (n - 1) * lsep;\r
-    luaL_Buffer b;\r
-    char *p = luaL_buffinitsize(L, &b, totallen);\r
-    while (n-- > 1) {  /* first n-1 copies (followed by separator) */\r
-      memcpy(p, s, l * sizeof(char)); p += l;\r
-      if (lsep > 0) {  /* avoid empty 'memcpy' (may be expensive) */\r
-        memcpy(p, sep, lsep * sizeof(char)); p += lsep;\r
-      }\r
-    }\r
-    memcpy(p, s, l * sizeof(char));  /* last copy (not followed by separator) */\r
-    luaL_pushresultsize(&b, totallen);\r
-  }\r
-  return 1;\r
-}\r
-\r
-\r
-static int str_byte (lua_State *L) {\r
-  size_t l;\r
-  const char *s = luaL_checklstring(L, 1, &l);\r
-  size_t posi = posrelat(luaL_optinteger(L, 2, 1), l);\r
-  size_t pose = posrelat(luaL_optinteger(L, 3, posi), l);\r
-  int n, i;\r
-  if (posi < 1) posi = 1;\r
-  if (pose > l) pose = l;\r
-  if (posi > pose) return 0;  /* empty interval; return no values */\r
-  n = (int)(pose -  posi + 1);\r
-  if (posi + n <= pose)  /* (size_t -> int) overflow? */\r
-    return luaL_error(L, "string slice too long");\r
-  luaL_checkstack(L, n, "string slice too long");\r
-  for (i=0; i<n; i++)\r
-    lua_pushinteger(L, uchar(s[posi+i-1]));\r
-  return n;\r
-}\r
-\r
-\r
-static int str_char (lua_State *L) {\r
-  int n = lua_gettop(L);  /* number of arguments */\r
-  int i;\r
-  luaL_Buffer b;\r
-  char *p = luaL_buffinitsize(L, &b, n);\r
-  for (i=1; i<=n; i++) {\r
-    int c = luaL_checkint(L, i);\r
-    luaL_argcheck(L, uchar(c) == c, i, "value out of range");\r
-    p[i - 1] = uchar(c);\r
-  }\r
-  luaL_pushresultsize(&b, n);\r
-  return 1;\r
-}\r
-\r
-\r
-static int writer (lua_State *L, const void* b, size_t size, void* B) {\r
-  (void)L;\r
-  luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);\r
-  return 0;\r
-}\r
-\r
-\r
-static int str_dump (lua_State *L) {\r
-  luaL_Buffer b;\r
-  luaL_checktype(L, 1, LUA_TFUNCTION);\r
-  lua_settop(L, 1);\r
-  luaL_buffinit(L,&b);\r
-  if (lua_dump(L, writer, &b) != 0)\r
-    return luaL_error(L, "unable to dump given function");\r
-  luaL_pushresult(&b);\r
-  return 1;\r
-}\r
-\r
-\r
-\r
-/*\r
-** {======================================================\r
-** PATTERN MATCHING\r
-** =======================================================\r
-*/\r
-\r
-\r
-#define CAP_UNFINISHED  (-1)\r
-#define CAP_POSITION  (-2)\r
-\r
-\r
-typedef struct MatchState {\r
-  int matchdepth;  /* control for recursive depth (to avoid C stack overflow) */\r
-  const char *src_init;  /* init of source string */\r
-  const char *src_end;  /* end ('\0') of source string */\r
-  const char *p_end;  /* end ('\0') of pattern */\r
-  lua_State *L;\r
-  int level;  /* total number of captures (finished or unfinished) */\r
-  struct {\r
-    const char *init;\r
-    ptrdiff_t len;\r
-  } capture[LUA_MAXCAPTURES];\r
-} MatchState;\r
-\r
-\r
-/* recursive function */\r
-static const char *match (MatchState *ms, const char *s, const char *p);\r
-\r
-\r
-/* maximum recursion depth for 'match' */\r
-#if !defined(MAXCCALLS)\r
-#define MAXCCALLS 200\r
-#endif\r
-\r
-\r
-#define L_ESC   '%'\r
-#define SPECIALS  "^$*+?.([%-"\r
-\r
-\r
-static int check_capture (MatchState *ms, int l) {\r
-  l -= '1';\r
-  if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)\r
-    return luaL_error(ms->L, "invalid capture index %%%d", l + 1);\r
-  return l;\r
-}\r
-\r
-\r
-static int capture_to_close (MatchState *ms) {\r
-  int level = ms->level;\r
-  for (level--; level>=0; level--)\r
-    if (ms->capture[level].len == CAP_UNFINISHED) return level;\r
-  return luaL_error(ms->L, "invalid pattern capture");\r
-}\r
-\r
-\r
-static const char *classend (MatchState *ms, const char *p) {\r
-  switch (*p++) {\r
-    case L_ESC: {\r
-      if (p == ms->p_end)\r
-        luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");\r
-      return p+1;\r
-    }\r
-    case '[': {\r
-      if (*p == '^') p++;\r
-      do {  /* look for a `]' */\r
-        if (p == ms->p_end)\r
-          luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");\r
-        if (*(p++) == L_ESC && p < ms->p_end)\r
-          p++;  /* skip escapes (e.g. `%]') */\r
-      } while (*p != ']');\r
-      return p+1;\r
-    }\r
-    default: {\r
-      return p;\r
-    }\r
-  }\r
-}\r
-\r
-\r
-static int match_class (int c, int cl) {\r
-  int res;\r
-  switch (tolower(cl)) {\r
-    case 'a' : res = isalpha(c); break;\r
-    case 'c' : res = iscntrl(c); break;\r
-    case 'd' : res = isdigit(c); break;\r
-    case 'g' : res = isgraph(c); break;\r
-    case 'l' : res = islower(c); break;\r
-    case 'p' : res = ispunct(c); break;\r
-    case 's' : res = isspace(c); break;\r
-    case 'u' : res = isupper(c); break;\r
-    case 'w' : res = isalnum(c); break;\r
-    case 'x' : res = isxdigit(c); break;\r
-    case 'z' : res = (c == 0); break;  /* deprecated option */\r
-    default: return (cl == c);\r
-  }\r
-  return (islower(cl) ? res : !res);\r
-}\r
-\r
-\r
-static int matchbracketclass (int c, const char *p, const char *ec) {\r
-  int sig = 1;\r
-  if (*(p+1) == '^') {\r
-    sig = 0;\r
-    p++;  /* skip the `^' */\r
-  }\r
-  while (++p < ec) {\r
-    if (*p == L_ESC) {\r
-      p++;\r
-      if (match_class(c, uchar(*p)))\r
-        return sig;\r
-    }\r
-    else if ((*(p+1) == '-') && (p+2 < ec)) {\r
-      p+=2;\r
-      if (uchar(*(p-2)) <= c && c <= uchar(*p))\r
-        return sig;\r
-    }\r
-    else if (uchar(*p) == c) return sig;\r
-  }\r
-  return !sig;\r
-}\r
-\r
-\r
-static int singlematch (MatchState *ms, const char *s, const char *p,\r
-                        const char *ep) {\r
-  if (s >= ms->src_end)\r
-    return 0;\r
-  else {\r
-    int c = uchar(*s);\r
-    switch (*p) {\r
-      case '.': return 1;  /* matches any char */\r
-      case L_ESC: return match_class(c, uchar(*(p+1)));\r
-      case '[': return matchbracketclass(c, p, ep-1);\r
-      default:  return (uchar(*p) == c);\r
-    }\r
-  }\r
-}\r
-\r
-\r
-static const char *matchbalance (MatchState *ms, const char *s,\r
-                                   const char *p) {\r
-  if (p >= ms->p_end - 1)\r
-    luaL_error(ms->L, "malformed pattern "\r
-                      "(missing arguments to " LUA_QL("%%b") ")");\r
-  if (*s != *p) return NULL;\r
-  else {\r
-    int b = *p;\r
-    int e = *(p+1);\r
-    int cont = 1;\r
-    while (++s < ms->src_end) {\r
-      if (*s == e) {\r
-        if (--cont == 0) return s+1;\r
-      }\r
-      else if (*s == b) cont++;\r
-    }\r
-  }\r
-  return NULL;  /* string ends out of balance */\r
-}\r
-\r
-\r
-static const char *max_expand (MatchState *ms, const char *s,\r
-                                 const char *p, const char *ep) {\r
-  ptrdiff_t i = 0;  /* counts maximum expand for item */\r
-  while (singlematch(ms, s + i, p, ep))\r
-    i++;\r
-  /* keeps trying to match with the maximum repetitions */\r
-  while (i>=0) {\r
-    const char *res = match(ms, (s+i), ep+1);\r
-    if (res) return res;\r
-    i--;  /* else didn't match; reduce 1 repetition to try again */\r
-  }\r
-  return NULL;\r
-}\r
-\r
-\r
-static const char *min_expand (MatchState *ms, const char *s,\r
-                                 const char *p, const char *ep) {\r
-  for (;;) {\r
-    const char *res = match(ms, s, ep+1);\r
-    if (res != NULL)\r
-      return res;\r
-    else if (singlematch(ms, s, p, ep))\r
-      s++;  /* try with one more repetition */\r
-    else return NULL;\r
-  }\r
-}\r
-\r
-\r
-static const char *start_capture (MatchState *ms, const char *s,\r
-                                    const char *p, int what) {\r
-  const char *res;\r
-  int level = ms->level;\r
-  if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");\r
-  ms->capture[level].init = s;\r
-  ms->capture[level].len = what;\r
-  ms->level = level+1;\r
-  if ((res=match(ms, s, p)) == NULL)  /* match failed? */\r
-    ms->level--;  /* undo capture */\r
-  return res;\r
-}\r
-\r
-\r
-static const char *end_capture (MatchState *ms, const char *s,\r
-                                  const char *p) {\r
-  int l = capture_to_close(ms);\r
-  const char *res;\r
-  ms->capture[l].len = s - ms->capture[l].init;  /* close capture */\r
-  if ((res = match(ms, s, p)) == NULL)  /* match failed? */\r
-    ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */\r
-  return res;\r
-}\r
-\r
-\r
-static const char *match_capture (MatchState *ms, const char *s, int l) {\r
-  size_t len;\r
-  l = check_capture(ms, l);\r
-  len = ms->capture[l].len;\r
-  if ((size_t)(ms->src_end-s) >= len &&\r
-      memcmp(ms->capture[l].init, s, len) == 0)\r
-    return s+len;\r
-  else return NULL;\r
-}\r
-\r
-\r
-static const char *match (MatchState *ms, const char *s, const char *p) {\r
-  if (ms->matchdepth-- == 0)\r
-    luaL_error(ms->L, "pattern too complex");\r
-  init: /* using goto's to optimize tail recursion */\r
-  if (p != ms->p_end) {  /* end of pattern? */\r
-    switch (*p) {\r
-      case '(': {  /* start capture */\r
-        if (*(p + 1) == ')')  /* position capture? */\r
-          s = start_capture(ms, s, p + 2, CAP_POSITION);\r
-        else\r
-          s = start_capture(ms, s, p + 1, CAP_UNFINISHED);\r
-        break;\r
-      }\r
-      case ')': {  /* end capture */\r
-        s = end_capture(ms, s, p + 1);\r
-        break;\r
-      }\r
-      case '$': {\r
-        if ((p + 1) != ms->p_end)  /* is the `$' the last char in pattern? */\r
-          goto dflt;  /* no; go to default */\r
-        s = (s == ms->src_end) ? s : NULL;  /* check end of string */\r
-        break;\r
-      }\r
-      case L_ESC: {  /* escaped sequences not in the format class[*+?-]? */\r
-        switch (*(p + 1)) {\r
-          case 'b': {  /* balanced string? */\r
-            s = matchbalance(ms, s, p + 2);\r
-            if (s != NULL) {\r
-              p += 4; goto init;  /* return match(ms, s, p + 4); */\r
-            }  /* else fail (s == NULL) */\r
-            break;\r
-          }\r
-          case 'f': {  /* frontier? */\r
-            const char *ep; char previous;\r
-            p += 2;\r
-            if (*p != '[')\r
-              luaL_error(ms->L, "missing " LUA_QL("[") " after "\r
-                                 LUA_QL("%%f") " in pattern");\r
-            ep = classend(ms, p);  /* points to what is next */\r
-            previous = (s == ms->src_init) ? '\0' : *(s - 1);\r
-            if (!matchbracketclass(uchar(previous), p, ep - 1) &&\r
-               matchbracketclass(uchar(*s), p, ep - 1)) {\r
-              p = ep; goto init;  /* return match(ms, s, ep); */\r
-            }\r
-            s = NULL;  /* match failed */\r
-            break;\r
-          }\r
-          case '0': case '1': case '2': case '3':\r
-          case '4': case '5': case '6': case '7':\r
-          case '8': case '9': {  /* capture results (%0-%9)? */\r
-            s = match_capture(ms, s, uchar(*(p + 1)));\r
-            if (s != NULL) {\r
-              p += 2; goto init;  /* return match(ms, s, p + 2) */\r
-            }\r
-            break;\r
-          }\r
-          default: goto dflt;\r
-        }\r
-        break;\r
-      }\r
-      default: dflt: {  /* pattern class plus optional suffix */\r
-        const char *ep = classend(ms, p);  /* points to optional suffix */\r
-        /* does not match at least once? */\r
-        if (!singlematch(ms, s, p, ep)) {\r
-          if (*ep == '*' || *ep == '?' || *ep == '-') {  /* accept empty? */\r
-            p = ep + 1; goto init;  /* return match(ms, s, ep + 1); */\r
-          }\r
-          else  /* '+' or no suffix */\r
-            s = NULL;  /* fail */\r
-        }\r
-        else {  /* matched once */\r
-          switch (*ep) {  /* handle optional suffix */\r
-            case '?': {  /* optional */\r
-              const char *res;\r
-              if ((res = match(ms, s + 1, ep + 1)) != NULL)\r
-                s = res;\r
-              else {\r
-                p = ep + 1; goto init;  /* else return match(ms, s, ep + 1); */\r
-              }\r
-              break;\r
-            }\r
-            case '+':  /* 1 or more repetitions */\r
-              s++;  /* 1 match already done */\r
-              /* go through */\r
-            case '*':  /* 0 or more repetitions */\r
-              s = max_expand(ms, s, p, ep);\r
-              break;\r
-            case '-':  /* 0 or more repetitions (minimum) */\r
-              s = min_expand(ms, s, p, ep);\r
-              break;\r
-            default:  /* no suffix */\r
-              s++; p = ep; goto init;  /* return match(ms, s + 1, ep); */\r
-          }\r
-        }\r
-        break;\r
-      }\r
-    }\r
-  }\r
-  ms->matchdepth++;\r
-  return s;\r
-}\r
-\r
-\r
-\r
-static const char *lmemfind (const char *s1, size_t l1,\r
-                               const char *s2, size_t l2) {\r
-  if (l2 == 0) return s1;  /* empty strings are everywhere */\r
-  else if (l2 > l1) return NULL;  /* avoids a negative `l1' */\r
-  else {\r
-    const char *init;  /* to search for a `*s2' inside `s1' */\r
-    l2--;  /* 1st char will be checked by `memchr' */\r
-    l1 = l1-l2;  /* `s2' cannot be found after that */\r
-    while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {\r
-      init++;   /* 1st char is already checked */\r
-      if (memcmp(init, s2+1, l2) == 0)\r
-        return init-1;\r
-      else {  /* correct `l1' and `s1' to try again */\r
-        l1 -= init-s1;\r
-        s1 = init;\r
-      }\r
-    }\r
-    return NULL;  /* not found */\r
-  }\r
-}\r
-\r
-\r
-static void push_onecapture (MatchState *ms, int i, const char *s,\r
-                                                    const char *e) {\r
-  if (i >= ms->level) {\r
-    if (i == 0)  /* ms->level == 0, too */\r
-      lua_pushlstring(ms->L, s, e - s);  /* add whole match */\r
-    else\r
-      luaL_error(ms->L, "invalid capture index");\r
-  }\r
-  else {\r
-    ptrdiff_t l = ms->capture[i].len;\r
-    if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");\r
-    if (l == CAP_POSITION)\r
-      lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);\r
-    else\r
-      lua_pushlstring(ms->L, ms->capture[i].init, l);\r
-  }\r
-}\r
-\r
-\r
-static int push_captures (MatchState *ms, const char *s, const char *e) {\r
-  int i;\r
-  int nlevels = (ms->level == 0 && s) ? 1 : ms->level;\r
-  luaL_checkstack(ms->L, nlevels, "too many captures");\r
-  for (i = 0; i < nlevels; i++)\r
-    push_onecapture(ms, i, s, e);\r
-  return nlevels;  /* number of strings pushed */\r
-}\r
-\r
-\r
-/* check whether pattern has no special characters */\r
-static int nospecials (const char *p, size_t l) {\r
-  size_t upto = 0;\r
-  do {\r
-    if (strpbrk(p + upto, SPECIALS))\r
-      return 0;  /* pattern has a special character */\r
-    upto += strlen(p + upto) + 1;  /* may have more after \0 */\r
-  } while (upto <= l);\r
-  return 1;  /* no special chars found */\r
-}\r
-\r
-\r
-static int str_find_aux (lua_State *L, int find) {\r
-  size_t ls, lp;\r
-  const char *s = luaL_checklstring(L, 1, &ls);\r
-  const char *p = luaL_checklstring(L, 2, &lp);\r
-  size_t init = posrelat(luaL_optinteger(L, 3, 1), ls);\r
-  if (init < 1) init = 1;\r
-  else if (init > ls + 1) {  /* start after string's end? */\r
-    lua_pushnil(L);  /* cannot find anything */\r
-    return 1;\r
-  }\r
-  /* explicit request or no special characters? */\r
-  if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {\r
-    /* do a plain search */\r
-    const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);\r
-    if (s2) {\r
-      lua_pushinteger(L, s2 - s + 1);\r
-      lua_pushinteger(L, s2 - s + lp);\r
-      return 2;\r
-    }\r
-  }\r
-  else {\r
-    MatchState ms;\r
-    const char *s1 = s + init - 1;\r
-    int anchor = (*p == '^');\r
-    if (anchor) {\r
-      p++; lp--;  /* skip anchor character */\r
-    }\r
-    ms.L = L;\r
-    ms.matchdepth = MAXCCALLS;\r
-    ms.src_init = s;\r
-    ms.src_end = s + ls;\r
-    ms.p_end = p + lp;\r
-    do {\r
-      const char *res;\r
-      ms.level = 0;\r
-      lua_assert(ms.matchdepth == MAXCCALLS);\r
-      if ((res=match(&ms, s1, p)) != NULL) {\r
-        if (find) {\r
-          lua_pushinteger(L, s1 - s + 1);  /* start */\r
-          lua_pushinteger(L, res - s);   /* end */\r
-          return push_captures(&ms, NULL, 0) + 2;\r
-        }\r
-        else\r
-          return push_captures(&ms, s1, res);\r
-      }\r
-    } while (s1++ < ms.src_end && !anchor);\r
-  }\r
-  lua_pushnil(L);  /* not found */\r
-  return 1;\r
-}\r
-\r
-\r
-static int str_find (lua_State *L) {\r
-  return str_find_aux(L, 1);\r
-}\r
-\r
-\r
-static int str_match (lua_State *L) {\r
-  return str_find_aux(L, 0);\r
-}\r
-\r
-\r
-static int gmatch_aux (lua_State *L) {\r
-  MatchState ms;\r
-  size_t ls, lp;\r
-  const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);\r
-  const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);\r
-  const char *src;\r
-  ms.L = L;\r
-  ms.matchdepth = MAXCCALLS;\r
-  ms.src_init = s;\r
-  ms.src_end = s+ls;\r
-  ms.p_end = p + lp;\r
-  for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));\r
-       src <= ms.src_end;\r
-       src++) {\r
-    const char *e;\r
-    ms.level = 0;\r
-    lua_assert(ms.matchdepth == MAXCCALLS);\r
-    if ((e = match(&ms, src, p)) != NULL) {\r
-      lua_Integer newstart = e-s;\r
-      if (e == src) newstart++;  /* empty match? go at least one position */\r
-      lua_pushinteger(L, newstart);\r
-      lua_replace(L, lua_upvalueindex(3));\r
-      return push_captures(&ms, src, e);\r
-    }\r
-  }\r
-  return 0;  /* not found */\r
-}\r
-\r
-\r
-static int gmatch (lua_State *L) {\r
-  luaL_checkstring(L, 1);\r
-  luaL_checkstring(L, 2);\r
-  lua_settop(L, 2);\r
-  lua_pushinteger(L, 0);\r
-  lua_pushcclosure(L, gmatch_aux, 3);\r
-  return 1;\r
-}\r
-\r
-\r
-static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,\r
-                                                   const char *e) {\r
-  size_t l, i;\r
-  const char *news = lua_tolstring(ms->L, 3, &l);\r
-  for (i = 0; i < l; i++) {\r
-    if (news[i] != L_ESC)\r
-      luaL_addchar(b, news[i]);\r
-    else {\r
-      i++;  /* skip ESC */\r
-      if (!isdigit(uchar(news[i]))) {\r
-        if (news[i] != L_ESC)\r
-          luaL_error(ms->L, "invalid use of " LUA_QL("%c")\r
-                           " in replacement string", L_ESC);\r
-        luaL_addchar(b, news[i]);\r
-      }\r
-      else if (news[i] == '0')\r
-          luaL_addlstring(b, s, e - s);\r
-      else {\r
-        push_onecapture(ms, news[i] - '1', s, e);\r
-        luaL_addvalue(b);  /* add capture to accumulated result */\r
-      }\r
-    }\r
-  }\r
-}\r
-\r
-\r
-static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,\r
-                                       const char *e, int tr) {\r
-  lua_State *L = ms->L;\r
-  switch (tr) {\r
-    case LUA_TFUNCTION: {\r
-      int n;\r
-      lua_pushvalue(L, 3);\r
-      n = push_captures(ms, s, e);\r
-      lua_call(L, n, 1);\r
-      break;\r
-    }\r
-    case LUA_TTABLE: {\r
-      push_onecapture(ms, 0, s, e);\r
-      lua_gettable(L, 3);\r
-      break;\r
-    }\r
-    default: {  /* LUA_TNUMBER or LUA_TSTRING */\r
-      add_s(ms, b, s, e);\r
-      return;\r
-    }\r
-  }\r
-  if (!lua_toboolean(L, -1)) {  /* nil or false? */\r
-    lua_pop(L, 1);\r
-    lua_pushlstring(L, s, e - s);  /* keep original text */\r
-  }\r
-  else if (!lua_isstring(L, -1))\r
-    luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));\r
-  luaL_addvalue(b);  /* add result to accumulator */\r
-}\r
-\r
-\r
-static int str_gsub (lua_State *L) {\r
-  size_t srcl, lp;\r
-  const char *src = luaL_checklstring(L, 1, &srcl);\r
-  const char *p = luaL_checklstring(L, 2, &lp);\r
-  int tr = lua_type(L, 3);\r
-  size_t max_s = luaL_optinteger(L, 4, srcl+1);\r
-  int anchor = (*p == '^');\r
-  size_t n = 0;\r
-  MatchState ms;\r
-  luaL_Buffer b;\r
-  luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||\r
-                   tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,\r
-                      "string/function/table expected");\r
-  luaL_buffinit(L, &b);\r
-  if (anchor) {\r
-    p++; lp--;  /* skip anchor character */\r
-  }\r
-  ms.L = L;\r
-  ms.matchdepth = MAXCCALLS;\r
-  ms.src_init = src;\r
-  ms.src_end = src+srcl;\r
-  ms.p_end = p + lp;\r
-  while (n < max_s) {\r
-    const char *e;\r
-    ms.level = 0;\r
-    lua_assert(ms.matchdepth == MAXCCALLS);\r
-    e = match(&ms, src, p);\r
-    if (e) {\r
-      n++;\r
-      add_value(&ms, &b, src, e, tr);\r
-    }\r
-    if (e && e>src) /* non empty match? */\r
-      src = e;  /* skip it */\r
-    else if (src < ms.src_end)\r
-      luaL_addchar(&b, *src++);\r
-    else break;\r
-    if (anchor) break;\r
-  }\r
-  luaL_addlstring(&b, src, ms.src_end-src);\r
-  luaL_pushresult(&b);\r
-  lua_pushinteger(L, n);  /* number of substitutions */\r
-  return 2;\r
-}\r
-\r
-/* }====================================================== */\r
-\r
-\r
-\r
-/*\r
-** {======================================================\r
-** STRING FORMAT\r
-** =======================================================\r
-*/\r
-\r
-/*\r
-** LUA_INTFRMLEN is the length modifier for integer conversions in\r
-** 'string.format'; LUA_INTFRM_T is the integer type corresponding to\r
-** the previous length\r
-*/\r
-#if !defined(LUA_INTFRMLEN) /* { */\r
-#if defined(LUA_USE_LONGLONG)\r
-\r
-#define LUA_INTFRMLEN   "ll"\r
-#define LUA_INTFRM_T    long long\r
-\r
-#else\r
-\r
-#define LUA_INTFRMLEN   "l"\r
-#define LUA_INTFRM_T    long\r
-\r
-#endif\r
-#endif        /* } */\r
-\r
-\r
-/*\r
-** LUA_FLTFRMLEN is the length modifier for float conversions in\r
-** 'string.format'; LUA_FLTFRM_T is the float type corresponding to\r
-** the previous length\r
-*/\r
-#if !defined(LUA_FLTFRMLEN)\r
-\r
-#define LUA_FLTFRMLEN   ""\r
-#define LUA_FLTFRM_T    double\r
-\r
-#endif\r
-\r
-\r
-/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */\r
-#define MAX_ITEM  512\r
-/* valid flags in a format specification */\r
-#define FLAGS "-+ #0"\r
-/*\r
-** maximum size of each format specification (such as '%-099.99d')\r
-** (+10 accounts for %99.99x plus margin of error)\r
-*/\r
-#define MAX_FORMAT  (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)\r
-\r
-\r
-static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {\r
-  size_t l;\r
-  const char *s = luaL_checklstring(L, arg, &l);\r
-  luaL_addchar(b, '"');\r
-  while (l--) {\r
-    if (*s == '"' || *s == '\\' || *s == '\n') {\r
-      luaL_addchar(b, '\\');\r
-      luaL_addchar(b, *s);\r
-    }\r
-    else if (*s == '\0' || iscntrl(uchar(*s))) {\r
-      char buff[10];\r
-      if (!isdigit(uchar(*(s+1))))\r
-        sprintf(buff, "\\%d", (int)uchar(*s));\r
-      else\r
-        sprintf(buff, "\\%03d", (int)uchar(*s));\r
-      luaL_addstring(b, buff);\r
-    }\r
-    else\r
-      luaL_addchar(b, *s);\r
-    s++;\r
-  }\r
-  luaL_addchar(b, '"');\r
-}\r
-\r
-static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {\r
-  const char *p = strfrmt;\r
-  while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++;  /* skip flags */\r
-  if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))\r
-    luaL_error(L, "invalid format (repeated flags)");\r
-  if (isdigit(uchar(*p))) p++;  /* skip width */\r
-  if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */\r
-  if (*p == '.') {\r
-    p++;\r
-    if (isdigit(uchar(*p))) p++;  /* skip precision */\r
-    if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */\r
-  }\r
-  if (isdigit(uchar(*p)))\r
-    luaL_error(L, "invalid format (width or precision too long)");\r
-  *(form++) = '%';\r
-  memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));\r
-  form += p - strfrmt + 1;\r
-  *form = '\0';\r
-  return p;\r
-}\r
-\r
-\r
-/*\r
-** add length modifier into formats\r
-*/\r
-static void addlenmod (char *form, const char *lenmod) {\r
-  size_t l = strlen(form);\r
-  size_t lm = strlen(lenmod);\r
-  char spec = form[l - 1];\r
-  strcpy(form + l - 1, lenmod);\r
-  form[l + lm - 1] = spec;\r
-  form[l + lm] = '\0';\r
-}\r
-\r
-\r
-static int str_format (lua_State *L) {\r
-  int top = lua_gettop(L);\r
-  int arg = 1;\r
-  size_t sfl;\r
-  const char *strfrmt = luaL_checklstring(L, arg, &sfl);\r
-  const char *strfrmt_end = strfrmt+sfl;\r
-  luaL_Buffer b;\r
-  luaL_buffinit(L, &b);\r
-  while (strfrmt < strfrmt_end) {\r
-    if (*strfrmt != L_ESC)\r
-      luaL_addchar(&b, *strfrmt++);\r
-    else if (*++strfrmt == L_ESC)\r
-      luaL_addchar(&b, *strfrmt++);  /* %% */\r
-    else { /* format item */\r
-      char form[MAX_FORMAT];  /* to store the format (`%...') */\r
-      char *buff = luaL_prepbuffsize(&b, MAX_ITEM);  /* to put formatted item */\r
-      int nb = 0;  /* number of bytes in added item */\r
-      if (++arg > top)\r
-        luaL_argerror(L, arg, "no value");\r
-      strfrmt = scanformat(L, strfrmt, form);\r
-      switch (*strfrmt++) {\r
-        case 'c': {\r
-          nb = sprintf(buff, form, luaL_checkint(L, arg));\r
-          break;\r
-        }\r
-        case 'd': case 'i': {\r
-          lua_Number n = luaL_checknumber(L, arg);\r
-          LUA_INTFRM_T ni = (LUA_INTFRM_T)n;\r
-          lua_Number diff = n - (lua_Number)ni;\r
-          luaL_argcheck(L, -1 < diff && diff < 1, arg,\r
-                        "not a number in proper range");\r
-          addlenmod(form, LUA_INTFRMLEN);\r
-          nb = sprintf(buff, form, ni);\r
-          break;\r
-        }\r
-        case 'o': case 'u': case 'x': case 'X': {\r
-          lua_Number n = luaL_checknumber(L, arg);\r
-          unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n;\r
-          lua_Number diff = n - (lua_Number)ni;\r
-          luaL_argcheck(L, -1 < diff && diff < 1, arg,\r
-                        "not a non-negative number in proper range");\r
-          addlenmod(form, LUA_INTFRMLEN);\r
-          nb = sprintf(buff, form, ni);\r
-          break;\r
-        }\r
-        case 'e': case 'E': case 'f':\r
-#if defined(LUA_USE_AFORMAT)\r
-        case 'a': case 'A':\r
-#endif\r
-        case 'g': case 'G': {\r
-          addlenmod(form, LUA_FLTFRMLEN);\r
-          nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));\r
-          break;\r
-        }\r
-        case 'q': {\r
-          addquoted(L, &b, arg);\r
-          break;\r
-        }\r
-        case 's': {\r
-          size_t l;\r
-          const char *s = luaL_tolstring(L, arg, &l);\r
-          if (!strchr(form, '.') && l >= 100) {\r
-            /* no precision and string is too long to be formatted;\r
-               keep original string */\r
-            luaL_addvalue(&b);\r
-            break;\r
-          }\r
-          else {\r
-            nb = sprintf(buff, form, s);\r
-            lua_pop(L, 1);  /* remove result from 'luaL_tolstring' */\r
-            break;\r
-          }\r
-        }\r
-        default: {  /* also treat cases `pnLlh' */\r
-          return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "\r
-                               LUA_QL("format"), *(strfrmt - 1));\r
-        }\r
-      }\r
-      luaL_addsize(&b, nb);\r
-    }\r
-  }\r
-  luaL_pushresult(&b);\r
-  return 1;\r
-}\r
-\r
-/* }====================================================== */\r
-\r
-\r
-static const luaL_Reg strlib[] = {\r
-  {"byte", str_byte},\r
-  {"char", str_char},\r
-  {"dump", str_dump},\r
-  {"find", str_find},\r
-  {"format", str_format},\r
-  {"gmatch", gmatch},\r
-  {"gsub", str_gsub},\r
-  {"len", str_len},\r
-  {"lower", str_lower},\r
-  {"match", str_match},\r
-  {"rep", str_rep},\r
-  {"reverse", str_reverse},\r
-  {"sub", str_sub},\r
-  {"upper", str_upper},\r
-  {NULL, NULL}\r
-};\r
-\r
-\r
-static void createmetatable (lua_State *L) {\r
-  lua_createtable(L, 0, 1);  /* table to be metatable for strings */\r
-  lua_pushliteral(L, "");  /* dummy string */\r
-  lua_pushvalue(L, -2);  /* copy table */\r
-  lua_setmetatable(L, -2);  /* set table as metatable for strings */\r
-  lua_pop(L, 1);  /* pop dummy string */\r
-  lua_pushvalue(L, -2);  /* get string library */\r
-  lua_setfield(L, -2, "__index");  /* metatable.__index = string */\r
-  lua_pop(L, 1);  /* pop metatable */\r
-}\r
-\r
-\r
-/*\r
-** Open string library\r
-*/\r
-LUAMOD_API int luaopen_string (lua_State *L) {\r
-  luaL_newlib(L, strlib);\r
-  createmetatable(L);\r
-  return 1;\r
-}\r
-\r