3 ** Copyright (C) 2005-2022 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
26 #include "lj_bcdump.h"
30 /* ------------------------------------------------------------------------ */
32 #define LJLIB_MODULE_string
34 LJLIB_ASM(string_len
) LJLIB_REC(.)
36 lj_lib_checkstr(L
, 1);
40 LJLIB_ASM(string_byte
) LJLIB_REC(string_range
0)
42 GCstr
*s
= lj_lib_checkstr(L
, 1);
43 int32_t len
= (int32_t)s
->len
;
44 int32_t start
= lj_lib_optint(L
, 2, 1);
45 int32_t stop
= lj_lib_optint(L
, 3, start
);
47 const unsigned char *p
;
48 if (stop
< 0) stop
+= len
+1;
49 if (start
< 0) start
+= len
+1;
50 if (start
<= 0) start
= 1;
51 if (stop
> len
) stop
= len
;
52 if (start
> stop
) return FFH_RES(0); /* Empty interval: return no results. */
55 if ((uint32_t)n
> LUAI_MAXCSTACK
)
56 lj_err_caller(L
, LJ_ERR_STRSLC
);
57 lj_state_checkstack(L
, (MSize
)n
);
58 p
= (const unsigned char *)strdata(s
) + start
;
59 for (i
= 0; i
< n
; i
++)
60 setintV(L
->base
+ i
-1, p
[i
]);
64 LJLIB_ASM(string_char
)
66 int i
, nargs
= (int)(L
->top
- L
->base
);
67 char *buf
= lj_str_needbuf(L
, &G(L
)->tmpbuf
, (MSize
)nargs
);
68 for (i
= 1; i
<= nargs
; i
++) {
69 int32_t k
= lj_lib_checkint(L
, i
);
71 lj_err_arg(L
, i
, LJ_ERR_BADVAL
);
74 setstrV(L
, L
->base
-1, lj_str_new(L
, buf
, (size_t)nargs
));
78 LJLIB_ASM(string_sub
) LJLIB_REC(string_range
1)
80 lj_lib_checkstr(L
, 1);
81 lj_lib_checkint(L
, 2);
82 setintV(L
->base
+2, lj_lib_optint(L
, 3, -1));
88 GCstr
*s
= lj_lib_checkstr(L
, 1);
89 int32_t k
= lj_lib_checkint(L
, 2);
90 GCstr
*sep
= lj_lib_optstr(L
, 3);
91 int32_t len
= (int32_t)s
->len
;
92 global_State
*g
= G(L
);
98 setstrV(L
, L
->base
-1, &g
->strempty
);
102 tlen
= (int64_t)len
+ sep
->len
;
103 if (tlen
> LJ_MAX_STR
)
104 lj_err_caller(L
, LJ_ERR_STROV
);
106 if (tlen
> LJ_MAX_STR
)
107 lj_err_caller(L
, LJ_ERR_STROV
);
109 tlen
= (int64_t)k
* len
;
110 if (tlen
> LJ_MAX_STR
)
111 lj_err_caller(L
, LJ_ERR_STROV
);
113 if (tlen
== 0) goto empty
;
114 buf
= lj_str_needbuf(L
, &g
->tmpbuf
, (MSize
)tlen
);
117 tlen
-= sep
->len
; /* Ignore trailing separator. */
118 if (k
> 1) { /* Paste one string and one separator. */
120 i
= 0; while (i
< len
) *buf
++ = src
[i
++];
121 src
= strdata(sep
); len
= sep
->len
;
122 i
= 0; while (i
< len
) *buf
++ = src
[i
++];
123 src
= g
->tmpbuf
.buf
; len
+= s
->len
; k
--; /* Now copy that k-1 times. */
128 do { *buf
++ = src
[i
++]; } while (i
< len
);
130 setstrV(L
, L
->base
-1, lj_str_new(L
, g
->tmpbuf
.buf
, (size_t)tlen
));
134 LJLIB_ASM(string_reverse
)
136 GCstr
*s
= lj_lib_checkstr(L
, 1);
137 lj_str_needbuf(L
, &G(L
)->tmpbuf
, s
->len
);
140 LJLIB_ASM_(string_lower
)
141 LJLIB_ASM_(string_upper
)
143 /* ------------------------------------------------------------------------ */
145 static int writer_buf(lua_State
*L
, const void *p
, size_t size
, void *b
)
147 luaL_addlstring((luaL_Buffer
*)b
, (const char *)p
, size
);
152 LJLIB_CF(string_dump
)
154 GCfunc
*fn
= lj_lib_checkfunc(L
, 1);
155 int strip
= L
->base
+1 < L
->top
&& tvistruecond(L
->base
+1);
158 luaL_buffinit(L
, &b
);
159 if (!isluafunc(fn
) || lj_bcwrite(L
, funcproto(fn
), writer_buf
, &b
, strip
))
160 lj_err_caller(L
, LJ_ERR_STRDUMP
);
165 /* ------------------------------------------------------------------------ */
167 /* macro to `unsign' a character */
168 #define uchar(c) ((unsigned char)(c))
170 #define CAP_UNFINISHED (-1)
171 #define CAP_POSITION (-2)
173 typedef struct MatchState
{
174 const char *src_init
; /* init of source string */
175 const char *src_end
; /* end (`\0') of source string */
177 int level
; /* total number of captures (finished or unfinished) */
182 } capture
[LUA_MAXCAPTURES
];
186 #define SPECIALS "^$*+?.([%-"
188 static int check_capture(MatchState
*ms
, int l
)
191 if (l
< 0 || l
>= ms
->level
|| ms
->capture
[l
].len
== CAP_UNFINISHED
)
192 lj_err_caller(ms
->L
, LJ_ERR_STRCAPI
);
196 static int capture_to_close(MatchState
*ms
)
198 int level
= ms
->level
;
199 for (level
--; level
>=0; level
--)
200 if (ms
->capture
[level
].len
== CAP_UNFINISHED
) return level
;
201 lj_err_caller(ms
->L
, LJ_ERR_STRPATC
);
202 return 0; /* unreachable */
205 static const char *classend(MatchState
*ms
, const char *p
)
210 lj_err_caller(ms
->L
, LJ_ERR_STRPATE
);
214 do { /* look for a `]' */
216 lj_err_caller(ms
->L
, LJ_ERR_STRPATM
);
217 if (*(p
++) == L_ESC
&& *p
!= '\0')
218 p
++; /* skip escapes (e.g. `%]') */
226 static const unsigned char match_class_map
[32] = {
227 0,LJ_CHAR_ALPHA
,0,LJ_CHAR_CNTRL
,LJ_CHAR_DIGIT
,0,0,LJ_CHAR_GRAPH
,0,0,0,0,
228 LJ_CHAR_LOWER
,0,0,0,LJ_CHAR_PUNCT
,0,0,LJ_CHAR_SPACE
,0,
229 LJ_CHAR_UPPER
,0,LJ_CHAR_ALNUM
,LJ_CHAR_XDIGIT
,0,0,0,0,0,0,0
232 static int match_class(int c
, int cl
)
234 if ((cl
& 0xc0) == 0x40) {
235 int t
= match_class_map
[(cl
&0x1f)];
237 t
= lj_char_isa(c
, t
);
238 return (cl
& 0x20) ? t
: !t
;
240 if (cl
== 'z') return c
== 0;
241 if (cl
== 'Z') return c
!= 0;
246 static int matchbracketclass(int c
, const char *p
, const char *ec
)
251 p
++; /* skip the `^' */
256 if (match_class(c
, uchar(*p
)))
259 else if ((*(p
+1) == '-') && (p
+2 < ec
)) {
261 if (uchar(*(p
-2)) <= c
&& c
<= uchar(*p
))
264 else if (uchar(*p
) == c
) return sig
;
269 static int singlematch(int c
, const char *p
, const char *ep
)
272 case '.': return 1; /* matches any char */
273 case L_ESC
: return match_class(c
, uchar(*(p
+1)));
274 case '[': return matchbracketclass(c
, p
, ep
-1);
275 default: return (uchar(*p
) == c
);
279 static const char *match(MatchState
*ms
, const char *s
, const char *p
);
281 static const char *matchbalance(MatchState
*ms
, const char *s
, const char *p
)
283 if (*p
== 0 || *(p
+1) == 0)
284 lj_err_caller(ms
->L
, LJ_ERR_STRPATU
);
291 while (++s
< ms
->src_end
) {
293 if (--cont
== 0) return s
+1;
294 } else if (*s
== b
) {
299 return NULL
; /* string ends out of balance */
302 static const char *max_expand(MatchState
*ms
, const char *s
,
303 const char *p
, const char *ep
)
305 ptrdiff_t i
= 0; /* counts maximum expand for item */
306 while ((s
+i
)<ms
->src_end
&& singlematch(uchar(*(s
+i
)), p
, ep
))
308 /* keeps trying to match with the maximum repetitions */
310 const char *res
= match(ms
, (s
+i
), ep
+1);
312 i
--; /* else didn't match; reduce 1 repetition to try again */
317 static const char *min_expand(MatchState
*ms
, const char *s
,
318 const char *p
, const char *ep
)
321 const char *res
= match(ms
, s
, ep
+1);
324 else if (s
<ms
->src_end
&& singlematch(uchar(*s
), p
, ep
))
325 s
++; /* try with one more repetition */
331 static const char *start_capture(MatchState
*ms
, const char *s
,
332 const char *p
, int what
)
335 int level
= ms
->level
;
336 if (level
>= LUA_MAXCAPTURES
) lj_err_caller(ms
->L
, LJ_ERR_STRCAPN
);
337 ms
->capture
[level
].init
= s
;
338 ms
->capture
[level
].len
= what
;
340 if ((res
=match(ms
, s
, p
)) == NULL
) /* match failed? */
341 ms
->level
--; /* undo capture */
345 static const char *end_capture(MatchState
*ms
, const char *s
,
348 int l
= capture_to_close(ms
);
350 ms
->capture
[l
].len
= s
- ms
->capture
[l
].init
; /* close capture */
351 if ((res
= match(ms
, s
, p
)) == NULL
) /* match failed? */
352 ms
->capture
[l
].len
= CAP_UNFINISHED
; /* undo capture */
356 static const char *match_capture(MatchState
*ms
, const char *s
, int l
)
359 l
= check_capture(ms
, l
);
360 len
= (size_t)ms
->capture
[l
].len
;
361 if ((size_t)(ms
->src_end
-s
) >= len
&&
362 memcmp(ms
->capture
[l
].init
, s
, len
) == 0)
368 static const char *match(MatchState
*ms
, const char *s
, const char *p
)
370 if (++ms
->depth
> LJ_MAX_XLEVEL
)
371 lj_err_caller(ms
->L
, LJ_ERR_STRPATX
);
372 init
: /* using goto's to optimize tail recursion */
374 case '(': /* start capture */
375 if (*(p
+1) == ')') /* position capture? */
376 s
= start_capture(ms
, s
, p
+2, CAP_POSITION
);
378 s
= start_capture(ms
, s
, p
+1, CAP_UNFINISHED
);
380 case ')': /* end capture */
381 s
= end_capture(ms
, s
, p
+1);
385 case 'b': /* balanced string? */
386 s
= matchbalance(ms
, s
, p
+2);
387 if (s
== NULL
) break;
389 goto init
; /* else s = match(ms, s, p+4); */
390 case 'f': { /* frontier? */
391 const char *ep
; char previous
;
394 lj_err_caller(ms
->L
, LJ_ERR_STRPATB
);
395 ep
= classend(ms
, p
); /* points to what is next */
396 previous
= (s
== ms
->src_init
) ? '\0' : *(s
-1);
397 if (matchbracketclass(uchar(previous
), p
, ep
-1) ||
398 !matchbracketclass(uchar(*s
), p
, ep
-1)) { s
= NULL
; break; }
400 goto init
; /* else s = match(ms, s, ep); */
403 if (lj_char_isdigit(uchar(*(p
+1)))) { /* capture results (%0-%9)? */
404 s
= match_capture(ms
, s
, uchar(*(p
+1)));
405 if (s
== NULL
) break;
407 goto init
; /* else s = match(ms, s, p+2) */
409 goto dflt
; /* case default */
412 case '\0': /* end of pattern */
413 break; /* match succeeded */
415 /* is the `$' the last char in pattern? */
416 if (*(p
+1) != '\0') goto dflt
;
417 if (s
!= ms
->src_end
) s
= NULL
; /* check end of string */
419 default: dflt
: { /* it is a pattern item */
420 const char *ep
= classend(ms
, p
); /* points to what is next */
421 int m
= s
<ms
->src_end
&& singlematch(uchar(*s
), p
, ep
);
423 case '?': { /* optional */
425 if (m
&& ((res
=match(ms
, s
+1, ep
+1)) != NULL
)) {
430 goto init
; /* else s = match(ms, s, ep+1); */
432 case '*': /* 0 or more repetitions */
433 s
= max_expand(ms
, s
, p
, ep
);
435 case '+': /* 1 or more repetitions */
436 s
= (m
? max_expand(ms
, s
+1, p
, ep
) : NULL
);
438 case '-': /* 0 or more repetitions (minimum) */
439 s
= min_expand(ms
, s
, p
, ep
);
442 if (m
) { s
++; p
=ep
; goto init
; } /* else s = match(ms, s+1, ep); */
453 static const char *lmemfind(const char *s1
, size_t l1
,
454 const char *s2
, size_t l2
)
457 return s1
; /* empty strings are everywhere */
458 } else if (l2
> l1
) {
459 return NULL
; /* avoids a negative `l1' */
461 const char *init
; /* to search for a `*s2' inside `s1' */
462 l2
--; /* 1st char will be checked by `memchr' */
463 l1
= l1
-l2
; /* `s2' cannot be found after that */
464 while (l1
> 0 && (init
= (const char *)memchr(s1
, *s2
, l1
)) != NULL
) {
465 init
++; /* 1st char is already checked */
466 if (memcmp(init
, s2
+1, l2
) == 0) {
468 } else { /* correct `l1' and `s1' to try again */
469 l1
-= (size_t)(init
-s1
);
473 return NULL
; /* not found */
477 static void push_onecapture(MatchState
*ms
, int i
, const char *s
, const char *e
)
479 if (i
>= ms
->level
) {
480 if (i
== 0) /* ms->level == 0, too */
481 lua_pushlstring(ms
->L
, s
, (size_t)(e
- s
)); /* add whole match */
483 lj_err_caller(ms
->L
, LJ_ERR_STRCAPI
);
485 ptrdiff_t l
= ms
->capture
[i
].len
;
486 if (l
== CAP_UNFINISHED
) lj_err_caller(ms
->L
, LJ_ERR_STRCAPU
);
487 if (l
== CAP_POSITION
)
488 lua_pushinteger(ms
->L
, ms
->capture
[i
].init
- ms
->src_init
+ 1);
490 lua_pushlstring(ms
->L
, ms
->capture
[i
].init
, (size_t)l
);
494 static int push_captures(MatchState
*ms
, const char *s
, const char *e
)
497 int nlevels
= (ms
->level
== 0 && s
) ? 1 : ms
->level
;
498 luaL_checkstack(ms
->L
, nlevels
, "too many captures");
499 for (i
= 0; i
< nlevels
; i
++)
500 push_onecapture(ms
, i
, s
, e
);
501 return nlevels
; /* number of strings pushed */
504 static ptrdiff_t posrelat(ptrdiff_t pos
, size_t len
)
506 /* relative string position: negative means back from end */
507 if (pos
< 0) pos
+= (ptrdiff_t)len
+ 1;
508 return (pos
>= 0) ? pos
: 0;
511 static int str_find_aux(lua_State
*L
, int find
)
514 const char *s
= luaL_checklstring(L
, 1, &l1
);
515 const char *p
= luaL_checklstring(L
, 2, &l2
);
516 ptrdiff_t init
= posrelat(luaL_optinteger(L
, 3, 1), l1
) - 1;
519 } else if ((size_t)(init
) > l1
) {
524 init
= (ptrdiff_t)l1
;
527 if (find
&& (lua_toboolean(L
, 4) || /* explicit request? */
528 strpbrk(p
, SPECIALS
) == NULL
)) { /* or no special characters? */
529 /* do a plain search */
530 const char *s2
= lmemfind(s
+init
, l1
-(size_t)init
, p
, l2
);
532 lua_pushinteger(L
, s2
-s
+1);
533 lua_pushinteger(L
, s2
-s
+(ptrdiff_t)l2
);
538 int anchor
= (*p
== '^') ? (p
++, 1) : 0;
539 const char *s1
=s
+init
;
545 ms
.level
= ms
.depth
= 0;
546 if ((res
=match(&ms
, s1
, p
)) != NULL
) {
548 lua_pushinteger(L
, s1
-s
+1); /* start */
549 lua_pushinteger(L
, res
-s
); /* end */
550 return push_captures(&ms
, NULL
, 0) + 2;
552 return push_captures(&ms
, s1
, res
);
555 } while (s1
++ < ms
.src_end
&& !anchor
);
557 lua_pushnil(L
); /* not found */
561 LJLIB_CF(string_find
)
563 return str_find_aux(L
, 1);
566 LJLIB_CF(string_match
)
568 return str_find_aux(L
, 0);
571 LJLIB_NOREG
LJLIB_CF(string_gmatch_aux
)
573 const char *p
= strVdata(lj_lib_upvalue(L
, 2));
574 GCstr
*str
= strV(lj_lib_upvalue(L
, 1));
575 const char *s
= strdata(str
);
576 TValue
*tvpos
= lj_lib_upvalue(L
, 3);
577 const char *src
= s
+ tvpos
->u32
.lo
;
581 ms
.src_end
= s
+ str
->len
;
582 for (; src
<= ms
.src_end
; src
++) {
584 ms
.level
= ms
.depth
= 0;
585 if ((e
= match(&ms
, src
, p
)) != NULL
) {
586 int32_t pos
= (int32_t)(e
- s
);
587 if (e
== src
) pos
++; /* Ensure progress for empty match. */
588 tvpos
->u32
.lo
= (uint32_t)pos
;
589 return push_captures(&ms
, src
, e
);
592 return 0; /* not found */
595 LJLIB_CF(string_gmatch
)
597 lj_lib_checkstr(L
, 1);
598 lj_lib_checkstr(L
, 2);
601 lj_lib_pushcc(L
, lj_cf_string_gmatch_aux
, FF_string_gmatch_aux
, 3);
605 static void add_s(MatchState
*ms
, luaL_Buffer
*b
, const char *s
, const char *e
)
608 const char *news
= lua_tolstring(ms
->L
, 3, &l
);
609 for (i
= 0; i
< l
; i
++) {
610 if (news
[i
] != L_ESC
) {
611 luaL_addchar(b
, news
[i
]);
614 if (!lj_char_isdigit(uchar(news
[i
]))) {
615 luaL_addchar(b
, news
[i
]);
616 } else if (news
[i
] == '0') {
617 luaL_addlstring(b
, s
, (size_t)(e
- s
));
619 push_onecapture(ms
, news
[i
] - '1', s
, e
);
620 luaL_addvalue(b
); /* add capture to accumulated result */
626 static void add_value(MatchState
*ms
, luaL_Buffer
*b
,
627 const char *s
, const char *e
)
629 lua_State
*L
= ms
->L
;
630 switch (lua_type(L
, 3)) {
636 case LUA_TFUNCTION
: {
639 n
= push_captures(ms
, s
, e
);
644 push_onecapture(ms
, 0, s
, e
);
649 if (!lua_toboolean(L
, -1)) { /* nil or false? */
651 lua_pushlstring(L
, s
, (size_t)(e
- s
)); /* keep original text */
652 } else if (!lua_isstring(L
, -1)) {
653 lj_err_callerv(L
, LJ_ERR_STRGSRV
, luaL_typename(L
, -1));
655 luaL_addvalue(b
); /* add result to accumulator */
658 LJLIB_CF(string_gsub
)
661 const char *src
= luaL_checklstring(L
, 1, &srcl
);
662 const char *p
= luaL_checkstring(L
, 2);
663 int tr
= lua_type(L
, 3);
664 int max_s
= luaL_optint(L
, 4, (int)(srcl
+1));
665 int anchor
= (*p
== '^') ? (p
++, 1) : 0;
669 if (!(tr
== LUA_TNUMBER
|| tr
== LUA_TSTRING
||
670 tr
== LUA_TFUNCTION
|| tr
== LUA_TTABLE
))
671 lj_err_arg(L
, 3, LJ_ERR_NOSFT
);
672 luaL_buffinit(L
, &b
);
675 ms
.src_end
= src
+srcl
;
678 ms
.level
= ms
.depth
= 0;
679 e
= match(&ms
, src
, p
);
682 add_value(&ms
, &b
, src
, e
);
684 if (e
&& e
>src
) /* non empty match? */
685 src
= e
; /* skip it */
686 else if (src
< ms
.src_end
)
687 luaL_addchar(&b
, *src
++);
693 luaL_addlstring(&b
, src
, (size_t)(ms
.src_end
-src
));
695 lua_pushinteger(L
, n
); /* number of substitutions */
699 /* ------------------------------------------------------------------------ */
701 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
702 #define MAX_FMTITEM 512
703 /* valid flags in a format specification */
704 #define FMT_FLAGS "-+ #0"
706 ** maximum size of each format specification (such as '%-099.99d')
707 ** (+10 accounts for %99.99x plus margin of error)
709 #define MAX_FMTSPEC (sizeof(FMT_FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
711 static void addquoted(lua_State
*L
, luaL_Buffer
*b
, int arg
)
713 GCstr
*str
= lj_lib_checkstr(L
, arg
);
714 int32_t len
= (int32_t)str
->len
;
715 const char *s
= strdata(str
);
716 luaL_addchar(b
, '"');
718 uint32_t c
= uchar(*s
);
719 if (c
== '"' || c
== '\\' || c
== '\n') {
720 luaL_addchar(b
, '\\');
721 } else if (lj_char_iscntrl(c
)) { /* This can only be 0-31 or 127. */
723 luaL_addchar(b
, '\\');
724 if (c
>= 100 || lj_char_isdigit(uchar(s
[1]))) {
725 luaL_addchar(b
, '0'+(c
>= 100)); if (c
>= 100) c
-= 100;
727 } else if (c
>= 10) {
729 d
= (c
* 205) >> 11; c
-= d
* 10; luaL_addchar(b
, '0'+d
);
736 luaL_addchar(b
, '"');
739 static const char *scanformat(lua_State
*L
, const char *strfrmt
, char *form
)
741 const char *p
= strfrmt
;
742 while (*p
!= '\0' && strchr(FMT_FLAGS
, *p
) != NULL
) p
++; /* skip flags */
743 if ((size_t)(p
- strfrmt
) >= sizeof(FMT_FLAGS
))
744 lj_err_caller(L
, LJ_ERR_STRFMTR
);
745 if (lj_char_isdigit(uchar(*p
))) p
++; /* skip width */
746 if (lj_char_isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
749 if (lj_char_isdigit(uchar(*p
))) p
++; /* skip precision */
750 if (lj_char_isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
752 if (lj_char_isdigit(uchar(*p
)))
753 lj_err_caller(L
, LJ_ERR_STRFMTW
);
755 strncpy(form
, strfrmt
, (size_t)(p
- strfrmt
+ 1));
756 form
+= p
- strfrmt
+ 1;
761 static void addintlen(char *form
)
763 size_t l
= strlen(form
);
764 char spec
= form
[l
- 1];
765 strcpy(form
+ l
- 1, LUA_INTFRMLEN
);
766 form
[l
+ sizeof(LUA_INTFRMLEN
) - 2] = spec
;
767 form
[l
+ sizeof(LUA_INTFRMLEN
) - 1] = '\0';
770 static unsigned LUA_INTFRM_T
num2intfrm(lua_State
*L
, int arg
)
772 if (sizeof(LUA_INTFRM_T
) == 4) {
773 return (LUA_INTFRM_T
)lj_lib_checkbit(L
, arg
);
776 lj_lib_checknumber(L
, arg
);
779 return (LUA_INTFRM_T
)intV(o
);
781 return (LUA_INTFRM_T
)numV(o
);
785 static unsigned LUA_INTFRM_T
num2uintfrm(lua_State
*L
, int arg
)
787 if (sizeof(LUA_INTFRM_T
) == 4) {
788 return (unsigned LUA_INTFRM_T
)lj_lib_checkbit(L
, arg
);
791 lj_lib_checknumber(L
, arg
);
794 return (unsigned LUA_INTFRM_T
)intV(o
);
795 else if ((int32_t)o
->u32
.hi
< 0)
796 return (unsigned LUA_INTFRM_T
)(LUA_INTFRM_T
)numV(o
);
798 return (unsigned LUA_INTFRM_T
)numV(o
);
802 static GCstr
*meta_tostring(lua_State
*L
, int arg
)
804 TValue
*o
= L
->base
+arg
-1;
806 lua_assert(o
< L
->top
); /* Caller already checks for existence. */
807 if (LJ_LIKELY(tvisstr(o
)))
809 if (!tvisnil(mo
= lj_meta_lookup(L
, o
, MM_tostring
))) {
810 copyTV(L
, L
->top
++, mo
);
811 copyTV(L
, L
->top
++, o
);
817 copyTV(L
, o
, L
->top
);
820 return lj_str_fromnumber(L
, o
);
821 } else if (tvisnil(o
)) {
822 return lj_str_newlit(L
, "nil");
823 } else if (tvisfalse(o
)) {
824 return lj_str_newlit(L
, "false");
825 } else if (tvistrue(o
)) {
826 return lj_str_newlit(L
, "true");
828 if (tvisfunc(o
) && isffunc(funcV(o
)))
829 lj_str_pushf(L
, "function: builtin#%d", funcV(o
)->c
.ffid
);
831 lj_str_pushf(L
, "%s: %p", lj_typename(o
), lua_topointer(L
, arg
));
837 LJLIB_CF(string_format
)
839 int arg
= 1, top
= (int)(L
->top
- L
->base
);
840 GCstr
*fmt
= lj_lib_checkstr(L
, arg
);
841 const char *strfrmt
= strdata(fmt
);
842 const char *strfrmt_end
= strfrmt
+ fmt
->len
;
844 luaL_buffinit(L
, &b
);
845 while (strfrmt
< strfrmt_end
) {
846 if (*strfrmt
!= L_ESC
) {
847 luaL_addchar(&b
, *strfrmt
++);
848 } else if (*++strfrmt
== L_ESC
) {
849 luaL_addchar(&b
, *strfrmt
++); /* %% */
850 } else { /* format item */
851 char form
[MAX_FMTSPEC
]; /* to store the format (`%...') */
852 char buff
[MAX_FMTITEM
]; /* to store the formatted item */
855 luaL_argerror(L
, arg
, lj_obj_typename
[0]);
856 strfrmt
= scanformat(L
, strfrmt
, form
);
857 switch (*strfrmt
++) {
859 n
= sprintf(buff
, form
, lj_lib_checkint(L
, arg
));
863 n
= sprintf(buff
, form
, num2intfrm(L
, arg
));
865 case 'o': case 'u': case 'x': case 'X':
867 n
= sprintf(buff
, form
, num2uintfrm(L
, arg
));
869 case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A': {
871 tv
.n
= lj_lib_checknum(L
, arg
);
872 if (LJ_UNLIKELY((tv
.u32
.hi
<< 1) >= 0xffe00000)) {
873 /* Canonicalize output of non-finite values. */
874 char *p
, nbuf
[LJ_STR_NUMBUF
];
875 size_t len
= lj_str_bufnum(nbuf
, &tv
);
876 if (strfrmt
[-1] < 'a') {
877 nbuf
[len
-3] = nbuf
[len
-3] - 0x20;
878 nbuf
[len
-2] = nbuf
[len
-2] - 0x20;
879 nbuf
[len
-1] = nbuf
[len
-1] - 0x20;
882 for (p
= form
; *p
< 'A' && *p
!= '.'; p
++) ;
883 *p
++ = 's'; *p
= '\0';
884 n
= sprintf(buff
, form
, nbuf
);
887 n
= sprintf(buff
, form
, (double)tv
.n
);
891 addquoted(L
, &b
, arg
);
894 lj_str_pushf(L
, "%p", lua_topointer(L
, arg
));
898 GCstr
*str
= meta_tostring(L
, arg
);
899 if (!strchr(form
, '.') && str
->len
>= 100) {
900 /* no precision and string is too long to be formatted;
901 keep original string */
902 setstrV(L
, L
->top
++, str
);
906 n
= sprintf(buff
, form
, strdata(str
));
910 lj_err_callerv(L
, LJ_ERR_STRFMTO
, *(strfrmt
-1));
913 luaL_addlstring(&b
, buff
, n
);
920 /* ------------------------------------------------------------------------ */
922 #include "lj_libdef.h"
924 LUALIB_API
int luaopen_string(lua_State
*L
)
928 LJ_LIB_REG(L
, LUA_STRLIBNAME
, string
);
929 #if defined(LUA_COMPAT_GFIND) && !LJ_52
930 lua_getfield(L
, -1, "gmatch");
931 lua_setfield(L
, -2, "gfind");
933 mt
= lj_tab_new(L
, 0, 1);
934 /* NOBARRIER: basemt is a GC root. */
936 setgcref(basemt_it(g
, LJ_TSTR
), obj2gco(mt
));
937 settabV(L
, lj_tab_setstr(L
, mt
, mmname_str(g
, MM_index
)), tabV(L
->top
-1));
938 mt
->nomm
= (uint8_t)(~(1u<<MM_index
));