3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
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
25 #include "lj_bcdump.h"
29 /* ------------------------------------------------------------------------ */
31 #define LJLIB_MODULE_string
33 LJLIB_ASM(string_len
) LJLIB_REC(.)
35 lj_lib_checkstr(L
, 1);
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
);
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. */
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
]);
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
);
70 lj_err_arg(L
, i
, LJ_ERR_BADVAL
);
73 setstrV(L
, L
->base
-1, lj_str_new(L
, buf
, (size_t)nargs
));
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));
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
;
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. */
101 do { *buf
++ = src
[i
++]; } while (i
< len
);
103 setstrV(L
, L
->base
-1, lj_str_new(L
, G(L
)->tmpbuf
.buf
, (size_t)tlen
));
107 LJLIB_ASM(string_reverse
)
109 GCstr
*s
= lj_lib_checkstr(L
, 1);
110 lj_str_needbuf(L
, &G(L
)->tmpbuf
, s
->len
);
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
);
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);
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
);
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 */
150 int level
; /* total number of captures (finished or unfinished) */
155 } capture
[LUA_MAXCAPTURES
];
159 #define SPECIALS "^$*+?.([%-"
161 static int check_capture(MatchState
*ms
, int l
)
164 if (l
< 0 || l
>= ms
->level
|| ms
->capture
[l
].len
== CAP_UNFINISHED
)
165 lj_err_caller(ms
->L
, LJ_ERR_STRCAPI
);
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
)
183 lj_err_caller(ms
->L
, LJ_ERR_STRPATE
);
187 do { /* look for a `]' */
189 lj_err_caller(ms
->L
, LJ_ERR_STRPATM
);
190 if (*(p
++) == L_ESC
&& *p
!= '\0')
191 p
++; /* skip escapes (e.g. `%]') */
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)];
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;
219 static int matchbracketclass(int c
, const char *p
, const char *ec
)
224 p
++; /* skip the `^' */
229 if (match_class(c
, uchar(*p
)))
232 else if ((*(p
+1) == '-') && (p
+2 < ec
)) {
234 if (uchar(*(p
-2)) <= c
&& c
<= uchar(*p
))
237 else if (uchar(*p
) == c
) return sig
;
242 static int singlematch(int c
, const char *p
, const char *ep
)
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
);
264 while (++s
< ms
->src_end
) {
266 if (--cont
== 0) return s
+1;
267 } else if (*s
== b
) {
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
))
281 /* keeps trying to match with the maximum repetitions */
283 const char *res
= match(ms
, (s
+i
), ep
+1);
285 i
--; /* else didn't match; reduce 1 repetition to try again */
290 static const char *min_expand(MatchState
*ms
, const char *s
,
291 const char *p
, const char *ep
)
294 const char *res
= match(ms
, s
, ep
+1);
297 else if (s
<ms
->src_end
&& singlematch(uchar(*s
), p
, ep
))
298 s
++; /* try with one more repetition */
304 static const char *start_capture(MatchState
*ms
, const char *s
,
305 const char *p
, int what
)
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
;
313 if ((res
=match(ms
, s
, p
)) == NULL
) /* match failed? */
314 ms
->level
--; /* undo capture */
318 static const char *end_capture(MatchState
*ms
, const char *s
,
321 int l
= capture_to_close(ms
);
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 */
329 static const char *match_capture(MatchState
*ms
, const char *s
, int l
)
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)
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 */
347 case '(': /* start capture */
348 if (*(p
+1) == ')') /* position capture? */
349 s
= start_capture(ms
, s
, p
+2, CAP_POSITION
);
351 s
= start_capture(ms
, s
, p
+1, CAP_UNFINISHED
);
353 case ')': /* end capture */
354 s
= end_capture(ms
, s
, p
+1);
358 case 'b': /* balanced string? */
359 s
= matchbalance(ms
, s
, p
+2);
360 if (s
== NULL
) break;
362 goto init
; /* else s = match(ms, s, p+4); */
363 case 'f': { /* frontier? */
364 const char *ep
; char previous
;
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; }
373 goto init
; /* else s = match(ms, s, ep); */
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;
380 goto init
; /* else s = match(ms, s, p+2) */
382 goto dflt
; /* case default */
385 case '\0': /* end of pattern */
386 break; /* match succeeded */
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 */
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
);
396 case '?': { /* optional */
398 if (m
&& ((res
=match(ms
, s
+1, ep
+1)) != NULL
)) {
403 goto init
; /* else s = match(ms, s, ep+1); */
405 case '*': /* 0 or more repetitions */
406 s
= max_expand(ms
, s
, p
, ep
);
408 case '+': /* 1 or more repetitions */
409 s
= (m
? max_expand(ms
, s
+1, p
, ep
) : NULL
);
411 case '-': /* 0 or more repetitions (minimum) */
412 s
= min_expand(ms
, s
, p
, ep
);
415 if (m
) { s
++; p
=ep
; goto init
; } /* else s = match(ms, s+1, ep); */
426 static const char *lmemfind(const char *s1
, size_t l1
,
427 const char *s2
, size_t l2
)
430 return s1
; /* empty strings are everywhere */
431 } else if (l2
> l1
) {
432 return NULL
; /* avoids a negative `l1' */
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) {
441 } else { /* correct `l1' and `s1' to try again */
442 l1
-= (size_t)(init
-s1
);
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 */
456 lj_err_caller(ms
->L
, LJ_ERR_STRCAPI
);
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);
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
)
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
)
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;
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
);
499 lua_pushinteger(L
, s2
-s
+1);
500 lua_pushinteger(L
, s2
-s
+(ptrdiff_t)l2
);
505 int anchor
= (*p
== '^') ? (p
++, 1) : 0;
506 const char *s1
=s
+init
;
512 ms
.level
= ms
.depth
= 0;
513 if ((res
=match(&ms
, s1
, p
)) != NULL
) {
515 lua_pushinteger(L
, s1
-s
+1); /* start */
516 lua_pushinteger(L
, res
-s
); /* end */
517 return push_captures(&ms
, NULL
, 0) + 2;
519 return push_captures(&ms
, s1
, res
);
522 } while (s1
++ < ms
.src_end
&& !anchor
);
524 lua_pushnil(L
); /* not found */
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
;
548 ms
.src_end
= s
+ str
->len
;
549 for (; src
<= ms
.src_end
; src
++) {
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);
568 lj_lib_pushcc(L
, lj_cf_string_gmatch_aux
, FF_string_gmatch_aux
, 3);
572 static void add_s(MatchState
*ms
, luaL_Buffer
*b
, const char *s
, const char *e
)
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
]);
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
));
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)) {
603 case LUA_TFUNCTION
: {
606 n
= push_captures(ms
, s
, e
);
611 push_onecapture(ms
, 0, s
, e
);
616 if (!lua_toboolean(L
, -1)) { /* nil or false? */
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
)
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;
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
);
642 ms
.src_end
= src
+srcl
;
645 ms
.level
= ms
.depth
= 0;
646 e
= match(&ms
, src
, p
);
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
++);
660 luaL_addlstring(&b
, src
, (size_t)(ms
.src_end
-src
));
662 lua_pushinteger(L
, n
); /* number of substitutions */
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
, '"');
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. */
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;
694 } else if (c
>= 10) {
696 d
= (c
* 205) >> 11; c
-= d
* 10; luaL_addchar(b
, '0'+d
);
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) */
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
);
722 strncpy(form
, strfrmt
, (size_t)(p
- strfrmt
+ 1));
723 form
+= p
- strfrmt
+ 1;
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
);
743 lj_lib_checknumber(L
, arg
);
746 return (LUA_INTFRM_T
)intV(o
);
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
);
758 lj_lib_checknumber(L
, arg
);
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
);
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
;
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 */
786 luaL_argerror(L
, arg
, lj_obj_typename
[0]);
787 strfrmt
= scanformat(L
, strfrmt
, form
);
788 switch (*strfrmt
++) {
790 sprintf(buff
, form
, lj_lib_checkint(L
, arg
));
794 sprintf(buff
, form
, num2intfrm(L
, arg
));
796 case 'o': case 'u': case 'x': case 'X':
798 sprintf(buff
, form
, num2uintfrm(L
, arg
));
800 case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A': {
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;
813 for (p
= form
; *p
< 'A' && *p
!= '.'; p
++) ;
814 *p
++ = 's'; *p
= '\0';
815 sprintf(buff
, form
, nbuf
);
818 sprintf(buff
, form
, (double)tv
.n
);
822 addquoted(L
, &b
, arg
);
825 lj_str_pushf(L
, "%p", lua_topointer(L
, arg
));
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
);
837 sprintf(buff
, form
, strdata(str
));
841 lj_err_callerv(L
, LJ_ERR_STRFMTO
, *(strfrmt
-1));
844 luaL_addlstring(&b
, buff
, strlen(buff
));
851 /* ------------------------------------------------------------------------ */
853 #include "lj_libdef.h"
855 LUALIB_API
int luaopen_string(lua_State
*L
)
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");
864 mt
= lj_tab_new(L
, 0, 1);
865 /* NOBARRIER: basemt is a GC root. */
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
));