The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lstrlib.cpp
Go to the documentation of this file.
1 /*
2 ** Standard library for string operations and pattern-matching
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <ctype.h>
8 #include <stddef.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #define lstrlib_c
14 #define LUA_LIB
15 
16 #include "lua.h"
17 
18 #include "lauxlib.h"
19 #include "lualib.h"
20 
21 
22 /*
23 ** maximum number of captures that a pattern can do during
24 ** pattern-matching. This limit is arbitrary.
25 */
26 #if !defined(LUA_MAXCAPTURES)
27 #define LUA_MAXCAPTURES 32
28 #endif
29 
30 
31 /* macro to `unsign' a character */
32 #define uchar(c) ((unsigned char)(c))
33 
34 
35 
36 static int str_len (lua_State *L) {
37  size_t l;
38  luaL_checklstring(L, 1, &l);
40  return 1;
41 }
42 
43 
44 /* translate a relative string position: negative means back from end */
45 static size_t posrelat (ptrdiff_t pos, size_t len) {
46  if (pos >= 0) return (size_t)pos;
47  else if (0u - (size_t)pos > len) return 0;
48  else return len - ((size_t)-pos) + 1;
49 }
50 
51 
52 static int str_sub (lua_State *L) {
53  size_t l;
54  const char *s = luaL_checklstring(L, 1, &l);
55  size_t start = posrelat(luaL_checkinteger(L, 2), l);
56  size_t end = posrelat(luaL_optinteger(L, 3, -1), l);
57  if (start < 1) start = 1;
58  if (end > l) end = l;
59  if (start <= end)
60  lua_pushlstring(L, s + start - 1, end - start + 1);
61  else lua_pushliteral(L, "");
62  return 1;
63 }
64 
65 
66 static int str_reverse (lua_State *L) {
67  size_t l, i;
68  luaL_Buffer b;
69  const char *s = luaL_checklstring(L, 1, &l);
70  char *p = luaL_buffinitsize(L, &b, l);
71  for (i = 0; i < l; i++)
72  p[i] = s[l - i - 1];
73  luaL_pushresultsize(&b, l);
74  return 1;
75 }
76 
77 
78 static int str_lower (lua_State *L) {
79  size_t l;
80  size_t i;
81  luaL_Buffer b;
82  const char *s = luaL_checklstring(L, 1, &l);
83  char *p = luaL_buffinitsize(L, &b, l);
84  for (i=0; i<l; i++)
85  p[i] = tolower(uchar(s[i]));
86  luaL_pushresultsize(&b, l);
87  return 1;
88 }
89 
90 
91 static int str_upper (lua_State *L) {
92  size_t l;
93  size_t i;
94  luaL_Buffer b;
95  const char *s = luaL_checklstring(L, 1, &l);
96  char *p = luaL_buffinitsize(L, &b, l);
97  for (i=0; i<l; i++)
98  p[i] = toupper(uchar(s[i]));
99  luaL_pushresultsize(&b, l);
100  return 1;
101 }
102 
103 
104 /* reasonable limit to avoid arithmetic overflow */
105 #define MAXSIZE ((~(size_t)0) >> 1)
106 
107 static int str_rep (lua_State *L) {
108  size_t l, lsep;
109  const char *s = luaL_checklstring(L, 1, &l);
110  int n = luaL_checkint(L, 2);
111  const char *sep = luaL_optlstring(L, 3, "", &lsep);
112  if (n <= 0) lua_pushliteral(L, "");
113  else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */
114  return luaL_error(L, "resulting string too large");
115  else {
116  size_t totallen = n * l + (n - 1) * lsep;
117  luaL_Buffer b;
118  char *p = luaL_buffinitsize(L, &b, totallen);
119  while (n-- > 1) { /* first n-1 copies (followed by separator) */
120  memcpy(p, s, l * sizeof(char)); p += l;
121  if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */
122  memcpy(p, sep, lsep * sizeof(char)); p += lsep;
123  }
124  }
125  memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
126  luaL_pushresultsize(&b, totallen);
127  }
128  return 1;
129 }
130 
131 
132 static int str_byte (lua_State *L) {
133  size_t l;
134  const char *s = luaL_checklstring(L, 1, &l);
135  size_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
136  size_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
137  int n, i;
138  if (posi < 1) posi = 1;
139  if (pose > l) pose = l;
140  if (posi > pose) return 0; /* empty interval; return no values */
141  n = (int)(pose - posi + 1);
142  if (posi + n <= pose) /* (size_t -> int) overflow? */
143  return luaL_error(L, "string slice too long");
144  luaL_checkstack(L, n, "string slice too long");
145  for (i=0; i<n; i++)
146  lua_pushinteger(L, uchar(s[posi+i-1]));
147  return n;
148 }
149 
150 
151 static int str_char (lua_State *L) {
152  int n = lua_gettop(L); /* number of arguments */
153  int i;
154  luaL_Buffer b;
155  char *p = luaL_buffinitsize(L, &b, n);
156  for (i=1; i<=n; i++) {
157  int c = luaL_checkint(L, i);
158  luaL_argcheck(L, uchar(c) == c, i, "value out of range");
159  p[i - 1] = uchar(c);
160  }
161  luaL_pushresultsize(&b, n);
162  return 1;
163 }
164 
165 
166 static int writer (lua_State *L, const void* b, size_t size, void* B) {
167  (void)L;
168  luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
169  return 0;
170 }
171 
172 
173 static int str_dump (lua_State *L) {
174  luaL_Buffer b;
176  lua_settop(L, 1);
177  luaL_buffinit(L,&b);
178  if (lua_dump(L, writer, &b) != 0)
179  return luaL_error(L, "unable to dump given function");
180  luaL_pushresult(&b);
181  return 1;
182 }
183 
184 
185 
186 /*
187 ** {======================================================
188 ** PATTERN MATCHING
189 ** =======================================================
190 */
191 
192 
193 #define CAP_UNFINISHED (-1)
194 #define CAP_POSITION (-2)
195 
196 
197 typedef struct MatchState {
198  int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
199  const char *src_init; /* init of source string */
200  const char *src_end; /* end ('\0') of source string */
201  const char *p_end; /* end ('\0') of pattern */
203  int level; /* total number of captures (finished or unfinished) */
204  struct {
205  const char *init;
206  ptrdiff_t len;
208 } MatchState;
209 
210 
211 /* recursive function */
212 static const char *match (MatchState *ms, const char *s, const char *p);
213 
214 
215 /* maximum recursion depth for 'match' */
216 #if !defined(MAXCCALLS)
217 #define MAXCCALLS 200
218 #endif
219 
220 
221 #define L_ESC '%'
222 #define SPECIALS "^$*+?.([%-"
223 
224 
225 static int check_capture (MatchState *ms, int l) {
226  l -= '1';
227  if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
228  return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
229  return l;
230 }
231 
232 
233 static int capture_to_close (MatchState *ms) {
234  int level = ms->level;
235  for (level--; level>=0; level--)
236  if (ms->capture[level].len == CAP_UNFINISHED) return level;
237  return luaL_error(ms->L, "invalid pattern capture");
238 }
239 
240 
241 static const char *classend (MatchState *ms, const char *p) {
242  switch (*p++) {
243  case L_ESC: {
244  if (p == ms->p_end)
245  luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
246  return p+1;
247  }
248  case '[': {
249  if (*p == '^') p++;
250  do { /* look for a `]' */
251  if (p == ms->p_end)
252  luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
253  if (*(p++) == L_ESC && p < ms->p_end)
254  p++; /* skip escapes (e.g. `%]') */
255  } while (*p != ']');
256  return p+1;
257  }
258  default: {
259  return p;
260  }
261  }
262 }
263 
264 
265 static int match_class (int c, int cl) {
266  int res;
267  switch (tolower(cl)) {
268  case 'a' : res = isalpha(c); break;
269  case 'c' : res = iscntrl(c); break;
270  case 'd' : res = isdigit(c); break;
271  case 'g' : res = isgraph(c); break;
272  case 'l' : res = islower(c); break;
273  case 'p' : res = ispunct(c); break;
274  case 's' : res = isspace(c); break;
275  case 'u' : res = isupper(c); break;
276  case 'w' : res = isalnum(c); break;
277  case 'x' : res = isxdigit(c); break;
278  case 'z' : res = (c == 0); break; /* deprecated option */
279  default: return (cl == c);
280  }
281  return (islower(cl) ? res : !res);
282 }
283 
284 
285 static int matchbracketclass (int c, const char *p, const char *ec) {
286  int sig = 1;
287  if (*(p+1) == '^') {
288  sig = 0;
289  p++; /* skip the `^' */
290  }
291  while (++p < ec) {
292  if (*p == L_ESC) {
293  p++;
294  if (match_class(c, uchar(*p)))
295  return sig;
296  }
297  else if ((*(p+1) == '-') && (p+2 < ec)) {
298  p+=2;
299  if (uchar(*(p-2)) <= c && c <= uchar(*p))
300  return sig;
301  }
302  else if (uchar(*p) == c) return sig;
303  }
304  return !sig;
305 }
306 
307 
308 static int singlematch (MatchState *ms, const char *s, const char *p,
309  const char *ep) {
310  if (s >= ms->src_end)
311  return 0;
312  else {
313  int c = uchar(*s);
314  switch (*p) {
315  case '.': return 1; /* matches any char */
316  case L_ESC: return match_class(c, uchar(*(p+1)));
317  case '[': return matchbracketclass(c, p, ep-1);
318  default: return (uchar(*p) == c);
319  }
320  }
321 }
322 
323 
324 static const char *matchbalance (MatchState *ms, const char *s,
325  const char *p) {
326  if (p >= ms->p_end - 1)
327  luaL_error(ms->L, "malformed pattern "
328  "(missing arguments to " LUA_QL("%%b") ")");
329  if (*s != *p) return NULL;
330  else {
331  int b = *p;
332  int e = *(p+1);
333  int cont = 1;
334  while (++s < ms->src_end) {
335  if (*s == e) {
336  if (--cont == 0) return s+1;
337  }
338  else if (*s == b) cont++;
339  }
340  }
341  return NULL; /* string ends out of balance */
342 }
343 
344 
345 static const char *max_expand (MatchState *ms, const char *s,
346  const char *p, const char *ep) {
347  ptrdiff_t i = 0; /* counts maximum expand for item */
348  while (singlematch(ms, s + i, p, ep))
349  i++;
350  /* keeps trying to match with the maximum repetitions */
351  while (i>=0) {
352  const char *res = match(ms, (s+i), ep+1);
353  if (res) return res;
354  i--; /* else didn't match; reduce 1 repetition to try again */
355  }
356  return NULL;
357 }
358 
359 
360 static const char *min_expand (MatchState *ms, const char *s,
361  const char *p, const char *ep) {
362  for (;;) {
363  const char *res = match(ms, s, ep+1);
364  if (res != NULL)
365  return res;
366  else if (singlematch(ms, s, p, ep))
367  s++; /* try with one more repetition */
368  else return NULL;
369  }
370 }
371 
372 
373 static const char *start_capture (MatchState *ms, const char *s,
374  const char *p, int what) {
375  const char *res;
376  int level = ms->level;
377  if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
378  ms->capture[level].init = s;
379  ms->capture[level].len = what;
380  ms->level = level+1;
381  if ((res=match(ms, s, p)) == NULL) /* match failed? */
382  ms->level--; /* undo capture */
383  return res;
384 }
385 
386 
387 static const char *end_capture (MatchState *ms, const char *s,
388  const char *p) {
389  int l = capture_to_close(ms);
390  const char *res;
391  ms->capture[l].len = s - ms->capture[l].init; /* close capture */
392  if ((res = match(ms, s, p)) == NULL) /* match failed? */
393  ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
394  return res;
395 }
396 
397 
398 static const char *match_capture (MatchState *ms, const char *s, int l) {
399  size_t len;
400  l = check_capture(ms, l);
401  len = ms->capture[l].len;
402  if ((size_t)(ms->src_end-s) >= len &&
403  memcmp(ms->capture[l].init, s, len) == 0)
404  return s+len;
405  else return NULL;
406 }
407 
408 
409 static const char *match (MatchState *ms, const char *s, const char *p) {
410  if (ms->matchdepth-- == 0)
411  luaL_error(ms->L, "pattern too complex");
412  init: /* using goto's to optimize tail recursion */
413  if (p != ms->p_end) { /* end of pattern? */
414  switch (*p) {
415  case '(': { /* start capture */
416  if (*(p + 1) == ')') /* position capture? */
417  s = start_capture(ms, s, p + 2, CAP_POSITION);
418  else
419  s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
420  break;
421  }
422  case ')': { /* end capture */
423  s = end_capture(ms, s, p + 1);
424  break;
425  }
426  case '$': {
427  if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */
428  goto dflt; /* no; go to default */
429  s = (s == ms->src_end) ? s : NULL; /* check end of string */
430  break;
431  }
432  case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
433  switch (*(p + 1)) {
434  case 'b': { /* balanced string? */
435  s = matchbalance(ms, s, p + 2);
436  if (s != NULL) {
437  p += 4; goto init; /* return match(ms, s, p + 4); */
438  } /* else fail (s == NULL) */
439  break;
440  }
441  case 'f': { /* frontier? */
442  const char *ep; char previous;
443  p += 2;
444  if (*p != '[')
445  luaL_error(ms->L, "missing " LUA_QL("[") " after "
446  LUA_QL("%%f") " in pattern");
447  ep = classend(ms, p); /* points to what is next */
448  previous = (s == ms->src_init) ? '\0' : *(s - 1);
449  if (!matchbracketclass(uchar(previous), p, ep - 1) &&
450  matchbracketclass(uchar(*s), p, ep - 1)) {
451  p = ep; goto init; /* return match(ms, s, ep); */
452  }
453  s = NULL; /* match failed */
454  break;
455  }
456  case '0': case '1': case '2': case '3':
457  case '4': case '5': case '6': case '7':
458  case '8': case '9': { /* capture results (%0-%9)? */
459  s = match_capture(ms, s, uchar(*(p + 1)));
460  if (s != NULL) {
461  p += 2; goto init; /* return match(ms, s, p + 2) */
462  }
463  break;
464  }
465  default: goto dflt;
466  }
467  break;
468  }
469  default: dflt: { /* pattern class plus optional suffix */
470  const char *ep = classend(ms, p); /* points to optional suffix */
471  /* does not match at least once? */
472  if (!singlematch(ms, s, p, ep)) {
473  if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
474  p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
475  }
476  else /* '+' or no suffix */
477  s = NULL; /* fail */
478  }
479  else { /* matched once */
480  switch (*ep) { /* handle optional suffix */
481  case '?': { /* optional */
482  const char *res;
483  if ((res = match(ms, s + 1, ep + 1)) != NULL)
484  s = res;
485  else {
486  p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
487  }
488  break;
489  }
490  case '+': /* 1 or more repetitions */
491  s++; /* 1 match already done */
492  /* go through */
493  case '*': /* 0 or more repetitions */
494  s = max_expand(ms, s, p, ep);
495  break;
496  case '-': /* 0 or more repetitions (minimum) */
497  s = min_expand(ms, s, p, ep);
498  break;
499  default: /* no suffix */
500  s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
501  }
502  }
503  break;
504  }
505  }
506  }
507  ms->matchdepth++;
508  return s;
509 }
510 
511 
512 
513 static const char *lmemfind (const char *s1, size_t l1,
514  const char *s2, size_t l2) {
515  if (l2 == 0) return s1; /* empty strings are everywhere */
516  else if (l2 > l1) return NULL; /* avoids a negative `l1' */
517  else {
518  const char *init; /* to search for a `*s2' inside `s1' */
519  l2--; /* 1st char will be checked by `memchr' */
520  l1 = l1-l2; /* `s2' cannot be found after that */
521  while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
522  init++; /* 1st char is already checked */
523  if (memcmp(init, s2+1, l2) == 0)
524  return init-1;
525  else { /* correct `l1' and `s1' to try again */
526  l1 -= init-s1;
527  s1 = init;
528  }
529  }
530  return NULL; /* not found */
531  }
532 }
533 
534 
535 static void push_onecapture (MatchState *ms, int i, const char *s,
536  const char *e) {
537  if (i >= ms->level) {
538  if (i == 0) /* ms->level == 0, too */
539  lua_pushlstring(ms->L, s, e - s); /* add whole match */
540  else
541  luaL_error(ms->L, "invalid capture index");
542  }
543  else {
544  ptrdiff_t l = ms->capture[i].len;
545  if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
546  if (l == CAP_POSITION)
547  lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
548  else
549  lua_pushlstring(ms->L, ms->capture[i].init, l);
550  }
551 }
552 
553 
554 static int push_captures (MatchState *ms, const char *s, const char *e) {
555  int i;
556  int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
557  luaL_checkstack(ms->L, nlevels, "too many captures");
558  for (i = 0; i < nlevels; i++)
559  push_onecapture(ms, i, s, e);
560  return nlevels; /* number of strings pushed */
561 }
562 
563 
564 /* check whether pattern has no special characters */
565 static int nospecials (const char *p, size_t l) {
566  size_t upto = 0;
567  do {
568  if (strpbrk(p + upto, SPECIALS))
569  return 0; /* pattern has a special character */
570  upto += strlen(p + upto) + 1; /* may have more after \0 */
571  } while (upto <= l);
572  return 1; /* no special chars found */
573 }
574 
575 
576 static int str_find_aux (lua_State *L, int find) {
577  size_t ls, lp;
578  const char *s = luaL_checklstring(L, 1, &ls);
579  const char *p = luaL_checklstring(L, 2, &lp);
580  size_t init = posrelat(luaL_optinteger(L, 3, 1), ls);
581  if (init < 1) init = 1;
582  else if (init > ls + 1) { /* start after string's end? */
583  lua_pushnil(L); /* cannot find anything */
584  return 1;
585  }
586  /* explicit request or no special characters? */
587  if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
588  /* do a plain search */
589  const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);
590  if (s2) {
591  lua_pushinteger(L, s2 - s + 1);
592  lua_pushinteger(L, s2 - s + lp);
593  return 2;
594  }
595  }
596  else {
597  MatchState ms;
598  const char *s1 = s + init - 1;
599  int anchor = (*p == '^');
600  if (anchor) {
601  p++; lp--; /* skip anchor character */
602  }
603  ms.L = L;
604  ms.matchdepth = MAXCCALLS;
605  ms.src_init = s;
606  ms.src_end = s + ls;
607  ms.p_end = p + lp;
608  do {
609  const char *res;
610  ms.level = 0;
612  if ((res=match(&ms, s1, p)) != NULL) {
613  if (find) {
614  lua_pushinteger(L, s1 - s + 1); /* start */
615  lua_pushinteger(L, res - s); /* end */
616  return push_captures(&ms, NULL, 0) + 2;
617  }
618  else
619  return push_captures(&ms, s1, res);
620  }
621  } while (s1++ < ms.src_end && !anchor);
622  }
623  lua_pushnil(L); /* not found */
624  return 1;
625 }
626 
627 
628 static int str_find (lua_State *L) {
629  return str_find_aux(L, 1);
630 }
631 
632 
633 static int str_match (lua_State *L) {
634  return str_find_aux(L, 0);
635 }
636 
637 
638 static int gmatch_aux (lua_State *L) {
639  MatchState ms;
640  size_t ls, lp;
641  const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
642  const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
643  const char *src;
644  ms.L = L;
645  ms.matchdepth = MAXCCALLS;
646  ms.src_init = s;
647  ms.src_end = s+ls;
648  ms.p_end = p + lp;
649  for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
650  src <= ms.src_end;
651  src++) {
652  const char *e;
653  ms.level = 0;
655  if ((e = match(&ms, src, p)) != NULL) {
656  lua_Integer newstart = e-s;
657  if (e == src) newstart++; /* empty match? go at least one position */
658  lua_pushinteger(L, newstart);
660  return push_captures(&ms, src, e);
661  }
662  }
663  return 0; /* not found */
664 }
665 
666 
667 static int gmatch (lua_State *L) {
668  luaL_checkstring(L, 1);
669  luaL_checkstring(L, 2);
670  lua_settop(L, 2);
671  lua_pushinteger(L, 0);
673  return 1;
674 }
675 
676 
677 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
678  const char *e) {
679  size_t l, i;
680  const char *news = lua_tolstring(ms->L, 3, &l);
681  for (i = 0; i < l; i++) {
682  if (news[i] != L_ESC)
683  luaL_addchar(b, news[i]);
684  else {
685  i++; /* skip ESC */
686  if (!isdigit(uchar(news[i]))) {
687  if (news[i] != L_ESC)
688  luaL_error(ms->L, "invalid use of " LUA_QL("%c")
689  " in replacement string", L_ESC);
690  luaL_addchar(b, news[i]);
691  }
692  else if (news[i] == '0')
693  luaL_addlstring(b, s, e - s);
694  else {
695  push_onecapture(ms, news[i] - '1', s, e);
696  luaL_addvalue(b); /* add capture to accumulated result */
697  }
698  }
699  }
700 }
701 
702 
703 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
704  const char *e, int tr) {
705  lua_State *L = ms->L;
706  switch (tr) {
707  case LUA_TFUNCTION: {
708  int n;
709  lua_pushvalue(L, 3);
710  n = push_captures(ms, s, e);
711  lua_call(L, n, 1);
712  break;
713  }
714  case LUA_TTABLE: {
715  push_onecapture(ms, 0, s, e);
716  lua_gettable(L, 3);
717  break;
718  }
719  default: { /* LUA_TNUMBER or LUA_TSTRING */
720  add_s(ms, b, s, e);
721  return;
722  }
723  }
724  if (!lua_toboolean(L, -1)) { /* nil or false? */
725  lua_pop(L, 1);
726  lua_pushlstring(L, s, e - s); /* keep original text */
727  }
728  else if (!lua_isstring(L, -1))
729  luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
730  luaL_addvalue(b); /* add result to accumulator */
731 }
732 
733 
734 static int str_gsub (lua_State *L) {
735  size_t srcl, lp;
736  const char *src = luaL_checklstring(L, 1, &srcl);
737  const char *p = luaL_checklstring(L, 2, &lp);
738  int tr = lua_type(L, 3);
739  size_t max_s = luaL_optinteger(L, 4, srcl+1);
740  int anchor = (*p == '^');
741  size_t n = 0;
742  MatchState ms;
743  luaL_Buffer b;
744  luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
745  tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
746  "string/function/table expected");
747  luaL_buffinit(L, &b);
748  if (anchor) {
749  p++; lp--; /* skip anchor character */
750  }
751  ms.L = L;
752  ms.matchdepth = MAXCCALLS;
753  ms.src_init = src;
754  ms.src_end = src+srcl;
755  ms.p_end = p + lp;
756  while (n < max_s) {
757  const char *e;
758  ms.level = 0;
760  e = match(&ms, src, p);
761  if (e) {
762  n++;
763  add_value(&ms, &b, src, e, tr);
764  }
765  if (e && e>src) /* non empty match? */
766  src = e; /* skip it */
767  else if (src < ms.src_end)
768  luaL_addchar(&b, *src++);
769  else break;
770  if (anchor) break;
771  }
772  luaL_addlstring(&b, src, ms.src_end-src);
773  luaL_pushresult(&b);
774  lua_pushinteger(L, n); /* number of substitutions */
775  return 2;
776 }
777 
778 /* }====================================================== */
779 
780 
781 
782 /*
783 ** {======================================================
784 ** STRING FORMAT
785 ** =======================================================
786 */
787 
788 /*
789 ** LUA_INTFRMLEN is the length modifier for integer conversions in
790 ** 'string.format'; LUA_INTFRM_T is the integer type corresponding to
791 ** the previous length
792 */
793 #if !defined(LUA_INTFRMLEN) /* { */
794 #if defined(LUA_USE_LONGLONG)
795 
796 #define LUA_INTFRMLEN "ll"
797 #define LUA_INTFRM_T long long
798 
799 #else
800 
801 #define LUA_INTFRMLEN "l"
802 #define LUA_INTFRM_T long
803 
804 #endif
805 #endif /* } */
806 
807 
808 /*
809 ** LUA_FLTFRMLEN is the length modifier for float conversions in
810 ** 'string.format'; LUA_FLTFRM_T is the float type corresponding to
811 ** the previous length
812 */
813 #if !defined(LUA_FLTFRMLEN)
814 
815 #define LUA_FLTFRMLEN ""
816 #define LUA_FLTFRM_T double
817 
818 #endif
819 
820 
821 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
822 #define MAX_ITEM 512
823 /* valid flags in a format specification */
824 #define FLAGS "-+ #0"
825 /*
826 ** maximum size of each format specification (such as '%-099.99d')
827 ** (+10 accounts for %99.99x plus margin of error)
828 */
829 #define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
830 
831 
832 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
833  size_t l;
834  const char *s = luaL_checklstring(L, arg, &l);
835  luaL_addchar(b, '"');
836  while (l--) {
837  if (*s == '"' || *s == '\\' || *s == '\n') {
838  luaL_addchar(b, '\\');
839  luaL_addchar(b, *s);
840  }
841  else if (*s == '\0' || iscntrl(uchar(*s))) {
842  char buff[10];
843  if (!isdigit(uchar(*(s+1))))
844  sprintf(buff, "\\%d", (int)uchar(*s));
845  else
846  sprintf(buff, "\\%03d", (int)uchar(*s));
847  luaL_addstring(b, buff);
848  }
849  else
850  luaL_addchar(b, *s);
851  s++;
852  }
853  luaL_addchar(b, '"');
854 }
855 
856 static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
857  const char *p = strfrmt;
858  while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
859  if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
860  luaL_error(L, "invalid format (repeated flags)");
861  if (isdigit(uchar(*p))) p++; /* skip width */
862  if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
863  if (*p == '.') {
864  p++;
865  if (isdigit(uchar(*p))) p++; /* skip precision */
866  if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
867  }
868  if (isdigit(uchar(*p)))
869  luaL_error(L, "invalid format (width or precision too long)");
870  *(form++) = '%';
871  memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
872  form += p - strfrmt + 1;
873  *form = '\0';
874  return p;
875 }
876 
877 
878 /*
879 ** add length modifier into formats
880 */
881 static void addlenmod (char *form, const char *lenmod) {
882  size_t l = strlen(form);
883  size_t lm = strlen(lenmod);
884  char spec = form[l - 1];
885  strcpy(form + l - 1, lenmod);
886  form[l + lm - 1] = spec;
887  form[l + lm] = '\0';
888 }
889 
890 
891 static int str_format (lua_State *L) {
892  int top = lua_gettop(L);
893  int arg = 1;
894  size_t sfl;
895  const char *strfrmt = luaL_checklstring(L, arg, &sfl);
896  const char *strfrmt_end = strfrmt+sfl;
897  luaL_Buffer b;
898  luaL_buffinit(L, &b);
899  while (strfrmt < strfrmt_end) {
900  if (*strfrmt != L_ESC)
901  luaL_addchar(&b, *strfrmt++);
902  else if (*++strfrmt == L_ESC)
903  luaL_addchar(&b, *strfrmt++); /* %% */
904  else { /* format item */
905  char form[MAX_FORMAT]; /* to store the format (`%...') */
906  char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
907  int nb = 0; /* number of bytes in added item */
908  if (++arg > top)
909  luaL_argerror(L, arg, "no value");
910  strfrmt = scanformat(L, strfrmt, form);
911  switch (*strfrmt++) {
912  case 'c': {
913  nb = sprintf(buff, form, luaL_checkint(L, arg));
914  break;
915  }
916  case 'd': case 'i': {
917  lua_Number n = luaL_checknumber(L, arg);
918  LUA_INTFRM_T ni = (LUA_INTFRM_T)n;
919  lua_Number diff = n - (lua_Number)ni;
920  luaL_argcheck(L, -1 < diff && diff < 1, arg,
921  "not a number in proper range");
922  addlenmod(form, LUA_INTFRMLEN);
923  nb = sprintf(buff, form, ni);
924  break;
925  }
926  case 'o': case 'u': case 'x': case 'X': {
927  lua_Number n = luaL_checknumber(L, arg);
928  unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n;
929  lua_Number diff = n - (lua_Number)ni;
930  luaL_argcheck(L, -1 < diff && diff < 1, arg,
931  "not a non-negative number in proper range");
932  addlenmod(form, LUA_INTFRMLEN);
933  nb = sprintf(buff, form, ni);
934  break;
935  }
936  case 'e': case 'E': case 'f':
937 #if defined(LUA_USE_AFORMAT)
938  case 'a': case 'A':
939 #endif
940  case 'g': case 'G': {
941  addlenmod(form, LUA_FLTFRMLEN);
942  nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
943  break;
944  }
945  case 'q': {
946  addquoted(L, &b, arg);
947  break;
948  }
949  case 's': {
950  size_t l;
951  const char *s = luaL_tolstring(L, arg, &l);
952  if (!strchr(form, '.') && l >= 100) {
953  /* no precision and string is too long to be formatted;
954  keep original string */
955  luaL_addvalue(&b);
956  break;
957  }
958  else {
959  nb = sprintf(buff, form, s);
960  lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
961  break;
962  }
963  }
964  default: { /* also treat cases `pnLlh' */
965  return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
966  LUA_QL("format"), *(strfrmt - 1));
967  }
968  }
969  luaL_addsize(&b, nb);
970  }
971  }
972  luaL_pushresult(&b);
973  return 1;
974 }
975 
976 /* }====================================================== */
977 
978 
979 static const luaL_Reg strlib[] = {
980  {"byte", str_byte},
981  {"char", str_char},
982  {"dump", str_dump},
983  {"find", str_find},
984  {"format", str_format},
985  {"gmatch", gmatch},
986  {"gsub", str_gsub},
987  {"len", str_len},
988  {"lower", str_lower},
989  {"match", str_match},
990  {"rep", str_rep},
991  {"reverse", str_reverse},
992  {"sub", str_sub},
993  {"upper", str_upper},
994  {NULL, NULL}
995 };
996 
997 
998 static void createmetatable (lua_State *L) {
999  lua_createtable(L, 0, 1); /* table to be metatable for strings */
1000  lua_pushliteral(L, ""); /* dummy string */
1001  lua_pushvalue(L, -2); /* copy table */
1002  lua_setmetatable(L, -2); /* set table as metatable for strings */
1003  lua_pop(L, 1); /* pop dummy string */
1004  lua_pushvalue(L, -2); /* get string library */
1005  lua_setfield(L, -2, "__index"); /* metatable.__index = string */
1006  lua_pop(L, 1); /* pop metatable */
1007 }
1008 
1009 
1010 /*
1011 ** Open string library
1012 */
1014  luaL_newlib(L, strlib);
1015  createmetatable(L);
1016  return 1;
1017 }
1018 
static const char * lmemfind(const char *s1, size_t l1, const char *s2, size_t l2)
Definition: lstrlib.cpp:513
#define luaL_addsize(B, s)
Definition: lauxlib.h:156
static int nospecials(const char *p, size_t l)
Definition: lstrlib.cpp:565
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
Definition: lauxlib.cpp:487
static int str_dump(lua_State *L)
Definition: lstrlib.cpp:173
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:667
static int str_match(lua_State *L)
Definition: lstrlib.cpp:633
static int str_reverse(lua_State *L)
Definition: lstrlib.cpp:66
LUA_API void lua_replace(lua_State *L, int idx)
Definition: lapi.cpp:211
static int check_capture(MatchState *ms, int l)
Definition: lstrlib.cpp:225
static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
Definition: lstrlib.cpp:535
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
Definition: lauxlib.cpp:391
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
LUALIB_API const char * luaL_tolstring(lua_State *L, int idx, size_t *len)
Definition: lauxlib.cpp:739
static int str_gsub(lua_State *L)
Definition: lstrlib.cpp:734
static int matchbracketclass(int c, const char *p, const char *ec)
Definition: lstrlib.cpp:285
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.cpp:159
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:243
LUALIB_API char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
Definition: lauxlib.cpp:439
static const char * matchbalance(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.cpp:324
GLint level
Definition: glew.h:1220
LUALIB_API const char * luaL_optlstring(lua_State *L, int narg, const char *def, size_t *len)
Definition: lauxlib.cpp:366
static void add_value(MatchState *ms, luaL_Buffer *b, const char *s, const char *e, int tr)
Definition: lstrlib.cpp:703
const GLfloat * c
Definition: glew.h:12741
int pos
Definition: formula.cpp:800
static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
Definition: lstrlib.cpp:677
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
Definition: lauxlib.cpp:347
#define uchar(c)
Definition: lstrlib.cpp:32
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
Definition: lauxlib.cpp:473
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:154
static const char * end_capture(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.cpp:387
static int str_char(lua_State *L)
Definition: lstrlib.cpp:151
#define lua_tointeger(L, i)
Definition: lua.h:319
#define MAX_ITEM
Definition: lstrlib.cpp:822
const char * what() const
#define CAP_POSITION
Definition: lstrlib.cpp:194
#define luaL_typename(L, i)
Definition: lauxlib.h:122
LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
Definition: lapi.cpp:991
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *msg)
Definition: lauxlib.cpp:335
GLenum src
Definition: glew.h:2392
#define LUA_TFUNCTION
Definition: lua.h:83
LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)
Definition: lauxlib.cpp:468
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
Definition: lapi.cpp:549
#define luaL_argcheck(L, cond, numarg, extramsg)
Definition: lauxlib.h:113
static int str_rep(lua_State *L)
Definition: lstrlib.cpp:107
static int str_byte(lua_State *L)
Definition: lstrlib.cpp:132
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
Definition: lauxlib.cpp:409
#define LUA_FLTFRMLEN
Definition: lstrlib.cpp:815
#define lua_pop(L, n)
Definition: lua.h:322
GLdouble l
Definition: glew.h:6966
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
LUALIB_API void luaL_pushresultsize(luaL_Buffer *B, size_t sz)
Definition: lauxlib.cpp:481
static int str_format(lua_State *L)
Definition: lstrlib.cpp:891
#define lua_upvalueindex(i)
Definition: lua.h:40
LUA_INTEGER lua_Integer
Definition: lua.h:106
#define LUA_TSTRING
Definition: lua.h:81
const char * src_init
Definition: lstrlib.cpp:199
GLuint GLuint end
Definition: glew.h:1221
static int str_lower(lua_State *L)
Definition: lstrlib.cpp:78
static int gmatch_aux(lua_State *L)
Definition: lstrlib.cpp:638
LUAMOD_API int luaopen_string(lua_State *L)
Definition: lstrlib.cpp:1013
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:268
static const char * classend(MatchState *ms, const char *p)
Definition: lstrlib.cpp:241
#define LUA_QL(x)
Definition: luaconf.h:198
static int push_captures(MatchState *ms, const char *s, const char *e)
Definition: lstrlib.cpp:554
#define LUA_INTFRM_T
Definition: lstrlib.cpp:802
#define MAXCCALLS
Definition: lstrlib.cpp:217
LUA_API int lua_toboolean(lua_State *L, int idx)
Definition: lapi.cpp:377
static int capture_to_close(MatchState *ms)
Definition: lstrlib.cpp:233
#define FLAGS
Definition: lstrlib.cpp:824
GLenum GLsizei len
Definition: glew.h:5662
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
Definition: lauxlib.cpp:498
GLuint start
Definition: glew.h:1221
lua_State * L
Definition: lstrlib.cpp:202
static void addquoted(lua_State *L, luaL_Buffer *b, int arg)
Definition: lstrlib.cpp:832
ptrdiff_t len
Definition: lstrlib.cpp:206
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
Definition: lapi.cpp:495
#define LUA_TNUMBER
Definition: lua.h:80
static const char * max_expand(MatchState *ms, const char *s, const char *p, const char *ep)
Definition: lstrlib.cpp:345
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
Definition: lauxlib.cpp:377
static size_t posrelat(ptrdiff_t pos, size_t len)
Definition: lstrlib.cpp:45
LUA_API int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:806
#define LUA_INTFRMLEN
Definition: lstrlib.cpp:801
static int singlematch(MatchState *ms, const char *s, const char *p, const char *ep)
Definition: lstrlib.cpp:308
LUA_API const char * lua_tolstring(lua_State *L, int idx, size_t *len)
Definition: lapi.cpp:383
#define luaL_addchar(B, c)
Definition: lauxlib.h:152
GLfloat GLfloat p
Definition: glew.h:12766
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void init()
Initializes the gui subsystems.
Definition: registry.cpp:482
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
Definition: lauxlib.cpp:461
LUA_API void lua_pushnil(lua_State *L)
Definition: lapi.cpp:459
static const char * start_capture(MatchState *ms, const char *s, const char *p, int what)
Definition: lstrlib.cpp:373
LUALIB_API const char * luaL_checklstring(lua_State *L, int narg, size_t *len)
Definition: lauxlib.cpp:359
#define lua_pushliteral(L, s)
Definition: lua.h:339
static int str_find(lua_State *L)
Definition: lstrlib.cpp:628
static const char * scanformat(lua_State *L, const char *strfrmt, char *form)
Definition: lstrlib.cpp:856
GLuint res
Definition: glew.h:9258
#define CAP_UNFINISHED
Definition: lstrlib.cpp:193
#define lua_assert(c)
Definition: llimits.h:65
#define MAX_FORMAT
Definition: lstrlib.cpp:829
static int str_len(lua_State *L)
Definition: lstrlib.cpp:36
static int str_find_aux(lua_State *L, int find)
Definition: lstrlib.cpp:576
#define LUAMOD_API
Definition: luaconf.h:163
static const char * match_capture(MatchState *ms, const char *s, int l)
Definition: lstrlib.cpp:398
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
size_t i
Definition: function.cpp:1057
#define SPECIALS
Definition: lstrlib.cpp:222
struct MatchState::@19 capture[LUA_MAXCAPTURES]
static int gmatch(lua_State *L)
Definition: lstrlib.cpp:667
static int writer(lua_State *L, const void *b, size_t size, void *B)
Definition: lstrlib.cpp:166
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:229
#define luaL_newlib(L, l)
Definition: lauxlib.h:111
static int str_upper(lua_State *L)
Definition: lstrlib.cpp:91
#define lua_call(L, n, r)
Definition: lua.h:252
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.cpp:198
const char * init
Definition: lstrlib.cpp:205
const char * p_end
Definition: lstrlib.cpp:201
#define LUA_MAXCAPTURES
Definition: lstrlib.cpp:27
LUA_API void lua_gettable(lua_State *L, int idx)
Definition: lapi.cpp:613
int matchdepth
Definition: lstrlib.cpp:198
static void createmetatable(lua_State *L)
Definition: lstrlib.cpp:998
GLsizeiptr size
Definition: glew.h:1649
static const luaL_Reg strlib[]
Definition: lstrlib.cpp:979
GLclampd n
Definition: glew.h:5903
#define luaL_checkint(L, n)
Definition: lauxlib.h:117
LUALIB_API char * luaL_buffinitsize(lua_State *L, luaL_Buffer *B, size_t sz)
Definition: lauxlib.cpp:506
bool find(E event, F functor)
Tests whether an event handler is available.
static int str_sub(lua_State *L)
Definition: lstrlib.cpp:52
static const char * min_expand(MatchState *ms, const char *s, const char *p, const char *ep)
Definition: lstrlib.cpp:360
static const char * match(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.cpp:409
static void addlenmod(char *form, const char *lenmod)
Definition: lstrlib.cpp:881
struct MatchState MatchState
#define e
#define MAXSIZE
Definition: lstrlib.cpp:105
GLdouble s
Definition: glew.h:1358
#define LUA_TTABLE
Definition: lua.h:82
static int match_class(int c, int cl)
Definition: lstrlib.cpp:265
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
const char * src_end
Definition: lstrlib.cpp:200
LUA_NUMBER lua_Number
Definition: lua.h:102
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:752
#define LUA_FLTFRM_T
Definition: lstrlib.cpp:816
#define L_ESC
Definition: lstrlib.cpp:221
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115