3 ** Copyright (C) 2005-2011 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) */
154 } capture
[LUA_MAXCAPTURES
];
158 #define SPECIALS "^$*+?.([%-"
160 static int check_capture(MatchState
*ms
, int l
)
163 if (l
< 0 || l
>= ms
->level
|| ms
->capture
[l
].len
== CAP_UNFINISHED
)
164 lj_err_caller(ms
->L
, LJ_ERR_STRCAPI
);
168 static int capture_to_close(MatchState
*ms
)
170 int level
= ms
->level
;
171 for (level
--; level
>=0; level
--)
172 if (ms
->capture
[level
].len
== CAP_UNFINISHED
) return level
;
173 lj_err_caller(ms
->L
, LJ_ERR_STRPATC
);
174 return 0; /* unreachable */
177 static const char *classend(MatchState
*ms
, const char *p
)
182 lj_err_caller(ms
->L
, LJ_ERR_STRPATE
);
186 do { /* look for a `]' */
188 lj_err_caller(ms
->L
, LJ_ERR_STRPATM
);
189 if (*(p
++) == L_ESC
&& *p
!= '\0')
190 p
++; /* skip escapes (e.g. `%]') */
198 static const unsigned char match_class_map
[32] = {
199 0,LJ_CHAR_ALPHA
,0,LJ_CHAR_CNTRL
,LJ_CHAR_DIGIT
,0,0,LJ_CHAR_GRAPH
,0,0,0,0,
200 LJ_CHAR_LOWER
,0,0,0,LJ_CHAR_PUNCT
,0,0,LJ_CHAR_SPACE
,0,
201 LJ_CHAR_UPPER
,0,LJ_CHAR_ALNUM
,LJ_CHAR_XDIGIT
,0,0,0,0,0,0,0
204 static int match_class(int c
, int cl
)
206 if ((cl
& 0xc0) == 0x40) {
207 int t
= match_class_map
[(cl
&0x1f)];
209 t
= lj_char_isa(c
, t
);
210 return (cl
& 0x20) ? t
: !t
;
212 if (cl
== 'z') return c
== 0;
213 if (cl
== 'Z') return c
!= 0;
218 static int matchbracketclass(int c
, const char *p
, const char *ec
)
223 p
++; /* skip the `^' */
228 if (match_class(c
, uchar(*p
)))
231 else if ((*(p
+1) == '-') && (p
+2 < ec
)) {
233 if (uchar(*(p
-2)) <= c
&& c
<= uchar(*p
))
236 else if (uchar(*p
) == c
) return sig
;
241 static int singlematch(int c
, const char *p
, const char *ep
)
244 case '.': return 1; /* matches any char */
245 case L_ESC
: return match_class(c
, uchar(*(p
+1)));
246 case '[': return matchbracketclass(c
, p
, ep
-1);
247 default: return (uchar(*p
) == c
);
251 static const char *match(MatchState
*ms
, const char *s
, const char *p
);
253 static const char *matchbalance(MatchState
*ms
, const char *s
, const char *p
)
255 if (*p
== 0 || *(p
+1) == 0)
256 lj_err_caller(ms
->L
, LJ_ERR_STRPATU
);
263 while (++s
< ms
->src_end
) {
265 if (--cont
== 0) return s
+1;
266 } else if (*s
== b
) {
271 return NULL
; /* string ends out of balance */
274 static const char *max_expand(MatchState
*ms
, const char *s
,
275 const char *p
, const char *ep
)
277 ptrdiff_t i
= 0; /* counts maximum expand for item */
278 while ((s
+i
)<ms
->src_end
&& singlematch(uchar(*(s
+i
)), p
, ep
))
280 /* keeps trying to match with the maximum repetitions */
282 const char *res
= match(ms
, (s
+i
), ep
+1);
284 i
--; /* else didn't match; reduce 1 repetition to try again */
289 static const char *min_expand(MatchState
*ms
, const char *s
,
290 const char *p
, const char *ep
)
293 const char *res
= match(ms
, s
, ep
+1);
296 else if (s
<ms
->src_end
&& singlematch(uchar(*s
), p
, ep
))
297 s
++; /* try with one more repetition */
303 static const char *start_capture(MatchState
*ms
, const char *s
,
304 const char *p
, int what
)
307 int level
= ms
->level
;
308 if (level
>= LUA_MAXCAPTURES
) lj_err_caller(ms
->L
, LJ_ERR_STRCAPN
);
309 ms
->capture
[level
].init
= s
;
310 ms
->capture
[level
].len
= what
;
312 if ((res
=match(ms
, s
, p
)) == NULL
) /* match failed? */
313 ms
->level
--; /* undo capture */
317 static const char *end_capture(MatchState
*ms
, const char *s
,
320 int l
= capture_to_close(ms
);
322 ms
->capture
[l
].len
= s
- ms
->capture
[l
].init
; /* close capture */
323 if ((res
= match(ms
, s
, p
)) == NULL
) /* match failed? */
324 ms
->capture
[l
].len
= CAP_UNFINISHED
; /* undo capture */
328 static const char *match_capture(MatchState
*ms
, const char *s
, int l
)
331 l
= check_capture(ms
, l
);
332 len
= (size_t)ms
->capture
[l
].len
;
333 if ((size_t)(ms
->src_end
-s
) >= len
&&
334 memcmp(ms
->capture
[l
].init
, s
, len
) == 0)
340 static const char *match(MatchState
*ms
, const char *s
, const char *p
)
342 init
: /* using goto's to optimize tail recursion */
344 case '(': /* start capture */
345 if (*(p
+1) == ')') /* position capture? */
346 return start_capture(ms
, s
, p
+2, CAP_POSITION
);
348 return start_capture(ms
, s
, p
+1, CAP_UNFINISHED
);
349 case ')': /* end capture */
350 return end_capture(ms
, s
, p
+1);
353 case 'b': /* balanced string? */
354 s
= matchbalance(ms
, s
, p
+2);
355 if (s
== NULL
) return NULL
;
357 goto init
; /* else return match(ms, s, p+4); */
358 case 'f': { /* frontier? */
359 const char *ep
; char previous
;
362 lj_err_caller(ms
->L
, LJ_ERR_STRPATB
);
363 ep
= classend(ms
, p
); /* points to what is next */
364 previous
= (s
== ms
->src_init
) ? '\0' : *(s
-1);
365 if (matchbracketclass(uchar(previous
), p
, ep
-1) ||
366 !matchbracketclass(uchar(*s
), p
, ep
-1)) return NULL
;
368 goto init
; /* else return match(ms, s, ep); */
371 if (lj_char_isdigit(uchar(*(p
+1)))) { /* capture results (%0-%9)? */
372 s
= match_capture(ms
, s
, uchar(*(p
+1)));
373 if (s
== NULL
) return NULL
;
375 goto init
; /* else return match(ms, s, p+2) */
377 goto dflt
; /* case default */
379 case '\0': /* end of pattern */
380 return s
; /* match succeeded */
382 if (*(p
+1) == '\0') /* is the `$' the last char in pattern? */
383 return (s
== ms
->src_end
) ? s
: NULL
; /* check end of string */
386 default: dflt
: { /* it is a pattern item */
387 const char *ep
= classend(ms
, p
); /* points to what is next */
388 int m
= s
<ms
->src_end
&& singlematch(uchar(*s
), p
, ep
);
390 case '?': { /* optional */
392 if (m
&& ((res
=match(ms
, s
+1, ep
+1)) != NULL
))
395 goto init
; /* else return match(ms, s, ep+1); */
397 case '*': /* 0 or more repetitions */
398 return max_expand(ms
, s
, p
, ep
);
399 case '+': /* 1 or more repetitions */
400 return (m
? max_expand(ms
, s
+1, p
, ep
) : NULL
);
401 case '-': /* 0 or more repetitions (minimum) */
402 return min_expand(ms
, s
, p
, ep
);
406 goto init
; /* else return match(ms, s+1, ep); */
412 static const char *lmemfind(const char *s1
, size_t l1
,
413 const char *s2
, size_t l2
)
416 return s1
; /* empty strings are everywhere */
417 } else if (l2
> l1
) {
418 return NULL
; /* avoids a negative `l1' */
420 const char *init
; /* to search for a `*s2' inside `s1' */
421 l2
--; /* 1st char will be checked by `memchr' */
422 l1
= l1
-l2
; /* `s2' cannot be found after that */
423 while (l1
> 0 && (init
= (const char *)memchr(s1
, *s2
, l1
)) != NULL
) {
424 init
++; /* 1st char is already checked */
425 if (memcmp(init
, s2
+1, l2
) == 0) {
427 } else { /* correct `l1' and `s1' to try again */
428 l1
-= (size_t)(init
-s1
);
432 return NULL
; /* not found */
436 static void push_onecapture(MatchState
*ms
, int i
, const char *s
, const char *e
)
438 if (i
>= ms
->level
) {
439 if (i
== 0) /* ms->level == 0, too */
440 lua_pushlstring(ms
->L
, s
, (size_t)(e
- s
)); /* add whole match */
442 lj_err_caller(ms
->L
, LJ_ERR_STRCAPI
);
444 ptrdiff_t l
= ms
->capture
[i
].len
;
445 if (l
== CAP_UNFINISHED
) lj_err_caller(ms
->L
, LJ_ERR_STRCAPU
);
446 if (l
== CAP_POSITION
)
447 lua_pushinteger(ms
->L
, ms
->capture
[i
].init
- ms
->src_init
+ 1);
449 lua_pushlstring(ms
->L
, ms
->capture
[i
].init
, (size_t)l
);
453 static int push_captures(MatchState
*ms
, const char *s
, const char *e
)
456 int nlevels
= (ms
->level
== 0 && s
) ? 1 : ms
->level
;
457 luaL_checkstack(ms
->L
, nlevels
, "too many captures");
458 for (i
= 0; i
< nlevels
; i
++)
459 push_onecapture(ms
, i
, s
, e
);
460 return nlevels
; /* number of strings pushed */
463 static ptrdiff_t posrelat(ptrdiff_t pos
, size_t len
)
465 /* relative string position: negative means back from end */
466 if (pos
< 0) pos
+= (ptrdiff_t)len
+ 1;
467 return (pos
>= 0) ? pos
: 0;
470 static int str_find_aux(lua_State
*L
, int find
)
473 const char *s
= luaL_checklstring(L
, 1, &l1
);
474 const char *p
= luaL_checklstring(L
, 2, &l2
);
475 ptrdiff_t init
= posrelat(luaL_optinteger(L
, 3, 1), l1
) - 1;
478 else if ((size_t)(init
) > l1
)
479 init
= (ptrdiff_t)l1
;
480 if (find
&& (lua_toboolean(L
, 4) || /* explicit request? */
481 strpbrk(p
, SPECIALS
) == NULL
)) { /* or no special characters? */
482 /* do a plain search */
483 const char *s2
= lmemfind(s
+init
, l1
-(size_t)init
, p
, l2
);
485 lua_pushinteger(L
, s2
-s
+1);
486 lua_pushinteger(L
, s2
-s
+(ptrdiff_t)l2
);
491 int anchor
= (*p
== '^') ? (p
++, 1) : 0;
492 const char *s1
=s
+init
;
499 if ((res
=match(&ms
, s1
, p
)) != NULL
) {
501 lua_pushinteger(L
, s1
-s
+1); /* start */
502 lua_pushinteger(L
, res
-s
); /* end */
503 return push_captures(&ms
, NULL
, 0) + 2;
505 return push_captures(&ms
, s1
, res
);
508 } while (s1
++ < ms
.src_end
&& !anchor
);
510 lua_pushnil(L
); /* not found */
514 LJLIB_CF(string_find
)
516 return str_find_aux(L
, 1);
519 LJLIB_CF(string_match
)
521 return str_find_aux(L
, 0);
524 LJLIB_NOREG
LJLIB_CF(string_gmatch_aux
)
526 const char *p
= strVdata(lj_lib_upvalue(L
, 2));
527 GCstr
*str
= strV(lj_lib_upvalue(L
, 1));
528 const char *s
= strdata(str
);
529 TValue
*tvpos
= lj_lib_upvalue(L
, 3);
530 const char *src
= s
+ tvpos
->u32
.lo
;
534 ms
.src_end
= s
+ str
->len
;
535 for (; src
<= ms
.src_end
; src
++) {
538 if ((e
= match(&ms
, src
, p
)) != NULL
) {
539 int32_t pos
= (int32_t)(e
- s
);
540 if (e
== src
) pos
++; /* Ensure progress for empty match. */
541 tvpos
->u32
.lo
= (uint32_t)pos
;
542 return push_captures(&ms
, src
, e
);
545 return 0; /* not found */
548 LJLIB_CF(string_gmatch
)
550 lj_lib_checkstr(L
, 1);
551 lj_lib_checkstr(L
, 2);
554 lj_lib_pushcc(L
, lj_cf_string_gmatch_aux
, FF_string_gmatch_aux
, 3);
558 static void add_s(MatchState
*ms
, luaL_Buffer
*b
, const char *s
, const char *e
)
561 const char *news
= lua_tolstring(ms
->L
, 3, &l
);
562 for (i
= 0; i
< l
; i
++) {
563 if (news
[i
] != L_ESC
) {
564 luaL_addchar(b
, news
[i
]);
567 if (!lj_char_isdigit(uchar(news
[i
]))) {
568 luaL_addchar(b
, news
[i
]);
569 } else if (news
[i
] == '0') {
570 luaL_addlstring(b
, s
, (size_t)(e
- s
));
572 push_onecapture(ms
, news
[i
] - '1', s
, e
);
573 luaL_addvalue(b
); /* add capture to accumulated result */
579 static void add_value(MatchState
*ms
, luaL_Buffer
*b
,
580 const char *s
, const char *e
)
582 lua_State
*L
= ms
->L
;
583 switch (lua_type(L
, 3)) {
589 case LUA_TFUNCTION
: {
592 n
= push_captures(ms
, s
, e
);
597 push_onecapture(ms
, 0, s
, e
);
602 if (!lua_toboolean(L
, -1)) { /* nil or false? */
604 lua_pushlstring(L
, s
, (size_t)(e
- s
)); /* keep original text */
605 } else if (!lua_isstring(L
, -1)) {
606 lj_err_callerv(L
, LJ_ERR_STRGSRV
, luaL_typename(L
, -1));
608 luaL_addvalue(b
); /* add result to accumulator */
611 LJLIB_CF(string_gsub
)
614 const char *src
= luaL_checklstring(L
, 1, &srcl
);
615 const char *p
= luaL_checkstring(L
, 2);
616 int tr
= lua_type(L
, 3);
617 int max_s
= luaL_optint(L
, 4, (int)(srcl
+1));
618 int anchor
= (*p
== '^') ? (p
++, 1) : 0;
622 if (!(tr
== LUA_TNUMBER
|| tr
== LUA_TSTRING
||
623 tr
== LUA_TFUNCTION
|| tr
== LUA_TTABLE
))
624 lj_err_arg(L
, 3, LJ_ERR_NOSFT
);
625 luaL_buffinit(L
, &b
);
628 ms
.src_end
= src
+srcl
;
632 e
= match(&ms
, src
, p
);
635 add_value(&ms
, &b
, src
, e
);
637 if (e
&& e
>src
) /* non empty match? */
638 src
= e
; /* skip it */
639 else if (src
< ms
.src_end
)
640 luaL_addchar(&b
, *src
++);
646 luaL_addlstring(&b
, src
, (size_t)(ms
.src_end
-src
));
648 lua_pushinteger(L
, n
); /* number of substitutions */
652 /* ------------------------------------------------------------------------ */
654 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
655 #define MAX_FMTITEM 512
656 /* valid flags in a format specification */
657 #define FMT_FLAGS "-+ #0"
659 ** maximum size of each format specification (such as '%-099.99d')
660 ** (+10 accounts for %99.99x plus margin of error)
662 #define MAX_FMTSPEC (sizeof(FMT_FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
664 static void addquoted(lua_State
*L
, luaL_Buffer
*b
, int arg
)
666 GCstr
*str
= lj_lib_checkstr(L
, arg
);
667 int32_t len
= (int32_t)str
->len
;
668 const char *s
= strdata(str
);
669 luaL_addchar(b
, '"');
671 if (*s
== '"' || *s
== '\\' || *s
== '\n') {
672 luaL_addchar(b
, '\\');
674 } else if (lj_char_iscntrl(uchar(*s
))) {
676 luaL_addchar(b
, '\\');
677 c1
= uchar(*s
); c3
= c1
% 10; c1
/= 10; c2
= c1
% 10; c1
/= 10;
678 if (c1
+ lj_char_isdigit(uchar(s
[1]))) luaL_addchar(b
, '0' + c1
);
679 if (c2
+ (c1
+ lj_char_isdigit(uchar(s
[1])))) luaL_addchar(b
, '0' + c2
);
680 luaL_addchar(b
, '0' + c3
);
686 luaL_addchar(b
, '"');
689 static const char *scanformat(lua_State
*L
, const char *strfrmt
, char *form
)
691 const char *p
= strfrmt
;
692 while (*p
!= '\0' && strchr(FMT_FLAGS
, *p
) != NULL
) p
++; /* skip flags */
693 if ((size_t)(p
- strfrmt
) >= sizeof(FMT_FLAGS
))
694 lj_err_caller(L
, LJ_ERR_STRFMTR
);
695 if (lj_char_isdigit(uchar(*p
))) p
++; /* skip width */
696 if (lj_char_isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
699 if (lj_char_isdigit(uchar(*p
))) p
++; /* skip precision */
700 if (lj_char_isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
702 if (lj_char_isdigit(uchar(*p
)))
703 lj_err_caller(L
, LJ_ERR_STRFMTW
);
705 strncpy(form
, strfrmt
, (size_t)(p
- strfrmt
+ 1));
706 form
+= p
- strfrmt
+ 1;
711 static void addintlen(char *form
)
713 size_t l
= strlen(form
);
714 char spec
= form
[l
- 1];
715 strcpy(form
+ l
- 1, LUA_INTFRMLEN
);
716 form
[l
+ sizeof(LUA_INTFRMLEN
) - 2] = spec
;
717 form
[l
+ sizeof(LUA_INTFRMLEN
) - 1] = '\0';
720 static unsigned LUA_INTFRM_T
num2intfrm(lua_State
*L
, int arg
)
722 if (sizeof(LUA_INTFRM_T
) == 4) {
723 return (LUA_INTFRM_T
)lj_lib_checkbit(L
, arg
);
726 lj_lib_checknumber(L
, arg
);
729 return (LUA_INTFRM_T
)intV(o
);
731 return (LUA_INTFRM_T
)numV(o
);
735 static unsigned LUA_INTFRM_T
num2uintfrm(lua_State
*L
, int arg
)
737 if (sizeof(LUA_INTFRM_T
) == 4) {
738 return (unsigned LUA_INTFRM_T
)lj_lib_checkbit(L
, arg
);
741 lj_lib_checknumber(L
, arg
);
744 return (unsigned LUA_INTFRM_T
)intV(o
);
745 else if ((int32_t)o
->u32
.hi
< 0)
746 return (unsigned LUA_INTFRM_T
)(LUA_INTFRM_T
)numV(o
);
748 return (unsigned LUA_INTFRM_T
)numV(o
);
752 LJLIB_CF(string_format
)
754 int arg
= 1, top
= (int)(L
->top
- L
->base
);
755 GCstr
*fmt
= lj_lib_checkstr(L
, arg
);
756 const char *strfrmt
= strdata(fmt
);
757 const char *strfrmt_end
= strfrmt
+ fmt
->len
;
759 luaL_buffinit(L
, &b
);
760 while (strfrmt
< strfrmt_end
) {
761 if (*strfrmt
!= L_ESC
) {
762 luaL_addchar(&b
, *strfrmt
++);
763 } else if (*++strfrmt
== L_ESC
) {
764 luaL_addchar(&b
, *strfrmt
++); /* %% */
765 } else { /* format item */
766 char form
[MAX_FMTSPEC
]; /* to store the format (`%...') */
767 char buff
[MAX_FMTITEM
]; /* to store the formatted item */
769 luaL_argerror(L
, arg
, lj_obj_typename
[0]);
770 strfrmt
= scanformat(L
, strfrmt
, form
);
771 switch (*strfrmt
++) {
773 sprintf(buff
, form
, lj_lib_checkint(L
, arg
));
777 sprintf(buff
, form
, num2intfrm(L
, arg
));
779 case 'o': case 'u': case 'x': case 'X':
781 sprintf(buff
, form
, num2uintfrm(L
, arg
));
783 case 'e': case 'E': case 'f': case 'g': case 'G': {
785 tv
.n
= lj_lib_checknum(L
, arg
);
786 if (LJ_UNLIKELY((tv
.u32
.hi
<< 1) >= 0xffe00000)) {
787 /* Canonicalize output of non-finite values. */
788 char *p
, nbuf
[LJ_STR_NUMBUF
];
789 size_t len
= lj_str_bufnum(nbuf
, &tv
);
790 if (strfrmt
[-1] == 'E' || strfrmt
[-1] == 'G') {
791 nbuf
[len
-3] = nbuf
[len
-3] - 0x20;
792 nbuf
[len
-2] = nbuf
[len
-2] - 0x20;
793 nbuf
[len
-1] = nbuf
[len
-1] - 0x20;
796 for (p
= form
; *p
< 'e' && *p
!= '.'; p
++) ;
797 *p
++ = 's'; *p
= '\0';
798 sprintf(buff
, form
, nbuf
);
801 sprintf(buff
, form
, (double)tv
.n
);
805 addquoted(L
, &b
, arg
);
808 lj_str_pushf(L
, "%p", lua_topointer(L
, arg
));
812 GCstr
*str
= lj_lib_checkstr(L
, arg
);
813 if (!strchr(form
, '.') && str
->len
>= 100) {
814 /* no precision and string is too long to be formatted;
815 keep original string */
816 setstrV(L
, L
->top
++, str
);
820 sprintf(buff
, form
, strdata(str
));
824 lj_err_callerv(L
, LJ_ERR_STRFMTO
, *(strfrmt
-1));
827 luaL_addlstring(&b
, buff
, strlen(buff
));
834 /* ------------------------------------------------------------------------ */
836 #include "lj_libdef.h"
838 LUALIB_API
int luaopen_string(lua_State
*L
)
842 LJ_LIB_REG(L
, LUA_STRLIBNAME
, string
);
843 #if defined(LUA_COMPAT_GFIND)
844 lua_getfield(L
, -1, "gmatch");
845 lua_setfield(L
, -2, "gfind");
847 mt
= lj_tab_new(L
, 0, 1);
848 /* NOBARRIER: basemt is a GC root. */
850 setgcref(basemt_it(g
, LJ_TSTR
), obj2gco(mt
));
851 settabV(L
, lj_tab_setstr(L
, mt
, mmname_str(g
, MM_index
)), tabV(L
->top
-1));
852 mt
->nomm
= (uint8_t)(~(1u<<MM_index
));