Disable LUA_COMPAT_GFIND and LUA_COMPAT_MOD in Lua 5.2 mode.
[luajit-2.0/celess22.git] / src / lib_string.c
blobd894f9f4a5d10200739b6c35ffdd4096907b8a24
1 /*
2 ** String library.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
4 **
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7 */
9 #include <stdio.h>
11 #define lib_string_c
12 #define LUA_LIB
14 #include "lua.h"
15 #include "lauxlib.h"
16 #include "lualib.h"
18 #include "lj_obj.h"
19 #include "lj_gc.h"
20 #include "lj_err.h"
21 #include "lj_str.h"
22 #include "lj_tab.h"
23 #include "lj_state.h"
24 #include "lj_ff.h"
25 #include "lj_bcdump.h"
26 #include "lj_char.h"
27 #include "lj_lib.h"
29 /* ------------------------------------------------------------------------ */
31 #define LJLIB_MODULE_string
33 LJLIB_ASM(string_len) LJLIB_REC(.)
35 lj_lib_checkstr(L, 1);
36 return FFH_RETRY;
39 LJLIB_ASM(string_byte) LJLIB_REC(string_range 0)
41 GCstr *s = lj_lib_checkstr(L, 1);
42 int32_t len = (int32_t)s->len;
43 int32_t start = lj_lib_optint(L, 2, 1);
44 int32_t stop = lj_lib_optint(L, 3, start);
45 int32_t n, i;
46 const unsigned char *p;
47 if (stop < 0) stop += len+1;
48 if (start < 0) start += len+1;
49 if (start <= 0) start = 1;
50 if (stop > len) stop = len;
51 if (start > stop) return FFH_RES(0); /* Empty interval: return no results. */
52 start--;
53 n = stop - start;
54 if ((uint32_t)n > LUAI_MAXCSTACK)
55 lj_err_caller(L, LJ_ERR_STRSLC);
56 lj_state_checkstack(L, (MSize)n);
57 p = (const unsigned char *)strdata(s) + start;
58 for (i = 0; i < n; i++)
59 setintV(L->base + i-1, p[i]);
60 return FFH_RES(n);
63 LJLIB_ASM(string_char)
65 int i, nargs = (int)(L->top - L->base);
66 char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, (size_t)nargs);
67 for (i = 1; i <= nargs; i++) {
68 int32_t k = lj_lib_checkint(L, i);
69 if (!checku8(k))
70 lj_err_arg(L, i, LJ_ERR_BADVAL);
71 buf[i-1] = (char)k;
73 setstrV(L, L->base-1, lj_str_new(L, buf, (size_t)nargs));
74 return FFH_RES(1);
77 LJLIB_ASM(string_sub) LJLIB_REC(string_range 1)
79 lj_lib_checkstr(L, 1);
80 lj_lib_checkint(L, 2);
81 setintV(L->base+2, lj_lib_optint(L, 3, -1));
82 return FFH_RETRY;
85 LJLIB_ASM(string_rep)
87 GCstr *s = lj_lib_checkstr(L, 1);
88 int32_t len = (int32_t)s->len;
89 int32_t k = lj_lib_checkint(L, 2);
90 int64_t tlen = (int64_t)k * len;
91 const char *src;
92 char *buf;
93 if (k <= 0) return FFH_RETRY;
94 if (tlen > LJ_MAX_STR)
95 lj_err_caller(L, LJ_ERR_STROV);
96 buf = lj_str_needbuf(L, &G(L)->tmpbuf, (MSize)tlen);
97 if (len <= 1) return FFH_RETRY; /* ASM code only needed buffer resize. */
98 src = strdata(s);
99 do {
100 int32_t i = 0;
101 do { *buf++ = src[i++]; } while (i < len);
102 } while (--k > 0);
103 setstrV(L, L->base-1, lj_str_new(L, G(L)->tmpbuf.buf, (size_t)tlen));
104 return FFH_RES(1);
107 LJLIB_ASM(string_reverse)
109 GCstr *s = lj_lib_checkstr(L, 1);
110 lj_str_needbuf(L, &G(L)->tmpbuf, s->len);
111 return FFH_RETRY;
113 LJLIB_ASM_(string_lower)
114 LJLIB_ASM_(string_upper)
116 /* ------------------------------------------------------------------------ */
118 static int writer_buf(lua_State *L, const void *p, size_t size, void *b)
120 luaL_addlstring((luaL_Buffer *)b, (const char *)p, size);
121 UNUSED(L);
122 return 0;
125 LJLIB_CF(string_dump)
127 GCfunc *fn = lj_lib_checkfunc(L, 1);
128 int strip = L->base+1 < L->top && tvistruecond(L->base+1);
129 luaL_Buffer b;
130 L->top = L->base+1;
131 luaL_buffinit(L, &b);
132 if (!isluafunc(fn) || lj_bcwrite(L, funcproto(fn), writer_buf, &b, strip))
133 lj_err_caller(L, LJ_ERR_STRDUMP);
134 luaL_pushresult(&b);
135 return 1;
138 /* ------------------------------------------------------------------------ */
140 /* macro to `unsign' a character */
141 #define uchar(c) ((unsigned char)(c))
143 #define CAP_UNFINISHED (-1)
144 #define CAP_POSITION (-2)
146 typedef struct MatchState {
147 const char *src_init; /* init of source string */
148 const char *src_end; /* end (`\0') of source string */
149 lua_State *L;
150 int level; /* total number of captures (finished or unfinished) */
151 int depth;
152 struct {
153 const char *init;
154 ptrdiff_t len;
155 } capture[LUA_MAXCAPTURES];
156 } MatchState;
158 #define L_ESC '%'
159 #define SPECIALS "^$*+?.([%-"
161 static int check_capture(MatchState *ms, int l)
163 l -= '1';
164 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
165 lj_err_caller(ms->L, LJ_ERR_STRCAPI);
166 return l;
169 static int capture_to_close(MatchState *ms)
171 int level = ms->level;
172 for (level--; level>=0; level--)
173 if (ms->capture[level].len == CAP_UNFINISHED) return level;
174 lj_err_caller(ms->L, LJ_ERR_STRPATC);
175 return 0; /* unreachable */
178 static const char *classend(MatchState *ms, const char *p)
180 switch (*p++) {
181 case L_ESC:
182 if (*p == '\0')
183 lj_err_caller(ms->L, LJ_ERR_STRPATE);
184 return p+1;
185 case '[':
186 if (*p == '^') p++;
187 do { /* look for a `]' */
188 if (*p == '\0')
189 lj_err_caller(ms->L, LJ_ERR_STRPATM);
190 if (*(p++) == L_ESC && *p != '\0')
191 p++; /* skip escapes (e.g. `%]') */
192 } while (*p != ']');
193 return p+1;
194 default:
195 return p;
199 static const unsigned char match_class_map[32] = {
200 0,LJ_CHAR_ALPHA,0,LJ_CHAR_CNTRL,LJ_CHAR_DIGIT,0,0,LJ_CHAR_GRAPH,0,0,0,0,
201 LJ_CHAR_LOWER,0,0,0,LJ_CHAR_PUNCT,0,0,LJ_CHAR_SPACE,0,
202 LJ_CHAR_UPPER,0,LJ_CHAR_ALNUM,LJ_CHAR_XDIGIT,0,0,0,0,0,0,0
205 static int match_class(int c, int cl)
207 if ((cl & 0xc0) == 0x40) {
208 int t = match_class_map[(cl&0x1f)];
209 if (t) {
210 t = lj_char_isa(c, t);
211 return (cl & 0x20) ? t : !t;
213 if (cl == 'z') return c == 0;
214 if (cl == 'Z') return c != 0;
216 return (cl == c);
219 static int matchbracketclass(int c, const char *p, const char *ec)
221 int sig = 1;
222 if (*(p+1) == '^') {
223 sig = 0;
224 p++; /* skip the `^' */
226 while (++p < ec) {
227 if (*p == L_ESC) {
228 p++;
229 if (match_class(c, uchar(*p)))
230 return sig;
232 else if ((*(p+1) == '-') && (p+2 < ec)) {
233 p+=2;
234 if (uchar(*(p-2)) <= c && c <= uchar(*p))
235 return sig;
237 else if (uchar(*p) == c) return sig;
239 return !sig;
242 static int singlematch(int c, const char *p, const char *ep)
244 switch (*p) {
245 case '.': return 1; /* matches any char */
246 case L_ESC: return match_class(c, uchar(*(p+1)));
247 case '[': return matchbracketclass(c, p, ep-1);
248 default: return (uchar(*p) == c);
252 static const char *match(MatchState *ms, const char *s, const char *p);
254 static const char *matchbalance(MatchState *ms, const char *s, const char *p)
256 if (*p == 0 || *(p+1) == 0)
257 lj_err_caller(ms->L, LJ_ERR_STRPATU);
258 if (*s != *p) {
259 return NULL;
260 } else {
261 int b = *p;
262 int e = *(p+1);
263 int cont = 1;
264 while (++s < ms->src_end) {
265 if (*s == e) {
266 if (--cont == 0) return s+1;
267 } else if (*s == b) {
268 cont++;
272 return NULL; /* string ends out of balance */
275 static const char *max_expand(MatchState *ms, const char *s,
276 const char *p, const char *ep)
278 ptrdiff_t i = 0; /* counts maximum expand for item */
279 while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
280 i++;
281 /* keeps trying to match with the maximum repetitions */
282 while (i>=0) {
283 const char *res = match(ms, (s+i), ep+1);
284 if (res) return res;
285 i--; /* else didn't match; reduce 1 repetition to try again */
287 return NULL;
290 static const char *min_expand(MatchState *ms, const char *s,
291 const char *p, const char *ep)
293 for (;;) {
294 const char *res = match(ms, s, ep+1);
295 if (res != NULL)
296 return res;
297 else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
298 s++; /* try with one more repetition */
299 else
300 return NULL;
304 static const char *start_capture(MatchState *ms, const char *s,
305 const char *p, int what)
307 const char *res;
308 int level = ms->level;
309 if (level >= LUA_MAXCAPTURES) lj_err_caller(ms->L, LJ_ERR_STRCAPN);
310 ms->capture[level].init = s;
311 ms->capture[level].len = what;
312 ms->level = level+1;
313 if ((res=match(ms, s, p)) == NULL) /* match failed? */
314 ms->level--; /* undo capture */
315 return res;
318 static const char *end_capture(MatchState *ms, const char *s,
319 const char *p)
321 int l = capture_to_close(ms);
322 const char *res;
323 ms->capture[l].len = s - ms->capture[l].init; /* close capture */
324 if ((res = match(ms, s, p)) == NULL) /* match failed? */
325 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
326 return res;
329 static const char *match_capture(MatchState *ms, const char *s, int l)
331 size_t len;
332 l = check_capture(ms, l);
333 len = (size_t)ms->capture[l].len;
334 if ((size_t)(ms->src_end-s) >= len &&
335 memcmp(ms->capture[l].init, s, len) == 0)
336 return s+len;
337 else
338 return NULL;
341 static const char *match(MatchState *ms, const char *s, const char *p)
343 if (++ms->depth > LJ_MAX_XLEVEL)
344 lj_err_caller(ms->L, LJ_ERR_STRPATX);
345 init: /* using goto's to optimize tail recursion */
346 switch (*p) {
347 case '(': /* start capture */
348 if (*(p+1) == ')') /* position capture? */
349 s = start_capture(ms, s, p+2, CAP_POSITION);
350 else
351 s = start_capture(ms, s, p+1, CAP_UNFINISHED);
352 break;
353 case ')': /* end capture */
354 s = end_capture(ms, s, p+1);
355 break;
356 case L_ESC:
357 switch (*(p+1)) {
358 case 'b': /* balanced string? */
359 s = matchbalance(ms, s, p+2);
360 if (s == NULL) break;
361 p+=4;
362 goto init; /* else s = match(ms, s, p+4); */
363 case 'f': { /* frontier? */
364 const char *ep; char previous;
365 p += 2;
366 if (*p != '[')
367 lj_err_caller(ms->L, LJ_ERR_STRPATB);
368 ep = classend(ms, p); /* points to what is next */
369 previous = (s == ms->src_init) ? '\0' : *(s-1);
370 if (matchbracketclass(uchar(previous), p, ep-1) ||
371 !matchbracketclass(uchar(*s), p, ep-1)) { s = NULL; break; }
372 p=ep;
373 goto init; /* else s = match(ms, s, ep); */
375 default:
376 if (lj_char_isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
377 s = match_capture(ms, s, uchar(*(p+1)));
378 if (s == NULL) break;
379 p+=2;
380 goto init; /* else s = match(ms, s, p+2) */
382 goto dflt; /* case default */
384 break;
385 case '\0': /* end of pattern */
386 break; /* match succeeded */
387 case '$':
388 /* is the `$' the last char in pattern? */
389 if (*(p+1) != '\0') goto dflt;
390 if (s != ms->src_end) s = NULL; /* check end of string */
391 break;
392 default: dflt: { /* it is a pattern item */
393 const char *ep = classend(ms, p); /* points to what is next */
394 int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
395 switch (*ep) {
396 case '?': { /* optional */
397 const char *res;
398 if (m && ((res=match(ms, s+1, ep+1)) != NULL)) {
399 s = res;
400 break;
402 p=ep+1;
403 goto init; /* else s = match(ms, s, ep+1); */
405 case '*': /* 0 or more repetitions */
406 s = max_expand(ms, s, p, ep);
407 break;
408 case '+': /* 1 or more repetitions */
409 s = (m ? max_expand(ms, s+1, p, ep) : NULL);
410 break;
411 case '-': /* 0 or more repetitions (minimum) */
412 s = min_expand(ms, s, p, ep);
413 break;
414 default:
415 if (m) { s++; p=ep; goto init; } /* else s = match(ms, s+1, ep); */
416 s = NULL;
417 break;
419 break;
422 ms->depth--;
423 return s;
426 static const char *lmemfind(const char *s1, size_t l1,
427 const char *s2, size_t l2)
429 if (l2 == 0) {
430 return s1; /* empty strings are everywhere */
431 } else if (l2 > l1) {
432 return NULL; /* avoids a negative `l1' */
433 } else {
434 const char *init; /* to search for a `*s2' inside `s1' */
435 l2--; /* 1st char will be checked by `memchr' */
436 l1 = l1-l2; /* `s2' cannot be found after that */
437 while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
438 init++; /* 1st char is already checked */
439 if (memcmp(init, s2+1, l2) == 0) {
440 return init-1;
441 } else { /* correct `l1' and `s1' to try again */
442 l1 -= (size_t)(init-s1);
443 s1 = init;
446 return NULL; /* not found */
450 static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
452 if (i >= ms->level) {
453 if (i == 0) /* ms->level == 0, too */
454 lua_pushlstring(ms->L, s, (size_t)(e - s)); /* add whole match */
455 else
456 lj_err_caller(ms->L, LJ_ERR_STRCAPI);
457 } else {
458 ptrdiff_t l = ms->capture[i].len;
459 if (l == CAP_UNFINISHED) lj_err_caller(ms->L, LJ_ERR_STRCAPU);
460 if (l == CAP_POSITION)
461 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
462 else
463 lua_pushlstring(ms->L, ms->capture[i].init, (size_t)l);
467 static int push_captures(MatchState *ms, const char *s, const char *e)
469 int i;
470 int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
471 luaL_checkstack(ms->L, nlevels, "too many captures");
472 for (i = 0; i < nlevels; i++)
473 push_onecapture(ms, i, s, e);
474 return nlevels; /* number of strings pushed */
477 static ptrdiff_t posrelat(ptrdiff_t pos, size_t len)
479 /* relative string position: negative means back from end */
480 if (pos < 0) pos += (ptrdiff_t)len + 1;
481 return (pos >= 0) ? pos : 0;
484 static int str_find_aux(lua_State *L, int find)
486 size_t l1, l2;
487 const char *s = luaL_checklstring(L, 1, &l1);
488 const char *p = luaL_checklstring(L, 2, &l2);
489 ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
490 if (init < 0)
491 init = 0;
492 else if ((size_t)(init) > l1)
493 init = (ptrdiff_t)l1;
494 if (find && (lua_toboolean(L, 4) || /* explicit request? */
495 strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
496 /* do a plain search */
497 const char *s2 = lmemfind(s+init, l1-(size_t)init, p, l2);
498 if (s2) {
499 lua_pushinteger(L, s2-s+1);
500 lua_pushinteger(L, s2-s+(ptrdiff_t)l2);
501 return 2;
503 } else {
504 MatchState ms;
505 int anchor = (*p == '^') ? (p++, 1) : 0;
506 const char *s1=s+init;
507 ms.L = L;
508 ms.src_init = s;
509 ms.src_end = s+l1;
510 do {
511 const char *res;
512 ms.level = ms.depth = 0;
513 if ((res=match(&ms, s1, p)) != NULL) {
514 if (find) {
515 lua_pushinteger(L, s1-s+1); /* start */
516 lua_pushinteger(L, res-s); /* end */
517 return push_captures(&ms, NULL, 0) + 2;
518 } else {
519 return push_captures(&ms, s1, res);
522 } while (s1++ < ms.src_end && !anchor);
524 lua_pushnil(L); /* not found */
525 return 1;
528 LJLIB_CF(string_find)
530 return str_find_aux(L, 1);
533 LJLIB_CF(string_match)
535 return str_find_aux(L, 0);
538 LJLIB_NOREG LJLIB_CF(string_gmatch_aux)
540 const char *p = strVdata(lj_lib_upvalue(L, 2));
541 GCstr *str = strV(lj_lib_upvalue(L, 1));
542 const char *s = strdata(str);
543 TValue *tvpos = lj_lib_upvalue(L, 3);
544 const char *src = s + tvpos->u32.lo;
545 MatchState ms;
546 ms.L = L;
547 ms.src_init = s;
548 ms.src_end = s + str->len;
549 for (; src <= ms.src_end; src++) {
550 const char *e;
551 ms.level = ms.depth = 0;
552 if ((e = match(&ms, src, p)) != NULL) {
553 int32_t pos = (int32_t)(e - s);
554 if (e == src) pos++; /* Ensure progress for empty match. */
555 tvpos->u32.lo = (uint32_t)pos;
556 return push_captures(&ms, src, e);
559 return 0; /* not found */
562 LJLIB_CF(string_gmatch)
564 lj_lib_checkstr(L, 1);
565 lj_lib_checkstr(L, 2);
566 L->top = L->base+3;
567 (L->top-1)->u64 = 0;
568 lj_lib_pushcc(L, lj_cf_string_gmatch_aux, FF_string_gmatch_aux, 3);
569 return 1;
572 static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
574 size_t l, i;
575 const char *news = lua_tolstring(ms->L, 3, &l);
576 for (i = 0; i < l; i++) {
577 if (news[i] != L_ESC) {
578 luaL_addchar(b, news[i]);
579 } else {
580 i++; /* skip ESC */
581 if (!lj_char_isdigit(uchar(news[i]))) {
582 luaL_addchar(b, news[i]);
583 } else if (news[i] == '0') {
584 luaL_addlstring(b, s, (size_t)(e - s));
585 } else {
586 push_onecapture(ms, news[i] - '1', s, e);
587 luaL_addvalue(b); /* add capture to accumulated result */
593 static void add_value(MatchState *ms, luaL_Buffer *b,
594 const char *s, const char *e)
596 lua_State *L = ms->L;
597 switch (lua_type(L, 3)) {
598 case LUA_TNUMBER:
599 case LUA_TSTRING: {
600 add_s(ms, b, s, e);
601 return;
603 case LUA_TFUNCTION: {
604 int n;
605 lua_pushvalue(L, 3);
606 n = push_captures(ms, s, e);
607 lua_call(L, n, 1);
608 break;
610 case LUA_TTABLE: {
611 push_onecapture(ms, 0, s, e);
612 lua_gettable(L, 3);
613 break;
616 if (!lua_toboolean(L, -1)) { /* nil or false? */
617 lua_pop(L, 1);
618 lua_pushlstring(L, s, (size_t)(e - s)); /* keep original text */
619 } else if (!lua_isstring(L, -1)) {
620 lj_err_callerv(L, LJ_ERR_STRGSRV, luaL_typename(L, -1));
622 luaL_addvalue(b); /* add result to accumulator */
625 LJLIB_CF(string_gsub)
627 size_t srcl;
628 const char *src = luaL_checklstring(L, 1, &srcl);
629 const char *p = luaL_checkstring(L, 2);
630 int tr = lua_type(L, 3);
631 int max_s = luaL_optint(L, 4, (int)(srcl+1));
632 int anchor = (*p == '^') ? (p++, 1) : 0;
633 int n = 0;
634 MatchState ms;
635 luaL_Buffer b;
636 if (!(tr == LUA_TNUMBER || tr == LUA_TSTRING ||
637 tr == LUA_TFUNCTION || tr == LUA_TTABLE))
638 lj_err_arg(L, 3, LJ_ERR_NOSFT);
639 luaL_buffinit(L, &b);
640 ms.L = L;
641 ms.src_init = src;
642 ms.src_end = src+srcl;
643 while (n < max_s) {
644 const char *e;
645 ms.level = ms.depth = 0;
646 e = match(&ms, src, p);
647 if (e) {
648 n++;
649 add_value(&ms, &b, src, e);
651 if (e && e>src) /* non empty match? */
652 src = e; /* skip it */
653 else if (src < ms.src_end)
654 luaL_addchar(&b, *src++);
655 else
656 break;
657 if (anchor)
658 break;
660 luaL_addlstring(&b, src, (size_t)(ms.src_end-src));
661 luaL_pushresult(&b);
662 lua_pushinteger(L, n); /* number of substitutions */
663 return 2;
666 /* ------------------------------------------------------------------------ */
668 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
669 #define MAX_FMTITEM 512
670 /* valid flags in a format specification */
671 #define FMT_FLAGS "-+ #0"
673 ** maximum size of each format specification (such as '%-099.99d')
674 ** (+10 accounts for %99.99x plus margin of error)
676 #define MAX_FMTSPEC (sizeof(FMT_FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
678 static void addquoted(lua_State *L, luaL_Buffer *b, int arg)
680 GCstr *str = lj_lib_checkstr(L, arg);
681 int32_t len = (int32_t)str->len;
682 const char *s = strdata(str);
683 luaL_addchar(b, '"');
684 while (len--) {
685 uint32_t c = uchar(*s);
686 if (c == '"' || c == '\\' || c == '\n') {
687 luaL_addchar(b, '\\');
688 } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */
689 uint32_t d;
690 luaL_addchar(b, '\\');
691 if (c >= 100 || lj_char_isdigit(uchar(s[1]))) {
692 luaL_addchar(b, '0'+(c >= 100)); if (c >= 100) c -= 100;
693 goto tens;
694 } else if (c >= 10) {
695 tens:
696 d = (c * 205) >> 11; c -= d * 10; luaL_addchar(b, '0'+d);
698 c += '0';
700 luaL_addchar(b, c);
701 s++;
703 luaL_addchar(b, '"');
706 static const char *scanformat(lua_State *L, const char *strfrmt, char *form)
708 const char *p = strfrmt;
709 while (*p != '\0' && strchr(FMT_FLAGS, *p) != NULL) p++; /* skip flags */
710 if ((size_t)(p - strfrmt) >= sizeof(FMT_FLAGS))
711 lj_err_caller(L, LJ_ERR_STRFMTR);
712 if (lj_char_isdigit(uchar(*p))) p++; /* skip width */
713 if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */
714 if (*p == '.') {
715 p++;
716 if (lj_char_isdigit(uchar(*p))) p++; /* skip precision */
717 if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */
719 if (lj_char_isdigit(uchar(*p)))
720 lj_err_caller(L, LJ_ERR_STRFMTW);
721 *(form++) = '%';
722 strncpy(form, strfrmt, (size_t)(p - strfrmt + 1));
723 form += p - strfrmt + 1;
724 *form = '\0';
725 return p;
728 static void addintlen(char *form)
730 size_t l = strlen(form);
731 char spec = form[l - 1];
732 strcpy(form + l - 1, LUA_INTFRMLEN);
733 form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
734 form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
737 static unsigned LUA_INTFRM_T num2intfrm(lua_State *L, int arg)
739 if (sizeof(LUA_INTFRM_T) == 4) {
740 return (LUA_INTFRM_T)lj_lib_checkbit(L, arg);
741 } else {
742 cTValue *o;
743 lj_lib_checknumber(L, arg);
744 o = L->base+arg-1;
745 if (tvisint(o))
746 return (LUA_INTFRM_T)intV(o);
747 else
748 return (LUA_INTFRM_T)numV(o);
752 static unsigned LUA_INTFRM_T num2uintfrm(lua_State *L, int arg)
754 if (sizeof(LUA_INTFRM_T) == 4) {
755 return (unsigned LUA_INTFRM_T)lj_lib_checkbit(L, arg);
756 } else {
757 cTValue *o;
758 lj_lib_checknumber(L, arg);
759 o = L->base+arg-1;
760 if (tvisint(o))
761 return (unsigned LUA_INTFRM_T)intV(o);
762 else if ((int32_t)o->u32.hi < 0)
763 return (unsigned LUA_INTFRM_T)(LUA_INTFRM_T)numV(o);
764 else
765 return (unsigned LUA_INTFRM_T)numV(o);
769 LJLIB_CF(string_format)
771 int arg = 1, top = (int)(L->top - L->base);
772 GCstr *fmt = lj_lib_checkstr(L, arg);
773 const char *strfrmt = strdata(fmt);
774 const char *strfrmt_end = strfrmt + fmt->len;
775 luaL_Buffer b;
776 luaL_buffinit(L, &b);
777 while (strfrmt < strfrmt_end) {
778 if (*strfrmt != L_ESC) {
779 luaL_addchar(&b, *strfrmt++);
780 } else if (*++strfrmt == L_ESC) {
781 luaL_addchar(&b, *strfrmt++); /* %% */
782 } else { /* format item */
783 char form[MAX_FMTSPEC]; /* to store the format (`%...') */
784 char buff[MAX_FMTITEM]; /* to store the formatted item */
785 if (++arg > top)
786 luaL_argerror(L, arg, lj_obj_typename[0]);
787 strfrmt = scanformat(L, strfrmt, form);
788 switch (*strfrmt++) {
789 case 'c':
790 sprintf(buff, form, lj_lib_checkint(L, arg));
791 break;
792 case 'd': case 'i':
793 addintlen(form);
794 sprintf(buff, form, num2intfrm(L, arg));
795 break;
796 case 'o': case 'u': case 'x': case 'X':
797 addintlen(form);
798 sprintf(buff, form, num2uintfrm(L, arg));
799 break;
800 case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A': {
801 TValue tv;
802 tv.n = lj_lib_checknum(L, arg);
803 if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) {
804 /* Canonicalize output of non-finite values. */
805 char *p, nbuf[LJ_STR_NUMBUF];
806 size_t len = lj_str_bufnum(nbuf, &tv);
807 if (strfrmt[-1] < 'a') {
808 nbuf[len-3] = nbuf[len-3] - 0x20;
809 nbuf[len-2] = nbuf[len-2] - 0x20;
810 nbuf[len-1] = nbuf[len-1] - 0x20;
812 nbuf[len] = '\0';
813 for (p = form; *p < 'A' && *p != '.'; p++) ;
814 *p++ = 's'; *p = '\0';
815 sprintf(buff, form, nbuf);
816 break;
818 sprintf(buff, form, (double)tv.n);
819 break;
821 case 'q':
822 addquoted(L, &b, arg);
823 continue;
824 case 'p':
825 lj_str_pushf(L, "%p", lua_topointer(L, arg));
826 luaL_addvalue(&b);
827 continue;
828 case 's': {
829 GCstr *str = lj_lib_checkstr(L, arg);
830 if (!strchr(form, '.') && str->len >= 100) {
831 /* no precision and string is too long to be formatted;
832 keep original string */
833 setstrV(L, L->top++, str);
834 luaL_addvalue(&b);
835 continue;
837 sprintf(buff, form, strdata(str));
838 break;
840 default:
841 lj_err_callerv(L, LJ_ERR_STRFMTO, *(strfrmt -1));
842 break;
844 luaL_addlstring(&b, buff, strlen(buff));
847 luaL_pushresult(&b);
848 return 1;
851 /* ------------------------------------------------------------------------ */
853 #include "lj_libdef.h"
855 LUALIB_API int luaopen_string(lua_State *L)
857 GCtab *mt;
858 global_State *g;
859 LJ_LIB_REG(L, LUA_STRLIBNAME, string);
860 #if defined(LUA_COMPAT_GFIND) && !LJ_52
861 lua_getfield(L, -1, "gmatch");
862 lua_setfield(L, -2, "gfind");
863 #endif
864 mt = lj_tab_new(L, 0, 1);
865 /* NOBARRIER: basemt is a GC root. */
866 g = G(L);
867 setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt));
868 settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1));
869 mt->nomm = (uint8_t)(~(1u<<MM_index));
870 return 1;