1 /*
2 ** $Id: lstrlib.c,v 1.221 2014/12/11 14:03:07 roberto Exp $
3 ** Standard library for string operations and pattern-matching
4 ** See Copyright Notice in lua.h
5 */
6
7 #define lstrlib_c
8 #define LUA_LIB
9
10 #include "lprefix.h"
11
12
13 #include <ctype.h>
14 #include <limits.h>
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "lua.h"
21
22 #include "lauxlib.h"
23 #include "lualib.h"
24
25
26 /*
27 ** maximum number of captures that a pattern can do during
28 ** pattern-matching. This limit is arbitrary.
29 */
30 #if !defined(LUA_MAXCAPTURES)
31 #define LUA_MAXCAPTURES 32
32 #endif
33
34
35 /* macro to 'unsign' a character */
36 #define uchar(c) ((unsigned char)(c))
37
38
39 /*
40 ** Some sizes are better limited to fit in 'int', but must also fit in
41 ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
42 */
43 #define MAXSIZE \
44 (sizeof(size_t) < sizeof(int) ? (~(size_t)0) : (size_t)(INT_MAX))
45
46
47
48
str_len(lua_State * L)49 static int str_len (lua_State *L) {
50 size_t l;
51 luaL_checklstring(L, 1, &l);
52 lua_pushinteger(L, (lua_Integer)l);
53 return 1;
54 }
55
56
57 /* translate a relative string position: negative means back from end */
posrelat(lua_Integer pos,size_t len)58 static lua_Integer posrelat (lua_Integer pos, size_t len) {
59 if (pos >= 0) return pos;
60 else if (0u - (size_t)pos > len) return 0;
61 else return (lua_Integer)len + pos + 1;
62 }
63
64
str_sub(lua_State * L)65 static int str_sub (lua_State *L) {
66 size_t l;
67 const char *s = luaL_checklstring(L, 1, &l);
68 lua_Integer start = posrelat(luaL_checkinteger(L, 2), l);
69 lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l);
70 if (start < 1) start = 1;
71 if (end > (lua_Integer)l) end = l;
72 if (start <= end)
73 lua_pushlstring(L, s + start - 1, (size_t)(end - start + 1));
74 else lua_pushliteral(L, "");
75 return 1;
76 }
77
78
str_reverse(lua_State * L)79 static int str_reverse (lua_State *L) {
80 size_t l, 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] = s[l - i - 1];
86 luaL_pushresultsize(&b, l);
87 return 1;
88 }
89
90
str_lower(lua_State * L)91 static int str_lower (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] = tolower(uchar(s[i]));
99 luaL_pushresultsize(&b, l);
100 return 1;
101 }
102
103
str_upper(lua_State * L)104 static int str_upper (lua_State *L) {
105 size_t l;
106 size_t i;
107 luaL_Buffer b;
108 const char *s = luaL_checklstring(L, 1, &l);
109 char *p = luaL_buffinitsize(L, &b, l);
110 for (i=0; i<l; i++)
111 p[i] = toupper(uchar(s[i]));
112 luaL_pushresultsize(&b, l);
113 return 1;
114 }
115
116
str_rep(lua_State * L)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 = (size_t)n * l + (size_t)(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
str_byte(lua_State * L)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
str_char(lua_State * L)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
writer(lua_State * L,const void * b,size_t size,void * B)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
str_dump(lua_State * L)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
check_capture(MatchState * ms,int l)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
capture_to_close(MatchState * ms)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
classend(MatchState * ms,const char * p)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 '%%')");
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 ']')");
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
match_class(int c,int cl)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
matchbracketclass(int c,const char * p,const char * ec)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
singlematch(MatchState * ms,const char * s,const char * p,const char * ep)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
matchbalance(MatchState * ms,const char * s,const char * p)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 (missing arguments to '%%b')");
340 if (*s != *p) return NULL;
341 else {
342 int b = *p;
343 int e = *(p+1);
344 int cont = 1;
345 while (++s < ms->src_end) {
346 if (*s == e) {
347 if (--cont == 0) return s+1;
348 }
349 else if (*s == b) cont++;
350 }
351 }
352 return NULL; /* string ends out of balance */
353 }
354
355
max_expand(MatchState * ms,const char * s,const char * p,const char * ep)356 static const char *max_expand (MatchState *ms, const char *s,
357 const char *p, const char *ep) {
358 ptrdiff_t i = 0; /* counts maximum expand for item */
359 while (singlematch(ms, s + i, p, ep))
360 i++;
361 /* keeps trying to match with the maximum repetitions */
362 while (i>=0) {
363 const char *res = match(ms, (s+i), ep+1);
364 if (res) return res;
365 i--; /* else didn't match; reduce 1 repetition to try again */
366 }
367 return NULL;
368 }
369
370
min_expand(MatchState * ms,const char * s,const char * p,const char * ep)371 static const char *min_expand (MatchState *ms, const char *s,
372 const char *p, const char *ep) {
373 for (;;) {
374 const char *res = match(ms, s, ep+1);
375 if (res != NULL)
376 return res;
377 else if (singlematch(ms, s, p, ep))
378 s++; /* try with one more repetition */
379 else return NULL;
380 }
381 }
382
383
start_capture(MatchState * ms,const char * s,const char * p,int what)384 static const char *start_capture (MatchState *ms, const char *s,
385 const char *p, int what) {
386 const char *res;
387 int level = ms->level;
388 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
389 ms->capture[level].init = s;
390 ms->capture[level].len = what;
391 ms->level = level+1;
392 if ((res=match(ms, s, p)) == NULL) /* match failed? */
393 ms->level--; /* undo capture */
394 return res;
395 }
396
397
end_capture(MatchState * ms,const char * s,const char * p)398 static const char *end_capture (MatchState *ms, const char *s,
399 const char *p) {
400 int l = capture_to_close(ms);
401 const char *res;
402 ms->capture[l].len = s - ms->capture[l].init; /* close capture */
403 if ((res = match(ms, s, p)) == NULL) /* match failed? */
404 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
405 return res;
406 }
407
408
match_capture(MatchState * ms,const char * s,int l)409 static const char *match_capture (MatchState *ms, const char *s, int l) {
410 size_t len;
411 l = check_capture(ms, l);
412 len = ms->capture[l].len;
413 if ((size_t)(ms->src_end-s) >= len &&
414 memcmp(ms->capture[l].init, s, len) == 0)
415 return s+len;
416 else return NULL;
417 }
418
419
match(MatchState * ms,const char * s,const char * p)420 static const char *match (MatchState *ms, const char *s, const char *p) {
421 if (ms->matchdepth-- == 0)
422 luaL_error(ms->L, "pattern too complex");
423 init: /* using goto's to optimize tail recursion */
424 if (p != ms->p_end) { /* end of pattern? */
425 switch (*p) {
426 case '(': { /* start capture */
427 if (*(p + 1) == ')') /* position capture? */
428 s = start_capture(ms, s, p + 2, CAP_POSITION);
429 else
430 s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
431 break;
432 }
433 case ')': { /* end capture */
434 s = end_capture(ms, s, p + 1);
435 break;
436 }
437 case '$': {
438 if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */
439 goto dflt; /* no; go to default */
440 s = (s == ms->src_end) ? s : NULL; /* check end of string */
441 break;
442 }
443 case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
444 switch (*(p + 1)) {
445 case 'b': { /* balanced string? */
446 s = matchbalance(ms, s, p + 2);
447 if (s != NULL) {
448 p += 4; goto init; /* return match(ms, s, p + 4); */
449 } /* else fail (s == NULL) */
450 break;
451 }
452 case 'f': { /* frontier? */
453 const char *ep; char previous;
454 p += 2;
455 if (*p != '[')
456 luaL_error(ms->L, "missing '[' after '%%f' in pattern");
457 ep = classend(ms, p); /* points to what is next */
458 previous = (s == ms->src_init) ? '\0' : *(s - 1);
459 if (!matchbracketclass(uchar(previous), p, ep - 1) &&
460 matchbracketclass(uchar(*s), p, ep - 1)) {
461 p = ep; goto init; /* return match(ms, s, ep); */
462 }
463 s = NULL; /* match failed */
464 break;
465 }
466 case '0': case '1': case '2': case '3':
467 case '4': case '5': case '6': case '7':
468 case '8': case '9': { /* capture results (%0-%9)? */
469 s = match_capture(ms, s, uchar(*(p + 1)));
470 if (s != NULL) {
471 p += 2; goto init; /* return match(ms, s, p + 2) */
472 }
473 break;
474 }
475 default: goto dflt;
476 }
477 break;
478 }
479 default: dflt: { /* pattern class plus optional suffix */
480 const char *ep = classend(ms, p); /* points to optional suffix */
481 /* does not match at least once? */
482 if (!singlematch(ms, s, p, ep)) {
483 if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
484 p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
485 }
486 else /* '+' or no suffix */
487 s = NULL; /* fail */
488 }
489 else { /* matched once */
490 switch (*ep) { /* handle optional suffix */
491 case '?': { /* optional */
492 const char *res;
493 if ((res = match(ms, s + 1, ep + 1)) != NULL)
494 s = res;
495 else {
496 p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
497 }
498 break;
499 }
500 case '+': /* 1 or more repetitions */
501 s++; /* 1 match already done */
502 /* go through */
503 case '*': /* 0 or more repetitions */
504 s = max_expand(ms, s, p, ep);
505 break;
506 case '-': /* 0 or more repetitions (minimum) */
507 s = min_expand(ms, s, p, ep);
508 break;
509 default: /* no suffix */
510 s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
511 }
512 }
513 break;
514 }
515 }
516 }
517 ms->matchdepth++;
518 return s;
519 }
520
521
522
lmemfind(const char * s1,size_t l1,const char * s2,size_t l2)523 static const char *lmemfind (const char *s1, size_t l1,
524 const char *s2, size_t l2) {
525 if (l2 == 0) return s1; /* empty strings are everywhere */
526 else if (l2 > l1) return NULL; /* avoids a negative 'l1' */
527 else {
528 const char *init; /* to search for a '*s2' inside 's1' */
529 l2--; /* 1st char will be checked by 'memchr' */
530 l1 = l1-l2; /* 's2' cannot be found after that */
531 while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
532 init++; /* 1st char is already checked */
533 if (memcmp(init, s2+1, l2) == 0)
534 return init-1;
535 else { /* correct 'l1' and 's1' to try again */
536 l1 -= init-s1;
537 s1 = init;
538 }
539 }
540 return NULL; /* not found */
541 }
542 }
543
544
push_onecapture(MatchState * ms,int i,const char * s,const char * e)545 static void push_onecapture (MatchState *ms, int i, const char *s,
546 const char *e) {
547 if (i >= ms->level) {
548 if (i == 0) /* ms->level == 0, too */
549 lua_pushlstring(ms->L, s, e - s); /* add whole match */
550 else
551 luaL_error(ms->L, "invalid capture index %%%d", i + 1);
552 }
553 else {
554 ptrdiff_t l = ms->capture[i].len;
555 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
556 if (l == CAP_POSITION)
557 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
558 else
559 lua_pushlstring(ms->L, ms->capture[i].init, l);
560 }
561 }
562
563
push_captures(MatchState * ms,const char * s,const char * e)564 static int push_captures (MatchState *ms, const char *s, const char *e) {
565 int i;
566 int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
567 luaL_checkstack(ms->L, nlevels, "too many captures");
568 for (i = 0; i < nlevels; i++)
569 push_onecapture(ms, i, s, e);
570 return nlevels; /* number of strings pushed */
571 }
572
573
574 /* check whether pattern has no special characters */
nospecials(const char * p,size_t l)575 static int nospecials (const char *p, size_t l) {
576 size_t upto = 0;
577 do {
578 if (strpbrk(p + upto, SPECIALS))
579 return 0; /* pattern has a special character */
580 upto += strlen(p + upto) + 1; /* may have more after \0 */
581 } while (upto <= l);
582 return 1; /* no special chars found */
583 }
584
585
str_find_aux(lua_State * L,int find)586 static int str_find_aux (lua_State *L, int find) {
587 size_t ls, lp;
588 const char *s = luaL_checklstring(L, 1, &ls);
589 const char *p = luaL_checklstring(L, 2, &lp);
590 lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
591 if (init < 1) init = 1;
592 else if (init > (lua_Integer)ls + 1) { /* start after string's end? */
593 lua_pushnil(L); /* cannot find anything */
594 return 1;
595 }
596 /* explicit request or no special characters? */
597 if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
598 /* do a plain search */
599 const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
600 if (s2) {
601 lua_pushinteger(L, s2 - s + 1);
602 lua_pushinteger(L, s2 - s + lp);
603 return 2;
604 }
605 }
606 else {
607 MatchState ms;
608 const char *s1 = s + init - 1;
609 int anchor = (*p == '^');
610 if (anchor) {
611 p++; lp--; /* skip anchor character */
612 }
613 ms.L = L;
614 ms.matchdepth = MAXCCALLS;
615 ms.src_init = s;
616 ms.src_end = s + ls;
617 ms.p_end = p + lp;
618 do {
619 const char *res;
620 ms.level = 0;
621 lua_assert(ms.matchdepth == MAXCCALLS);
622 if ((res=match(&ms, s1, p)) != NULL) {
623 if (find) {
624 lua_pushinteger(L, s1 - s + 1); /* start */
625 lua_pushinteger(L, res - s); /* end */
626 return push_captures(&ms, NULL, 0) + 2;
627 }
628 else
629 return push_captures(&ms, s1, res);
630 }
631 } while (s1++ < ms.src_end && !anchor);
632 }
633 lua_pushnil(L); /* not found */
634 return 1;
635 }
636
637
str_find(lua_State * L)638 static int str_find (lua_State *L) {
639 return str_find_aux(L, 1);
640 }
641
642
str_match(lua_State * L)643 static int str_match (lua_State *L) {
644 return str_find_aux(L, 0);
645 }
646
647
gmatch_aux(lua_State * L)648 static int gmatch_aux (lua_State *L) {
649 MatchState ms;
650 size_t ls, lp;
651 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
652 const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
653 const char *src;
654 ms.L = L;
655 ms.matchdepth = MAXCCALLS;
656 ms.src_init = s;
657 ms.src_end = s+ls;
658 ms.p_end = p + lp;
659 for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
660 src <= ms.src_end;
661 src++) {
662 const char *e;
663 ms.level = 0;
664 lua_assert(ms.matchdepth == MAXCCALLS);
665 if ((e = match(&ms, src, p)) != NULL) {
666 lua_Integer newstart = e-s;
667 if (e == src) newstart++; /* empty match? go at least one position */
668 lua_pushinteger(L, newstart);
669 lua_replace(L, lua_upvalueindex(3));
670 return push_captures(&ms, src, e);
671 }
672 }
673 return 0; /* not found */
674 }
675
676
gmatch(lua_State * L)677 static int gmatch (lua_State *L) {
678 luaL_checkstring(L, 1);
679 luaL_checkstring(L, 2);
680 lua_settop(L, 2);
681 lua_pushinteger(L, 0);
682 lua_pushcclosure(L, gmatch_aux, 3);
683 return 1;
684 }
685
686
add_s(MatchState * ms,luaL_Buffer * b,const char * s,const char * e)687 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
688 const char *e) {
689 size_t l, i;
690 lua_State *L = ms->L;
691 const char *news = lua_tolstring(L, 3, &l);
692 for (i = 0; i < l; i++) {
693 if (news[i] != L_ESC)
694 luaL_addchar(b, news[i]);
695 else {
696 i++; /* skip ESC */
697 if (!isdigit(uchar(news[i]))) {
698 if (news[i] != L_ESC)
699 luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
700 luaL_addchar(b, news[i]);
701 }
702 else if (news[i] == '0')
703 luaL_addlstring(b, s, e - s);
704 else {
705 push_onecapture(ms, news[i] - '1', s, e);
706 luaL_tolstring(L, -1, NULL); /* if number, convert it to string */
707 lua_remove(L, -2); /* remove original value */
708 luaL_addvalue(b); /* add capture to accumulated result */
709 }
710 }
711 }
712 }
713
714
add_value(MatchState * ms,luaL_Buffer * b,const char * s,const char * e,int tr)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
str_gsub(lua_State * L)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 lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1);
752 int anchor = (*p == '^');
753 lua_Integer 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
addquoted(lua_State * L,luaL_Buffer * b,int arg)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
scanformat(lua_State * L,const char * strfrmt,char * form)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 */
addlenmod(char * form,const char * lenmod)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
str_format(lua_State * L)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, (int)luaL_checkinteger(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 #if defined(LUA_USE_AFORMAT)
905 case 'a': case 'A':
906 #endif
907 case 'e': case 'E': case 'f':
908 case 'g': case 'G': {
909 addlenmod(form, LUA_NUMBER_FRMLEN);
910 nb = sprintf(buff, form, luaL_checknumber(L, arg));
911 break;
912 }
913 case 'q': {
914 addquoted(L, &b, arg);
915 break;
916 }
917 case 's': {
918 size_t l;
919 const char *s = luaL_tolstring(L, arg, &l);
920 if (!strchr(form, '.') && l >= 100) {
921 /* no precision and string is too long to be formatted;
922 keep original string */
923 luaL_addvalue(&b);
924 break;
925 }
926 else {
927 nb = sprintf(buff, form, s);
928 lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
929 break;
930 }
931 }
932 default: { /* also treat cases 'pnLlh' */
933 return luaL_error(L, "invalid option '%%%c' to 'format'",
934 *(strfrmt - 1));
935 }
936 }
937 luaL_addsize(&b, nb);
938 }
939 }
940 luaL_pushresult(&b);
941 return 1;
942 }
943
944 /* }====================================================== */
945
946
947 /*
948 ** {======================================================
949 ** PACK/UNPACK
950 ** =======================================================
951 */
952
953
954 /* value used for padding */
955 #if !defined(LUA_PACKPADBYTE)
956 #define LUA_PACKPADBYTE 0x00
957 #endif
958
959 /* maximum size for the binary representation of an integer */
960 #define MAXINTSIZE 16
961
962 /* number of bits in a character */
963 #define NB CHAR_BIT
964
965 /* mask for one character (NB 1's) */
966 #define MC ((1 << NB) - 1)
967
968 /* size of a lua_Integer */
969 #define SZINT ((int)sizeof(lua_Integer))
970
971
972 /* dummy union to get native endianness */
973 static const union {
974 int dummy;
975 char little; /* true iff machine is little endian */
976 } nativeendian = {1};
977
978
979 /* dummy structure to get native alignment requirements */
980 struct cD {
981 char c;
982 union { double d; void *p; lua_Integer i; lua_Number n; } u;
983 };
984
985 #define MAXALIGN (offsetof(struct cD, u))
986
987
988 /*
989 ** Union for serializing floats
990 */
991 typedef union Ftypes {
992 float f;
993 double d;
994 lua_Number n;
995 char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
996 } Ftypes;
997
998
999 /*
1000 ** information to pack/unpack stuff
1001 */
1002 typedef struct Header {
1003 lua_State *L;
1004 int islittle;
1005 int maxalign;
1006 } Header;
1007
1008
1009 /*
1010 ** options for pack/unpack
1011 */
1012 typedef enum KOption {
1013 Kint, /* signed integers */
1014 Kuint, /* unsigned integers */
1015 Kfloat, /* floating-point numbers */
1016 Kchar, /* fixed-length strings */
1017 Kstring, /* strings with prefixed length */
1018 Kzstr, /* zero-terminated strings */
1019 Kpadding, /* padding */
1020 Kpaddalign, /* padding for alignment */
1021 Knop /* no-op (configuration or spaces) */
1022 } KOption;
1023
1024
1025 /*
1026 ** Read an integer numeral from string 'fmt' or return 'df' if
1027 ** there is no numeral
1028 */
digit(int c)1029 static int digit (int c) { return '0' <= c && c <= '9'; }
1030
getnum(const char ** fmt,int df)1031 static int getnum (const char **fmt, int df) {
1032 if (!digit(**fmt)) /* no number? */
1033 return df; /* return default value */
1034 else {
1035 int a = 0;
1036 do {
1037 a = a*10 + (*((*fmt)++) - '0');
1038 } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10);
1039 return a;
1040 }
1041 }
1042
1043
1044 /*
1045 ** Read an integer numeral and raises an error if it is larger
1046 ** than the maximum size for integers.
1047 */
getnumlimit(Header * h,const char ** fmt,int df)1048 static int getnumlimit (Header *h, const char **fmt, int df) {
1049 int sz = getnum(fmt, df);
1050 if (sz > MAXINTSIZE || sz <= 0)
1051 luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
1052 sz, MAXINTSIZE);
1053 return sz;
1054 }
1055
1056
1057 /*
1058 ** Initialize Header
1059 */
initheader(lua_State * L,Header * h)1060 static void initheader (lua_State *L, Header *h) {
1061 h->L = L;
1062 h->islittle = nativeendian.little;
1063 h->maxalign = 1;
1064 }
1065
1066
1067 /*
1068 ** Read and classify next option. 'size' is filled with option's size.
1069 */
getoption(Header * h,const char ** fmt,int * size)1070 static KOption getoption (Header *h, const char **fmt, int *size) {
1071 int opt = *((*fmt)++);
1072 *size = 0; /* default */
1073 switch (opt) {
1074 case 'b': *size = sizeof(char); return Kint;
1075 case 'B': *size = sizeof(char); return Kuint;
1076 case 'h': *size = sizeof(short); return Kint;
1077 case 'H': *size = sizeof(short); return Kuint;
1078 case 'l': *size = sizeof(long); return Kint;
1079 case 'L': *size = sizeof(long); return Kuint;
1080 case 'j': *size = sizeof(lua_Integer); return Kint;
1081 case 'J': *size = sizeof(lua_Integer); return Kuint;
1082 case 'T': *size = sizeof(size_t); return Kuint;
1083 case 'f': *size = sizeof(float); return Kfloat;
1084 case 'd': *size = sizeof(double); return Kfloat;
1085 case 'n': *size = sizeof(lua_Number); return Kfloat;
1086 case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
1087 case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
1088 case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
1089 case 'c':
1090 *size = getnum(fmt, -1);
1091 if (*size == -1)
1092 luaL_error(h->L, "missing size for format option 'c'");
1093 return Kchar;
1094 case 'z': return Kzstr;
1095 case 'x': *size = 1; return Kpadding;
1096 case 'X': return Kpaddalign;
1097 case ' ': break;
1098 case '<': h->islittle = 1; break;
1099 case '>': h->islittle = 0; break;
1100 case '=': h->islittle = nativeendian.little; break;
1101 case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break;
1102 default: luaL_error(h->L, "invalid format option '%c'", opt);
1103 }
1104 return Knop;
1105 }
1106
1107
1108 /*
1109 ** Read, classify, and fill other details about the next option.
1110 ** 'psize' is filled with option's size, 'notoalign' with its
1111 ** alignment requirements.
1112 ** Local variable 'size' gets the size to be aligned. (Kpadal option
1113 ** always gets its full alignment, other options are limited by
1114 ** the maximum alignment ('maxalign'). Kchar option needs no alignment
1115 ** despite its size.
1116 */
getdetails(Header * h,size_t totalsize,const char ** fmt,int * psize,int * ntoalign)1117 static KOption getdetails (Header *h, size_t totalsize,
1118 const char **fmt, int *psize, int *ntoalign) {
1119 KOption opt = getoption(h, fmt, psize);
1120 int align = *psize; /* usually, alignment follows size */
1121 if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
1122 if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
1123 luaL_argerror(h->L, 1, "invalid next option for option 'X'");
1124 }
1125 if (align <= 1 || opt == Kchar) /* need no alignment? */
1126 *ntoalign = 0;
1127 else {
1128 if (align > h->maxalign) /* enforce maximum alignment */
1129 align = h->maxalign;
1130 if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */
1131 luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
1132 *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
1133 }
1134 return opt;
1135 }
1136
1137
1138 /*
1139 ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
1140 ** The final 'if' handles the case when 'size' is larger than
1141 ** the size of a Lua integer, correcting the extra sign-extension
1142 ** bytes if necessary (by default they would be zeros).
1143 */
packint(luaL_Buffer * b,lua_Unsigned n,int islittle,int size,int neg)1144 static void packint (luaL_Buffer *b, lua_Unsigned n,
1145 int islittle, int size, int neg) {
1146 char *buff = luaL_prepbuffsize(b, size);
1147 int i;
1148 buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
1149 for (i = 1; i < size; i++) {
1150 n >>= NB;
1151 buff[islittle ? i : size - 1 - i] = (char)(n & MC);
1152 }
1153 if (neg && size > SZINT) { /* negative number need sign extension? */
1154 for (i = SZINT; i < size; i++) /* correct extra bytes */
1155 buff[islittle ? i : size - 1 - i] = (char)MC;
1156 }
1157 luaL_addsize(b, size); /* add result to buffer */
1158 }
1159
1160
1161 /*
1162 ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
1163 ** given 'islittle' is different from native endianness.
1164 */
copywithendian(volatile char * dest,volatile const char * src,int size,int islittle)1165 static void copywithendian (volatile char *dest, volatile const char *src,
1166 int size, int islittle) {
1167 if (islittle == nativeendian.little) {
1168 while (size-- != 0)
1169 *(dest++) = *(src++);
1170 }
1171 else {
1172 dest += size - 1;
1173 while (size-- != 0)
1174 *(dest--) = *(src++);
1175 }
1176 }
1177
1178
str_pack(lua_State * L)1179 static int str_pack (lua_State *L) {
1180 luaL_Buffer b;
1181 Header h;
1182 const char *fmt = luaL_checkstring(L, 1); /* format string */
1183 int arg = 1; /* current argument to pack */
1184 size_t totalsize = 0; /* accumulate total size of result */
1185 initheader(L, &h);
1186 lua_pushnil(L); /* mark to separate arguments from string buffer */
1187 luaL_buffinit(L, &b);
1188 while (*fmt != '\0') {
1189 int size, ntoalign;
1190 KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
1191 totalsize += ntoalign + size;
1192 while (ntoalign-- > 0)
1193 luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */
1194 arg++;
1195 switch (opt) {
1196 case Kint: { /* signed integers */
1197 lua_Integer n = luaL_checkinteger(L, arg);
1198 if (size < SZINT) { /* need overflow check? */
1199 lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
1200 luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
1201 }
1202 packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
1203 break;
1204 }
1205 case Kuint: { /* unsigned integers */
1206 lua_Integer n = luaL_checkinteger(L, arg);
1207 if (size < SZINT) /* need overflow check? */
1208 luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
1209 arg, "unsigned overflow");
1210 packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
1211 break;
1212 }
1213 case Kfloat: { /* floating-point options */
1214 volatile Ftypes u;
1215 char *buff = luaL_prepbuffsize(&b, size);
1216 lua_Number n = luaL_checknumber(L, arg); /* get argument */
1217 if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
1218 else if (size == sizeof(u.d)) u.d = (double)n;
1219 else u.n = n;
1220 /* move 'u' to final result, correcting endianness if needed */
1221 copywithendian(buff, u.buff, size, h.islittle);
1222 luaL_addsize(&b, size);
1223 break;
1224 }
1225 case Kchar: { /* fixed-size string */
1226 size_t len;
1227 const char *s = luaL_checklstring(L, arg, &len);
1228 luaL_argcheck(L, len == (size_t)size, arg, "wrong length");
1229 luaL_addlstring(&b, s, size);
1230 break;
1231 }
1232 case Kstring: { /* strings with length count */
1233 size_t len;
1234 const char *s = luaL_checklstring(L, arg, &len);
1235 luaL_argcheck(L, size >= (int)sizeof(size_t) ||
1236 len < ((size_t)1 << (size * NB)),
1237 arg, "string length does not fit in given size");
1238 packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */
1239 luaL_addlstring(&b, s, len);
1240 totalsize += len;
1241 break;
1242 }
1243 case Kzstr: { /* zero-terminated string */
1244 size_t len;
1245 const char *s = luaL_checklstring(L, arg, &len);
1246 luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
1247 luaL_addlstring(&b, s, len);
1248 luaL_addchar(&b, '\0'); /* add zero at the end */
1249 totalsize += len + 1;
1250 break;
1251 }
1252 case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* go through */
1253 case Kpaddalign: case Knop:
1254 arg--; /* undo increment */
1255 break;
1256 }
1257 }
1258 luaL_pushresult(&b);
1259 return 1;
1260 }
1261
1262
str_packsize(lua_State * L)1263 static int str_packsize (lua_State *L) {
1264 Header h;
1265 const char *fmt = luaL_checkstring(L, 1); /* format string */
1266 size_t totalsize = 0; /* accumulate total size of result */
1267 initheader(L, &h);
1268 while (*fmt != '\0') {
1269 int size, ntoalign;
1270 KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
1271 size += ntoalign; /* total space used by option */
1272 luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
1273 "format result too large");
1274 totalsize += size;
1275 switch (opt) {
1276 case Kstring: /* strings with length count */
1277 case Kzstr: /* zero-terminated string */
1278 luaL_argerror(L, 1, "variable-length format");
1279 break;
1280 default: break;
1281 }
1282 }
1283 lua_pushinteger(L, (lua_Integer)totalsize);
1284 return 1;
1285 }
1286
1287
1288 /*
1289 ** Unpack an integer with 'size' bytes and 'islittle' endianness.
1290 ** If size is smaller than the size of a Lua integer and integer
1291 ** is signed, must do sign extension (propagating the sign to the
1292 ** higher bits); if size is larger than the size of a Lua integer,
1293 ** it must check the unread bytes to see whether they do not cause an
1294 ** overflow.
1295 */
unpackint(lua_State * L,const char * str,int islittle,int size,int issigned)1296 static lua_Integer unpackint (lua_State *L, const char *str,
1297 int islittle, int size, int issigned) {
1298 lua_Unsigned res = 0;
1299 int i;
1300 int limit = (size <= SZINT) ? size : SZINT;
1301 for (i = limit - 1; i >= 0; i--) {
1302 res <<= NB;
1303 res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i];
1304 }
1305 if (size < SZINT) { /* real size smaller than lua_Integer? */
1306 if (issigned) { /* needs sign extension? */
1307 lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
1308 res = ((res ^ mask) - mask); /* do sign extension */
1309 }
1310 }
1311 else if (size > SZINT) { /* must check unread bytes */
1312 int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
1313 for (i = limit; i < size; i++) {
1314 if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
1315 luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
1316 }
1317 }
1318 return (lua_Integer)res;
1319 }
1320
1321
str_unpack(lua_State * L)1322 static int str_unpack (lua_State *L) {
1323 Header h;
1324 const char *fmt = luaL_checkstring(L, 1);
1325 size_t ld;
1326 const char *data = luaL_checklstring(L, 2, &ld);
1327 size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
1328 int n = 0; /* number of results */
1329 luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
1330 initheader(L, &h);
1331 while (*fmt != '\0') {
1332 int size, ntoalign;
1333 KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
1334 if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
1335 luaL_argerror(L, 2, "data string too short");
1336 pos += ntoalign; /* skip alignment */
1337 /* stack space for item + next position */
1338 luaL_checkstack(L, 2, "too many results");
1339 n++;
1340 switch (opt) {
1341 case Kint:
1342 case Kuint: {
1343 lua_Integer res = unpackint(L, data + pos, h.islittle, size,
1344 (opt == Kint));
1345 lua_pushinteger(L, res);
1346 break;
1347 }
1348 case Kfloat: {
1349 volatile Ftypes u;
1350 lua_Number num;
1351 copywithendian(u.buff, data + pos, size, h.islittle);
1352 if (size == sizeof(u.f)) num = (lua_Number)u.f;
1353 else if (size == sizeof(u.d)) num = (lua_Number)u.d;
1354 else num = u.n;
1355 lua_pushnumber(L, num);
1356 break;
1357 }
1358 case Kchar: {
1359 lua_pushlstring(L, data + pos, size);
1360 break;
1361 }
1362 case Kstring: {
1363 size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
1364 luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
1365 lua_pushlstring(L, data + pos + size, len);
1366 pos += len; /* skip string */
1367 break;
1368 }
1369 case Kzstr: {
1370 size_t len = (int)strlen(data + pos);
1371 lua_pushlstring(L, data + pos, len);
1372 pos += len + 1; /* skip string plus final '\0' */
1373 break;
1374 }
1375 case Kpaddalign: case Kpadding: case Knop:
1376 n--; /* undo increment */
1377 break;
1378 }
1379 pos += size;
1380 }
1381 lua_pushinteger(L, pos + 1); /* next position */
1382 return n + 1;
1383 }
1384
1385 /* }====================================================== */
1386
1387
1388 static const luaL_Reg strlib[] = {
1389 {"byte", str_byte},
1390 {"char", str_char},
1391 {"dump", str_dump},
1392 {"find", str_find},
1393 {"format", str_format},
1394 {"gmatch", gmatch},
1395 {"gsub", str_gsub},
1396 {"len", str_len},
1397 {"lower", str_lower},
1398 {"match", str_match},
1399 {"rep", str_rep},
1400 {"reverse", str_reverse},
1401 {"sub", str_sub},
1402 {"upper", str_upper},
1403 {"pack", str_pack},
1404 {"packsize", str_packsize},
1405 {"unpack", str_unpack},
1406 {NULL, NULL}
1407 };
1408
1409
createmetatable(lua_State * L)1410 static void createmetatable (lua_State *L) {
1411 lua_createtable(L, 0, 1); /* table to be metatable for strings */
1412 lua_pushliteral(L, ""); /* dummy string */
1413 lua_pushvalue(L, -2); /* copy table */
1414 lua_setmetatable(L, -2); /* set table as metatable for strings */
1415 lua_pop(L, 1); /* pop dummy string */
1416 lua_pushvalue(L, -2); /* get string library */
1417 lua_setfield(L, -2, "__index"); /* metatable.__index = string */
1418 lua_pop(L, 1); /* pop metatable */
1419 }
1420
1421
1422 /*
1423 ** Open string library
1424 */
luaopen_string(lua_State * L)1425 LUAMOD_API int luaopen_string (lua_State *L) {
1426 luaL_newlib(L, strlib);
1427 createmetatable(L);
1428 return 1;
1429 }
1430
1431