xref: /netbsd-src/external/mit/lua/dist/src/lstrlib.c (revision f14316bcbc544b96a93e884bc5c2b15fd60e22ae)
1 /*	$NetBSD: lstrlib.c,v 1.5 2014/07/19 18:38:34 lneto Exp $	*/
2 
3 /*
4 ** $Id: lstrlib.c,v 1.5 2014/07/19 18:38:34 lneto Exp $
5 ** Standard library for string operations and pattern-matching
6 ** See Copyright Notice in lua.h
7 */
8 
9 
10 #ifndef _KERNEL
11 #include <ctype.h>
12 #include <limits.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #endif
18 
19 #define lstrlib_c
20 #define LUA_LIB
21 
22 #include "lua.h"
23 
24 #include "lauxlib.h"
25 #include "lualib.h"
26 
27 
28 /*
29 ** maximum number of captures that a pattern can do during
30 ** pattern-matching. This limit is arbitrary.
31 */
32 #if !defined(LUA_MAXCAPTURES)
33 #define LUA_MAXCAPTURES		32
34 #endif
35 
36 
37 /* macro to `unsign' a character */
38 #define uchar(c)	((unsigned char)(c))
39 
40 
41 
42 static int str_len (lua_State *L) {
43   size_t l;
44   luaL_checklstring(L, 1, &l);
45   lua_pushinteger(L, (lua_Integer)l);
46   return 1;
47 }
48 
49 
50 /* translate a relative string position: negative means back from end */
51 static lua_Integer posrelat (lua_Integer pos, size_t len) {
52   if (pos >= 0) return pos;
53   else if (0u - (size_t)pos > len) return 0;
54   else return (lua_Integer)len + pos + 1;
55 }
56 
57 
58 static int str_sub (lua_State *L) {
59   size_t l;
60   const char *s = luaL_checklstring(L, 1, &l);
61   lua_Integer start = posrelat(luaL_checkinteger(L, 2), l);
62   lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l);
63   if (start < 1) start = 1;
64   if (end > (lua_Integer)l) end = l;
65   if (start <= end)
66     lua_pushlstring(L, s + start - 1, end - start + 1);
67   else lua_pushliteral(L, "");
68   return 1;
69 }
70 
71 
72 static int str_reverse (lua_State *L) {
73   size_t l, i;
74   luaL_Buffer b;
75   const char *s = luaL_checklstring(L, 1, &l);
76   char *p = luaL_buffinitsize(L, &b, l);
77   for (i = 0; i < l; i++)
78     p[i] = s[l - i - 1];
79   luaL_pushresultsize(&b, l);
80   return 1;
81 }
82 
83 
84 static int str_lower (lua_State *L) {
85   size_t l;
86   size_t i;
87   luaL_Buffer b;
88   const char *s = luaL_checklstring(L, 1, &l);
89   char *p = luaL_buffinitsize(L, &b, l);
90   for (i=0; i<l; i++)
91     p[i] = tolower(uchar(s[i]));
92   luaL_pushresultsize(&b, l);
93   return 1;
94 }
95 
96 
97 static int str_upper (lua_State *L) {
98   size_t l;
99   size_t i;
100   luaL_Buffer b;
101   const char *s = luaL_checklstring(L, 1, &l);
102   char *p = luaL_buffinitsize(L, &b, l);
103   for (i=0; i<l; i++)
104     p[i] = toupper(uchar(s[i]));
105   luaL_pushresultsize(&b, l);
106   return 1;
107 }
108 
109 
110 /* reasonable limit to avoid arithmetic overflow and strings too big */
111 #if LUA_MAXINTEGER / 2 <= 0x10000000
112 #define MAXSIZE		((size_t)(LUA_MAXINTEGER / 2))
113 #else
114 #define MAXSIZE		((size_t)0x10000000)
115 #endif
116 
117 static int str_rep (lua_State *L) {
118   size_t l, lsep;
119   const char *s = luaL_checklstring(L, 1, &l);
120   lua_Integer n = luaL_checkinteger(L, 2);
121   const char *sep = luaL_optlstring(L, 3, "", &lsep);
122   if (n <= 0) lua_pushliteral(L, "");
123   else if (l + lsep < l || l + lsep > MAXSIZE / n)  /* may overflow? */
124     return luaL_error(L, "resulting string too large");
125   else {
126     size_t totallen = n * l + (n - 1) * lsep;
127     luaL_Buffer b;
128     char *p = luaL_buffinitsize(L, &b, totallen);
129     while (n-- > 1) {  /* first n-1 copies (followed by separator) */
130       memcpy(p, s, l * sizeof(char)); p += l;
131       if (lsep > 0) {  /* empty 'memcpy' is not that cheap */
132         memcpy(p, sep, lsep * sizeof(char));
133         p += lsep;
134       }
135     }
136     memcpy(p, s, l * sizeof(char));  /* last copy (not followed by separator) */
137     luaL_pushresultsize(&b, totallen);
138   }
139   return 1;
140 }
141 
142 
143 static int str_byte (lua_State *L) {
144   size_t l;
145   const char *s = luaL_checklstring(L, 1, &l);
146   lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l);
147   lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l);
148   int n, i;
149   if (posi < 1) posi = 1;
150   if (pose > (lua_Integer)l) pose = l;
151   if (posi > pose) return 0;  /* empty interval; return no values */
152   n = (int)(pose -  posi + 1);
153   if (posi + n <= pose)  /* arithmetic overflow? */
154     return luaL_error(L, "string slice too long");
155   luaL_checkstack(L, n, "string slice too long");
156   for (i=0; i<n; i++)
157     lua_pushinteger(L, uchar(s[posi+i-1]));
158   return n;
159 }
160 
161 
162 static int str_char (lua_State *L) {
163   int n = lua_gettop(L);  /* number of arguments */
164   int i;
165   luaL_Buffer b;
166   char *p = luaL_buffinitsize(L, &b, n);
167   for (i=1; i<=n; i++) {
168     lua_Integer c = luaL_checkinteger(L, i);
169     luaL_argcheck(L, uchar(c) == c, i, "value out of range");
170     p[i - 1] = uchar(c);
171   }
172   luaL_pushresultsize(&b, n);
173   return 1;
174 }
175 
176 
177 static int writer (lua_State *L, const void *b, size_t size, void *B) {
178   (void)L;
179   luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
180   return 0;
181 }
182 
183 
184 static int str_dump (lua_State *L) {
185   luaL_Buffer b;
186   int strip = lua_toboolean(L, 2);
187   luaL_checktype(L, 1, LUA_TFUNCTION);
188   lua_settop(L, 1);
189   luaL_buffinit(L,&b);
190   if (lua_dump(L, writer, &b, strip) != 0)
191     return luaL_error(L, "unable to dump given function");
192   luaL_pushresult(&b);
193   return 1;
194 }
195 
196 
197 
198 /*
199 ** {======================================================
200 ** PATTERN MATCHING
201 ** =======================================================
202 */
203 
204 
205 #define CAP_UNFINISHED	(-1)
206 #define CAP_POSITION	(-2)
207 
208 
209 typedef struct MatchState {
210   int matchdepth;  /* control for recursive depth (to avoid C stack overflow) */
211   const char *src_init;  /* init of source string */
212   const char *src_end;  /* end ('\0') of source string */
213   const char *p_end;  /* end ('\0') of pattern */
214   lua_State *L;
215   int level;  /* total number of captures (finished or unfinished) */
216   struct {
217     const char *init;
218     ptrdiff_t len;
219   } capture[LUA_MAXCAPTURES];
220 } MatchState;
221 
222 
223 /* recursive function */
224 static const char *match (MatchState *ms, const char *s, const char *p);
225 
226 
227 /* maximum recursion depth for 'match' */
228 #if !defined(MAXCCALLS)
229 #define MAXCCALLS	200
230 #endif
231 
232 
233 #define L_ESC		'%'
234 #define SPECIALS	"^$*+?.([%-"
235 
236 
237 static int check_capture (MatchState *ms, int l) {
238   l -= '1';
239   if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
240     return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
241   return l;
242 }
243 
244 
245 static int capture_to_close (MatchState *ms) {
246   int level = ms->level;
247   for (level--; level>=0; level--)
248     if (ms->capture[level].len == CAP_UNFINISHED) return level;
249   return luaL_error(ms->L, "invalid pattern capture");
250 }
251 
252 
253 static const char *classend (MatchState *ms, const char *p) {
254   switch (*p++) {
255     case L_ESC: {
256       if (p == ms->p_end)
257         luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
258       return p+1;
259     }
260     case '[': {
261       if (*p == '^') p++;
262       do {  /* look for a `]' */
263         if (p == ms->p_end)
264           luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
265         if (*(p++) == L_ESC && p < ms->p_end)
266           p++;  /* skip escapes (e.g. `%]') */
267       } while (*p != ']');
268       return p+1;
269     }
270     default: {
271       return p;
272     }
273   }
274 }
275 
276 
277 static int match_class (int c, int cl) {
278   int res;
279   switch (tolower(cl)) {
280     case 'a' : res = isalpha(c); break;
281     case 'c' : res = iscntrl(c); break;
282     case 'd' : res = isdigit(c); break;
283     case 'g' : res = isgraph(c); break;
284     case 'l' : res = islower(c); break;
285     case 'p' : res = ispunct(c); break;
286     case 's' : res = isspace(c); break;
287     case 'u' : res = isupper(c); break;
288     case 'w' : res = isalnum(c); break;
289     case 'x' : res = isxdigit(c); break;
290     case 'z' : res = (c == 0); break;  /* deprecated option */
291     default: return (cl == c);
292   }
293   return (islower(cl) ? res : !res);
294 }
295 
296 
297 static int matchbracketclass (int c, const char *p, const char *ec) {
298   int sig = 1;
299   if (*(p+1) == '^') {
300     sig = 0;
301     p++;  /* skip the `^' */
302   }
303   while (++p < ec) {
304     if (*p == L_ESC) {
305       p++;
306       if (match_class(c, uchar(*p)))
307         return sig;
308     }
309     else if ((*(p+1) == '-') && (p+2 < ec)) {
310       p+=2;
311       if (uchar(*(p-2)) <= c && c <= uchar(*p))
312         return sig;
313     }
314     else if (uchar(*p) == c) return sig;
315   }
316   return !sig;
317 }
318 
319 
320 static int singlematch (MatchState *ms, const char *s, const char *p,
321                         const char *ep) {
322   if (s >= ms->src_end)
323     return 0;
324   else {
325     int c = uchar(*s);
326     switch (*p) {
327       case '.': return 1;  /* matches any char */
328       case L_ESC: return match_class(c, uchar(*(p+1)));
329       case '[': return matchbracketclass(c, p, ep-1);
330       default:  return (uchar(*p) == c);
331     }
332   }
333 }
334 
335 
336 static const char *matchbalance (MatchState *ms, const char *s,
337                                    const char *p) {
338   if (p >= ms->p_end - 1)
339     luaL_error(ms->L, "malformed pattern "
340                       "(missing arguments to " LUA_QL("%%b") ")");
341   if (*s != *p) return NULL;
342   else {
343     int b = *p;
344     int e = *(p+1);
345     int cont = 1;
346     while (++s < ms->src_end) {
347       if (*s == e) {
348         if (--cont == 0) return s+1;
349       }
350       else if (*s == b) cont++;
351     }
352   }
353   return NULL;  /* string ends out of balance */
354 }
355 
356 
357 static const char *max_expand (MatchState *ms, const char *s,
358                                  const char *p, const char *ep) {
359   ptrdiff_t i = 0;  /* counts maximum expand for item */
360   while (singlematch(ms, s + i, p, ep))
361     i++;
362   /* keeps trying to match with the maximum repetitions */
363   while (i>=0) {
364     const char *res = match(ms, (s+i), ep+1);
365     if (res) return res;
366     i--;  /* else didn't match; reduce 1 repetition to try again */
367   }
368   return NULL;
369 }
370 
371 
372 static const char *min_expand (MatchState *ms, const char *s,
373                                  const char *p, const char *ep) {
374   for (;;) {
375     const char *res = match(ms, s, ep+1);
376     if (res != NULL)
377       return res;
378     else if (singlematch(ms, s, p, ep))
379       s++;  /* try with one more repetition */
380     else return NULL;
381   }
382 }
383 
384 
385 static const char *start_capture (MatchState *ms, const char *s,
386                                     const char *p, int what) {
387   const char *res;
388   int level = ms->level;
389   if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
390   ms->capture[level].init = s;
391   ms->capture[level].len = what;
392   ms->level = level+1;
393   if ((res=match(ms, s, p)) == NULL)  /* match failed? */
394     ms->level--;  /* undo capture */
395   return res;
396 }
397 
398 
399 static const char *end_capture (MatchState *ms, const char *s,
400                                   const char *p) {
401   int l = capture_to_close(ms);
402   const char *res;
403   ms->capture[l].len = s - ms->capture[l].init;  /* close capture */
404   if ((res = match(ms, s, p)) == NULL)  /* match failed? */
405     ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */
406   return res;
407 }
408 
409 
410 static const char *match_capture (MatchState *ms, const char *s, int l) {
411   size_t len;
412   l = check_capture(ms, l);
413   len = ms->capture[l].len;
414   if ((size_t)(ms->src_end-s) >= len &&
415       memcmp(ms->capture[l].init, s, len) == 0)
416     return s+len;
417   else return NULL;
418 }
419 
420 
421 static const char *match (MatchState *ms, const char *s, const char *p) {
422   if (ms->matchdepth-- == 0)
423     luaL_error(ms->L, "pattern too complex");
424   init: /* using goto's to optimize tail recursion */
425   if (p != ms->p_end) {  /* end of pattern? */
426     switch (*p) {
427       case '(': {  /* start capture */
428         if (*(p + 1) == ')')  /* position capture? */
429           s = start_capture(ms, s, p + 2, CAP_POSITION);
430         else
431           s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
432         break;
433       }
434       case ')': {  /* end capture */
435         s = end_capture(ms, s, p + 1);
436         break;
437       }
438       case '$': {
439         if ((p + 1) != ms->p_end)  /* is the `$' the last char in pattern? */
440           goto dflt;  /* no; go to default */
441         s = (s == ms->src_end) ? s : NULL;  /* check end of string */
442         break;
443       }
444       case L_ESC: {  /* escaped sequences not in the format class[*+?-]? */
445         switch (*(p + 1)) {
446           case 'b': {  /* balanced string? */
447             s = matchbalance(ms, s, p + 2);
448             if (s != NULL) {
449               p += 4; goto init;  /* return match(ms, s, p + 4); */
450             }  /* else fail (s == NULL) */
451             break;
452           }
453           case 'f': {  /* frontier? */
454             const char *ep; char previous;
455             p += 2;
456             if (*p != '[')
457               luaL_error(ms->L, "missing " LUA_QL("[") " after "
458                                  LUA_QL("%%f") " in pattern");
459             ep = classend(ms, p);  /* points to what is next */
460             previous = (s == ms->src_init) ? '\0' : *(s - 1);
461             if (!matchbracketclass(uchar(previous), p, ep - 1) &&
462                matchbracketclass(uchar(*s), p, ep - 1)) {
463               p = ep; goto init;  /* return match(ms, s, ep); */
464             }
465             s = NULL;  /* match failed */
466             break;
467           }
468           case '0': case '1': case '2': case '3':
469           case '4': case '5': case '6': case '7':
470           case '8': case '9': {  /* capture results (%0-%9)? */
471             s = match_capture(ms, s, uchar(*(p + 1)));
472             if (s != NULL) {
473               p += 2; goto init;  /* return match(ms, s, p + 2) */
474             }
475             break;
476           }
477           default: goto dflt;
478         }
479         break;
480       }
481       default: dflt: {  /* pattern class plus optional suffix */
482         const char *ep = classend(ms, p);  /* points to optional suffix */
483         /* does not match at least once? */
484         if (!singlematch(ms, s, p, ep)) {
485           if (*ep == '*' || *ep == '?' || *ep == '-') {  /* accept empty? */
486             p = ep + 1; goto init;  /* return match(ms, s, ep + 1); */
487           }
488           else  /* '+' or no suffix */
489             s = NULL;  /* fail */
490         }
491         else {  /* matched once */
492           switch (*ep) {  /* handle optional suffix */
493             case '?': {  /* optional */
494               const char *res;
495               if ((res = match(ms, s + 1, ep + 1)) != NULL)
496                 s = res;
497               else {
498                 p = ep + 1; goto init;  /* else return match(ms, s, ep + 1); */
499               }
500               break;
501             }
502             case '+':  /* 1 or more repetitions */
503               s++;  /* 1 match already done */
504               /* go through */
505             case '*':  /* 0 or more repetitions */
506               s = max_expand(ms, s, p, ep);
507               break;
508             case '-':  /* 0 or more repetitions (minimum) */
509               s = min_expand(ms, s, p, ep);
510               break;
511             default:  /* no suffix */
512               s++; p = ep; goto init;  /* return match(ms, s + 1, ep); */
513           }
514         }
515         break;
516       }
517     }
518   }
519   ms->matchdepth++;
520   return s;
521 }
522 
523 
524 
525 static const char *lmemfind (const char *s1, size_t l1,
526                                const char *s2, size_t l2) {
527   if (l2 == 0) return s1;  /* empty strings are everywhere */
528   else if (l2 > l1) return NULL;  /* avoids a negative `l1' */
529   else {
530     const char *init;  /* to search for a `*s2' inside `s1' */
531     l2--;  /* 1st char will be checked by `memchr' */
532     l1 = l1-l2;  /* `s2' cannot be found after that */
533     while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
534       init++;   /* 1st char is already checked */
535       if (memcmp(init, s2+1, l2) == 0)
536         return init-1;
537       else {  /* correct `l1' and `s1' to try again */
538         l1 -= init-s1;
539         s1 = init;
540       }
541     }
542     return NULL;  /* not found */
543   }
544 }
545 
546 
547 static void push_onecapture (MatchState *ms, int i, const char *s,
548                                                     const char *e) {
549   if (i >= ms->level) {
550     if (i == 0)  /* ms->level == 0, too */
551       lua_pushlstring(ms->L, s, e - s);  /* add whole match */
552     else
553       luaL_error(ms->L, "invalid capture index");
554   }
555   else {
556     ptrdiff_t l = ms->capture[i].len;
557     if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
558     if (l == CAP_POSITION)
559       lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
560     else
561       lua_pushlstring(ms->L, ms->capture[i].init, l);
562   }
563 }
564 
565 
566 static int push_captures (MatchState *ms, const char *s, const char *e) {
567   int i;
568   int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
569   luaL_checkstack(ms->L, nlevels, "too many captures");
570   for (i = 0; i < nlevels; i++)
571     push_onecapture(ms, i, s, e);
572   return nlevels;  /* number of strings pushed */
573 }
574 
575 
576 /* check whether pattern has no special characters */
577 static int nospecials (const char *p, size_t l) {
578   size_t upto = 0;
579   do {
580     if (strpbrk(p + upto, SPECIALS))
581       return 0;  /* pattern has a special character */
582     upto += strlen(p + upto) + 1;  /* may have more after \0 */
583   } while (upto <= l);
584   return 1;  /* no special chars found */
585 }
586 
587 
588 static int str_find_aux (lua_State *L, int find) {
589   size_t ls, lp;
590   const char *s = luaL_checklstring(L, 1, &ls);
591   const char *p = luaL_checklstring(L, 2, &lp);
592   lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
593   if (init < 1) init = 1;
594   else if (init > (lua_Integer)ls + 1) {  /* start after string's end? */
595     lua_pushnil(L);  /* cannot find anything */
596     return 1;
597   }
598   /* explicit request or no special characters? */
599   if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
600     /* do a plain search */
601     const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);
602     if (s2) {
603       lua_pushinteger(L, s2 - s + 1);
604       lua_pushinteger(L, s2 - s + lp);
605       return 2;
606     }
607   }
608   else {
609     MatchState ms;
610     const char *s1 = s + init - 1;
611     int anchor = (*p == '^');
612     if (anchor) {
613       p++; lp--;  /* skip anchor character */
614     }
615     ms.L = L;
616     ms.matchdepth = MAXCCALLS;
617     ms.src_init = s;
618     ms.src_end = s + ls;
619     ms.p_end = p + lp;
620     do {
621       const char *res;
622       ms.level = 0;
623       lua_assert(ms.matchdepth == MAXCCALLS);
624       if ((res=match(&ms, s1, p)) != NULL) {
625         if (find) {
626           lua_pushinteger(L, s1 - s + 1);  /* start */
627           lua_pushinteger(L, res - s);   /* end */
628           return push_captures(&ms, NULL, 0) + 2;
629         }
630         else
631           return push_captures(&ms, s1, res);
632       }
633     } while (s1++ < ms.src_end && !anchor);
634   }
635   lua_pushnil(L);  /* not found */
636   return 1;
637 }
638 
639 
640 static int str_find (lua_State *L) {
641   return str_find_aux(L, 1);
642 }
643 
644 
645 static int str_match (lua_State *L) {
646   return str_find_aux(L, 0);
647 }
648 
649 
650 static int gmatch_aux (lua_State *L) {
651   MatchState ms;
652   size_t ls, lp;
653   const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
654   const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
655   const char *src;
656   ms.L = L;
657   ms.matchdepth = MAXCCALLS;
658   ms.src_init = s;
659   ms.src_end = s+ls;
660   ms.p_end = p + lp;
661   for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
662        src <= ms.src_end;
663        src++) {
664     const char *e;
665     ms.level = 0;
666     lua_assert(ms.matchdepth == MAXCCALLS);
667     if ((e = match(&ms, src, p)) != NULL) {
668       lua_Integer newstart = e-s;
669       if (e == src) newstart++;  /* empty match? go at least one position */
670       lua_pushinteger(L, newstart);
671       lua_replace(L, lua_upvalueindex(3));
672       return push_captures(&ms, src, e);
673     }
674   }
675   return 0;  /* not found */
676 }
677 
678 
679 static int gmatch (lua_State *L) {
680   luaL_checkstring(L, 1);
681   luaL_checkstring(L, 2);
682   lua_settop(L, 2);
683   lua_pushinteger(L, 0);
684   lua_pushcclosure(L, gmatch_aux, 3);
685   return 1;
686 }
687 
688 
689 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
690                                                    const char *e) {
691   size_t l, i;
692   const char *news = lua_tolstring(ms->L, 3, &l);
693   for (i = 0; i < l; i++) {
694     if (news[i] != L_ESC)
695       luaL_addchar(b, news[i]);
696     else {
697       i++;  /* skip ESC */
698       if (!isdigit(uchar(news[i]))) {
699         if (news[i] != L_ESC)
700           luaL_error(ms->L, "invalid use of " LUA_QL("%c")
701                            " in replacement string", L_ESC);
702         luaL_addchar(b, news[i]);
703       }
704       else if (news[i] == '0')
705           luaL_addlstring(b, s, e - s);
706       else {
707         push_onecapture(ms, news[i] - '1', s, e);
708         luaL_addvalue(b);  /* add capture to accumulated result */
709       }
710     }
711   }
712 }
713 
714 
715 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
716                                        const char *e, int tr) {
717   lua_State *L = ms->L;
718   switch (tr) {
719     case LUA_TFUNCTION: {
720       int n;
721       lua_pushvalue(L, 3);
722       n = push_captures(ms, s, e);
723       lua_call(L, n, 1);
724       break;
725     }
726     case LUA_TTABLE: {
727       push_onecapture(ms, 0, s, e);
728       lua_gettable(L, 3);
729       break;
730     }
731     default: {  /* LUA_TNUMBER or LUA_TSTRING */
732       add_s(ms, b, s, e);
733       return;
734     }
735   }
736   if (!lua_toboolean(L, -1)) {  /* nil or false? */
737     lua_pop(L, 1);
738     lua_pushlstring(L, s, e - s);  /* keep original text */
739   }
740   else if (!lua_isstring(L, -1))
741     luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
742   luaL_addvalue(b);  /* add result to accumulator */
743 }
744 
745 
746 static int str_gsub (lua_State *L) {
747   size_t srcl, lp;
748   const char *src = luaL_checklstring(L, 1, &srcl);
749   const char *p = luaL_checklstring(L, 2, &lp);
750   int tr = lua_type(L, 3);
751   size_t max_s = luaL_optinteger(L, 4, srcl+1);
752   int anchor = (*p == '^');
753   size_t n = 0;
754   MatchState ms;
755   luaL_Buffer b;
756   luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
757                    tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
758                       "string/function/table expected");
759   luaL_buffinit(L, &b);
760   if (anchor) {
761     p++; lp--;  /* skip anchor character */
762   }
763   ms.L = L;
764   ms.matchdepth = MAXCCALLS;
765   ms.src_init = src;
766   ms.src_end = src+srcl;
767   ms.p_end = p + lp;
768   while (n < max_s) {
769     const char *e;
770     ms.level = 0;
771     lua_assert(ms.matchdepth == MAXCCALLS);
772     e = match(&ms, src, p);
773     if (e) {
774       n++;
775       add_value(&ms, &b, src, e, tr);
776     }
777     if (e && e>src) /* non empty match? */
778       src = e;  /* skip it */
779     else if (src < ms.src_end)
780       luaL_addchar(&b, *src++);
781     else break;
782     if (anchor) break;
783   }
784   luaL_addlstring(&b, src, ms.src_end-src);
785   luaL_pushresult(&b);
786   lua_pushinteger(L, n);  /* number of substitutions */
787   return 2;
788 }
789 
790 /* }====================================================== */
791 
792 
793 
794 /*
795 ** {======================================================
796 ** STRING FORMAT
797 ** =======================================================
798 */
799 
800 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
801 #define MAX_ITEM	512
802 
803 /* valid flags in a format specification */
804 #define FLAGS	"-+ #0"
805 
806 /*
807 ** maximum size of each format specification (such as "%-099.99d")
808 ** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error)
809 */
810 #define MAX_FORMAT	(sizeof(FLAGS) + 2 + 10)
811 
812 
813 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
814   size_t l;
815   const char *s = luaL_checklstring(L, arg, &l);
816   luaL_addchar(b, '"');
817   while (l--) {
818     if (*s == '"' || *s == '\\' || *s == '\n') {
819       luaL_addchar(b, '\\');
820       luaL_addchar(b, *s);
821     }
822     else if (*s == '\0' || iscntrl(uchar(*s))) {
823       char buff[10];
824       if (!isdigit(uchar(*(s+1))))
825         sprintf(buff, "\\%d", (int)uchar(*s));
826       else
827         sprintf(buff, "\\%03d", (int)uchar(*s));
828       luaL_addstring(b, buff);
829     }
830     else
831       luaL_addchar(b, *s);
832     s++;
833   }
834   luaL_addchar(b, '"');
835 }
836 
837 static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
838   const char *p = strfrmt;
839   while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++;  /* skip flags */
840   if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
841     luaL_error(L, "invalid format (repeated flags)");
842   if (isdigit(uchar(*p))) p++;  /* skip width */
843   if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */
844   if (*p == '.') {
845     p++;
846     if (isdigit(uchar(*p))) p++;  /* skip precision */
847     if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */
848   }
849   if (isdigit(uchar(*p)))
850     luaL_error(L, "invalid format (width or precision too long)");
851   *(form++) = '%';
852   memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
853   form += p - strfrmt + 1;
854   *form = '\0';
855   return p;
856 }
857 
858 
859 /*
860 ** add length modifier into formats
861 */
862 static void addlenmod (char *form, const char *lenmod) {
863   size_t l = strlen(form);
864   size_t lm = strlen(lenmod);
865   char spec = form[l - 1];
866   strcpy(form + l - 1, lenmod);
867   form[l + lm - 1] = spec;
868   form[l + lm] = '\0';
869 }
870 
871 
872 static int str_format (lua_State *L) {
873   int top = lua_gettop(L);
874   int arg = 1;
875   size_t sfl;
876   const char *strfrmt = luaL_checklstring(L, arg, &sfl);
877   const char *strfrmt_end = strfrmt+sfl;
878   luaL_Buffer b;
879   luaL_buffinit(L, &b);
880   while (strfrmt < strfrmt_end) {
881     if (*strfrmt != L_ESC)
882       luaL_addchar(&b, *strfrmt++);
883     else if (*++strfrmt == L_ESC)
884       luaL_addchar(&b, *strfrmt++);  /* %% */
885     else { /* format item */
886       char form[MAX_FORMAT];  /* to store the format (`%...') */
887       char *buff = luaL_prepbuffsize(&b, MAX_ITEM);  /* to put formatted item */
888       int nb = 0;  /* number of bytes in added item */
889       if (++arg > top)
890         luaL_argerror(L, arg, "no value");
891       strfrmt = scanformat(L, strfrmt, form);
892       switch (*strfrmt++) {
893         case 'c': {
894           nb = sprintf(buff, form, luaL_checkint(L, arg));
895           break;
896         }
897         case 'd': case 'i':
898         case 'o': case 'u': case 'x': case 'X': {
899           lua_Integer n = luaL_checkinteger(L, arg);
900           addlenmod(form, LUA_INTEGER_FRMLEN);
901           nb = sprintf(buff, form, n);
902           break;
903         }
904 #ifndef _KERNEL
905         case 'e': case 'E': case 'f':
906 #if defined(LUA_USE_AFORMAT)
907         case 'a': case 'A':
908 #endif
909         case 'g': case 'G': {
910           addlenmod(form, LUA_NUMBER_FRMLEN);
911           nb = sprintf(buff, form, luaL_checknumber(L, arg));
912           break;
913         }
914 #endif
915         case 'q': {
916           addquoted(L, &b, arg);
917           break;
918         }
919         case 's': {
920           size_t l;
921           const char *s = luaL_tolstring(L, arg, &l);
922           if (!strchr(form, '.') && l >= 100) {
923             /* no precision and string is too long to be formatted;
924                keep original string */
925             luaL_addvalue(&b);
926             break;
927           }
928           else {
929             nb = sprintf(buff, form, s);
930             lua_pop(L, 1);  /* remove result from 'luaL_tolstring' */
931             break;
932           }
933         }
934         default: {  /* also treat cases `pnLlh' */
935           return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
936                                LUA_QL("format"), *(strfrmt - 1));
937         }
938       }
939       luaL_addsize(&b, nb);
940     }
941   }
942   luaL_pushresult(&b);
943   return 1;
944 }
945 
946 /* }====================================================== */
947 
948 
949 /*
950 ** {======================================================
951 ** PACK/UNPACK
952 ** =======================================================
953 */
954 
955 
956 /* number of bits in a character */
957 #define NB	CHAR_BIT
958 
959 /* mask for one character (NB 1's) */
960 #define MC	((1 << NB) - 1)
961 
962 /* mask for one character without sign bit ((NB - 1) 1's) */
963 #define SM	(MC >> 1)
964 
965 /* size of a lua_Integer */
966 #define SZINT	((int)sizeof(lua_Integer))
967 
968 /* maximum size for the binary representation of an integer */
969 #define MAXINTSIZE	12
970 
971 
972 static union {
973   int dummy;
974   char little;  /* true iff machine is little endian */
975 } const nativeendian = {1};
976 
977 
978 static int getendian (lua_State *L, int arg) {
979   const char *endian = luaL_optstring(L, arg,
980                              (nativeendian.little ? "l" : "b"));
981   if (*endian == 'n')  /* native? */
982     return nativeendian.little;
983   luaL_argcheck(L, *endian == 'l' || *endian == 'b', arg,
984                    "endianness must be 'l'/'b'/'n'");
985   return (*endian == 'l');
986 }
987 
988 
989 static int getintsize (lua_State *L, int arg) {
990   int size = luaL_optint(L, arg, 0);
991   if (size == 0) size = SZINT;
992   luaL_argcheck(L, 1 <= size && size <= MAXINTSIZE, arg,
993                    "integer size out of valid range");
994   return size;
995 }
996 
997 
998 /* mask for all ones in last byte in a lua Integer */
999 #define HIGHERBYTE    ((lua_Unsigned)MC << (NB * (SZINT - 1)))
1000 
1001 
1002 static int dumpint (char *buff, lua_Integer m, int littleendian, int size) {
1003   int i;
1004   lua_Unsigned n = (lua_Unsigned)m;
1005   lua_Unsigned mask = (m >= 0) ? 0 : HIGHERBYTE;  /*  sign extension */
1006   if (littleendian) {
1007     for (i = 0; i < size - 1; i++) {
1008       buff[i] = (n & MC);
1009       n = (n >> NB) | mask;
1010     }
1011   }
1012   else {
1013     for (i = size - 1; i > 0; i--) {
1014       buff[i] = (n & MC);
1015       n = (n >> NB) | mask;
1016     }
1017   }
1018   buff[i] = (n & MC);  /* last byte */
1019   if (size < SZINT) {  /* need test for overflow? */
1020     /* OK if there are only zeros left in higher bytes,
1021        or only ones left (excluding non-signal bits in last byte) */
1022     return ((n & ~(lua_Unsigned)MC) == 0 ||
1023             (n | SM) == ~(lua_Unsigned)0);
1024   }
1025   else return 1;  /* no overflow can occur with full size */
1026 }
1027 
1028 
1029 static int dumpint_l (lua_State *L) {
1030   char buff[MAXINTSIZE];
1031   lua_Integer n = luaL_checkinteger(L, 1);
1032   int size = getintsize(L, 2);
1033   int endian = getendian(L, 3);
1034   if (dumpint(buff, n, endian, size))
1035     lua_pushlstring(L, buff, size);
1036   else
1037     luaL_error(L, "integer does not fit into given size (%d)", size);
1038   return 1;
1039 }
1040 
1041 
1042 /* mask to check higher-order byte + signal bit of next (lower) byte */
1043 #define HIGHERBYTE1   (HIGHERBYTE | (HIGHERBYTE >> 1))
1044 
1045 
1046 static int undumpint (const char *buff, lua_Integer *res,
1047                       int littleendian, int size) {
1048   lua_Unsigned n = 0;
1049   int i;
1050   for (i = 0; i < size; i++) {
1051     if (i >= SZINT) {  /* will throw away a byte? */
1052       /* check for overflow: it is OK to throw away leading zeros for a
1053          positive number, leading ones for a negative number, and a
1054          leading zero byte to allow unsigned integers with a 1 in
1055          its "signal bit" */
1056       if (!((n & HIGHERBYTE1) == 0 ||  /* zeros for positive number */
1057           (n & HIGHERBYTE1) == HIGHERBYTE1 ||  /* ones for negative number */
1058           (i == size - 1 && (n & HIGHERBYTE) == 0)))  /* leading zero */
1059         return 0;  /* overflow */
1060     }
1061     n <<= NB;
1062     n |= (lua_Unsigned)(unsigned char)buff[littleendian ? size - 1 - i : i];
1063   }
1064   if (size < SZINT) {  /* need sign extension? */
1065     lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
1066     *res = (lua_Integer)((n ^ mask) - mask);  /* do sign extension */
1067   }
1068   else
1069     *res = (lua_Integer)n;
1070   return 1;
1071 }
1072 
1073 
1074 static int undumpint_l (lua_State *L) {
1075   lua_Integer res;
1076   size_t len;
1077   const char *s = luaL_checklstring(L, 1, &len);
1078   lua_Integer pos = posrelat(luaL_optinteger(L, 2, 1), len);
1079   int size = getintsize(L, 3);
1080   int endian = getendian(L, 4);
1081   luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,
1082                    "string too short");
1083   if(undumpint(s + pos - 1, &res, endian, size))
1084     lua_pushinteger(L, res);
1085   else
1086     luaL_error(L, "result does not fit into a Lua integer");
1087   return 1;
1088 }
1089 
1090 
1091 #ifndef _KERNEL
1092 static void correctendianness (lua_State *L, char *b, int size, int endianarg) {
1093   int endian = getendian(L, endianarg);
1094   if (endian != nativeendian.little) {  /* not native endianness? */
1095     int i = 0;
1096     while (i < --size) {
1097       char temp = b[i];
1098       b[i++] = b[size];
1099       b[size] = temp;
1100     }
1101   }
1102 }
1103 
1104 
1105 static int getfloatsize (lua_State *L, int arg) {
1106   const char *size = luaL_optstring(L, arg, "n");
1107   if (*size == 'n') return sizeof(lua_Number);
1108   luaL_argcheck(L, *size == 'd' || *size == 'f', arg,
1109                    "size must be 'f'/'d'/'n'");
1110   return (*size == 'd' ? sizeof(double) : sizeof(float));
1111 }
1112 
1113 
1114 static int dumpfloat_l (lua_State *L) {
1115   float f;  double d;
1116   char *pn;  /* pointer to number */
1117   lua_Number n = luaL_checknumber(L, 1);
1118   int size = getfloatsize(L, 2);
1119   if (size == sizeof(lua_Number))
1120     pn = (char*)&n;
1121   else if (size == sizeof(float)) {
1122     f = (float)n;
1123     pn = (char*)&f;
1124   }
1125   else {  /* native lua_Number may be neither float nor double */
1126     lua_assert(size == sizeof(double));
1127     d = (double)n;
1128     pn = (char*)&d;
1129   }
1130   correctendianness(L, pn, size, 3);
1131   lua_pushlstring(L, pn, size);
1132   return 1;
1133 }
1134 
1135 
1136 static int undumpfloat_l (lua_State *L) {
1137   lua_Number res;
1138   size_t len;
1139   const char *s = luaL_checklstring(L, 1, &len);
1140   lua_Integer pos = posrelat(luaL_optinteger(L, 2, 1), len);
1141   int size = getfloatsize(L, 3);
1142   luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,
1143                    "string too short");
1144   if (size == sizeof(lua_Number)) {
1145     memcpy(&res, s + pos - 1, size);
1146     correctendianness(L, (char*)&res, size, 4);
1147   }
1148   else if (size == sizeof(float)) {
1149     float f;
1150     memcpy(&f, s + pos - 1, size);
1151     correctendianness(L, (char*)&f, size, 4);
1152     res = (lua_Number)f;
1153   }
1154   else {  /* native lua_Number may be neither float nor double */
1155     double d;
1156     lua_assert(size == sizeof(double));
1157     memcpy(&d, s + pos - 1, size);
1158     correctendianness(L, (char*)&d, size, 4);
1159     res = (lua_Number)d;
1160   }
1161   lua_pushnumber(L, res);
1162   return 1;
1163 }
1164 #endif
1165 
1166 /* }====================================================== */
1167 
1168 
1169 static const luaL_Reg strlib[] = {
1170   {"byte", str_byte},
1171   {"char", str_char},
1172   {"dump", str_dump},
1173   {"find", str_find},
1174   {"format", str_format},
1175   {"gmatch", gmatch},
1176   {"gsub", str_gsub},
1177   {"len", str_len},
1178   {"lower", str_lower},
1179   {"match", str_match},
1180   {"rep", str_rep},
1181   {"reverse", str_reverse},
1182   {"sub", str_sub},
1183   {"upper", str_upper},
1184 #ifndef _KERNEL
1185   {"dumpfloat", dumpfloat_l},
1186 #endif
1187   {"dumpint", dumpint_l},
1188 #ifndef _KERNEL
1189   {"undumpfloat", undumpfloat_l},
1190 #endif
1191   {"undumpint", undumpint_l},
1192   {NULL, NULL}
1193 };
1194 
1195 
1196 static void createmetatable (lua_State *L) {
1197   lua_createtable(L, 0, 1);  /* table to be metatable for strings */
1198   lua_pushliteral(L, "");  /* dummy string */
1199   lua_pushvalue(L, -2);  /* copy table */
1200   lua_setmetatable(L, -2);  /* set table as metatable for strings */
1201   lua_pop(L, 1);  /* pop dummy string */
1202   lua_pushvalue(L, -2);  /* get string library */
1203   lua_setfield(L, -2, "__index");  /* metatable.__index = string */
1204   lua_pop(L, 1);  /* pop metatable */
1205 }
1206 
1207 
1208 /*
1209 ** Open string library
1210 */
1211 LUAMOD_API int luaopen_string (lua_State *L) {
1212   luaL_newlib(L, strlib);
1213   createmetatable(L);
1214   return 1;
1215 }
1216 
1217