2 ** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $
3 ** Standard library for string operations and pattern-matching
4 ** See Copyright Notice in lua.h
9 #include <sys/zfs_context.h>
21 ** maximum number of captures that a pattern can do during
22 ** pattern-matching. This limit is arbitrary.
24 #if !defined(LUA_MAXCAPTURES)
25 #define LUA_MAXCAPTURES 32
29 /* macro to `unsign' a character */
30 #define uchar(c) ((unsigned char)(c))
33 * PATCHED: add missing character macros.
35 #define tolower(C) (((C) >= 'A' && (C) <= 'Z') ? (C) - 'A' + 'a' : (C))
36 #define toupper(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A': (C))
37 #define isgraph(C) ((C) >= 0x21 && (C) <= 0x7E)
38 #define ispunct(C) (((C) >= 0x21 && (C) <= 0x2F) || \
39 ((C) >= 0x3A && (C) <= 0x40) || \
40 ((C) >= 0x5B && (C) <= 0x60) || \
41 ((C) >= 0x7B && (C) <= 0x7E))
44 * The provided version of sprintf returns a char *, but str_format expects
45 * it to return the number of characters printed. This version has the expected
48 static size_t str_sprintf(char *buf
, const char *fmt
, ...) {
53 len
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
60 static int str_len (lua_State
*L
) {
62 luaL_checklstring(L
, 1, &l
);
63 lua_pushinteger(L
, (lua_Integer
)l
);
68 /* translate a relative string position: negative means back from end */
69 static size_t posrelat (ptrdiff_t pos
, size_t len
) {
70 if (pos
>= 0) return (size_t)pos
;
71 else if (0u - (size_t)pos
> len
) return 0;
72 else return len
- ((size_t)-pos
) + 1;
76 static int str_sub (lua_State
*L
) {
78 const char *s
= luaL_checklstring(L
, 1, &l
);
79 size_t start
= posrelat(luaL_checkinteger(L
, 2), l
);
80 size_t end
= posrelat(luaL_optinteger(L
, 3, -1), l
);
81 if (start
< 1) start
= 1;
84 lua_pushlstring(L
, s
+ start
- 1, end
- start
+ 1);
85 else lua_pushliteral(L
, "");
90 static int str_reverse (lua_State
*L
) {
93 const char *s
= luaL_checklstring(L
, 1, &l
);
94 char *p
= luaL_buffinitsize(L
, &b
, l
);
95 for (i
= 0; i
< l
; i
++)
97 luaL_pushresultsize(&b
, l
);
102 static int str_lower (lua_State
*L
) {
106 const char *s
= luaL_checklstring(L
, 1, &l
);
107 char *p
= luaL_buffinitsize(L
, &b
, l
);
109 p
[i
] = tolower(uchar(s
[i
]));
110 luaL_pushresultsize(&b
, l
);
115 static int str_upper (lua_State
*L
) {
119 const char *s
= luaL_checklstring(L
, 1, &l
);
120 char *p
= luaL_buffinitsize(L
, &b
, l
);
122 p
[i
] = toupper(uchar(s
[i
]));
123 luaL_pushresultsize(&b
, l
);
128 /* reasonable limit to avoid arithmetic overflow */
129 #define MAXSIZE ((~(size_t)0) >> 1)
131 static int str_rep (lua_State
*L
) {
133 const char *s
= luaL_checklstring(L
, 1, &l
);
134 int n
= luaL_checkint(L
, 2);
135 const char *sep
= luaL_optlstring(L
, 3, "", &lsep
);
136 if (n
<= 0) lua_pushliteral(L
, "");
137 else if (l
+ lsep
< l
|| l
+ lsep
>= MAXSIZE
/ n
) /* may overflow? */
138 return luaL_error(L
, "resulting string too large");
140 size_t totallen
= n
* l
+ (n
- 1) * lsep
;
142 char *p
= luaL_buffinitsize(L
, &b
, totallen
);
143 while (n
-- > 1) { /* first n-1 copies (followed by separator) */
144 memcpy(p
, s
, l
* sizeof(char)); p
+= l
;
145 if (lsep
> 0) { /* avoid empty 'memcpy' (may be expensive) */
146 memcpy(p
, sep
, lsep
* sizeof(char)); p
+= lsep
;
149 memcpy(p
, s
, l
* sizeof(char)); /* last copy (not followed by separator) */
150 luaL_pushresultsize(&b
, totallen
);
156 static int str_byte (lua_State
*L
) {
158 const char *s
= luaL_checklstring(L
, 1, &l
);
159 size_t posi
= posrelat(luaL_optinteger(L
, 2, 1), l
);
160 size_t pose
= posrelat(luaL_optinteger(L
, 3, posi
), l
);
162 if (posi
< 1) posi
= 1;
163 if (pose
> l
) pose
= l
;
164 if (posi
> pose
) return 0; /* empty interval; return no values */
165 n
= (int)(pose
- posi
+ 1);
166 if (posi
+ n
<= pose
) /* (size_t -> int) overflow? */
167 return luaL_error(L
, "string slice too long");
168 luaL_checkstack(L
, n
, "string slice too long");
170 lua_pushinteger(L
, uchar(s
[posi
+i
-1]));
175 static int str_char (lua_State
*L
) {
176 int n
= lua_gettop(L
); /* number of arguments */
179 char *p
= luaL_buffinitsize(L
, &b
, n
);
180 for (i
=1; i
<=n
; i
++) {
181 int c
= luaL_checkint(L
, i
);
182 luaL_argcheck(L
, uchar(c
) == c
, i
, "value out of range");
185 luaL_pushresultsize(&b
, n
);
190 static int writer (lua_State
*L
, const void* b
, size_t size
, void* B
) {
192 luaL_addlstring((luaL_Buffer
*) B
, (const char *)b
, size
);
197 static int str_dump (lua_State
*L
) {
199 luaL_checktype(L
, 1, LUA_TFUNCTION
);
202 if (lua_dump(L
, writer
, &b
) != 0)
203 return luaL_error(L
, "unable to dump given function");
211 ** {======================================================
213 ** =======================================================
217 #define CAP_UNFINISHED (-1)
218 #define CAP_POSITION (-2)
221 typedef struct MatchState
{
222 int matchdepth
; /* control for recursive depth (to avoid C stack overflow) */
223 const char *src_init
; /* init of source string */
224 const char *src_end
; /* end ('\0') of source string */
225 const char *p_end
; /* end ('\0') of pattern */
227 int level
; /* total number of captures (finished or unfinished) */
231 } capture
[LUA_MAXCAPTURES
];
235 /* recursive function */
236 static const char *match (MatchState
*ms
, const char *s
, const char *p
);
239 /* maximum recursion depth for 'match' */
240 #if !defined(MAXCCALLS)
241 #define MAXCCALLS 200
246 #define SPECIALS "^$*+?.([%-"
249 static int check_capture (MatchState
*ms
, int l
) {
251 if (l
< 0 || l
>= ms
->level
|| ms
->capture
[l
].len
== CAP_UNFINISHED
)
252 return luaL_error(ms
->L
, "invalid capture index %%%d", l
+ 1);
257 static int capture_to_close (MatchState
*ms
) {
258 int level
= ms
->level
;
259 for (level
--; level
>=0; level
--)
260 if (ms
->capture
[level
].len
== CAP_UNFINISHED
) return level
;
261 return luaL_error(ms
->L
, "invalid pattern capture");
265 static const char *classend (MatchState
*ms
, const char *p
) {
269 luaL_error(ms
->L
, "malformed pattern (ends with " LUA_QL("%%") ")");
274 do { /* look for a `]' */
276 luaL_error(ms
->L
, "malformed pattern (missing " LUA_QL("]") ")");
277 if (*(p
++) == L_ESC
&& p
< ms
->p_end
)
278 p
++; /* skip escapes (e.g. `%]') */
292 return ((c
< 0x20) || (c
== 0x7f));
296 static int match_class (int c
, int cl
) {
298 switch (tolower(cl
)) {
299 case 'a' : res
= isalpha(c
); break;
300 case 'c' : res
= iscntrl(c
); break;
301 case 'd' : res
= isdigit(c
); break;
302 case 'g' : res
= isgraph(c
); break;
303 case 'l' : res
= islower(c
); break;
304 case 'p' : res
= ispunct(c
); break;
305 case 's' : res
= isspace(c
); break;
306 case 'u' : res
= isupper(c
); break;
307 case 'w' : res
= isalnum(c
); break;
308 case 'x' : res
= isxdigit(c
); break;
309 case 'z' : res
= (c
== 0); break; /* deprecated option */
310 default: return (cl
== c
);
312 return (islower(cl
) ? res
: !res
);
316 static int matchbracketclass (int c
, const char *p
, const char *ec
) {
320 p
++; /* skip the `^' */
325 if (match_class(c
, uchar(*p
)))
328 else if ((*(p
+1) == '-') && (p
+2 < ec
)) {
330 if (uchar(*(p
-2)) <= c
&& c
<= uchar(*p
))
333 else if (uchar(*p
) == c
) return sig
;
339 static int singlematch (MatchState
*ms
, const char *s
, const char *p
,
341 if (s
>= ms
->src_end
)
346 case '.': return 1; /* matches any char */
347 case L_ESC
: return match_class(c
, uchar(*(p
+1)));
348 case '[': return matchbracketclass(c
, p
, ep
-1);
349 default: return (uchar(*p
) == c
);
355 static const char *matchbalance (MatchState
*ms
, const char *s
,
357 if (p
>= ms
->p_end
- 1)
358 luaL_error(ms
->L
, "malformed pattern "
359 "(missing arguments to " LUA_QL("%%b") ")");
360 if (*s
!= *p
) return NULL
;
365 while (++s
< ms
->src_end
) {
367 if (--cont
== 0) return s
+1;
369 else if (*s
== b
) cont
++;
372 return NULL
; /* string ends out of balance */
376 static const char *max_expand (MatchState
*ms
, const char *s
,
377 const char *p
, const char *ep
) {
378 ptrdiff_t i
= 0; /* counts maximum expand for item */
379 while (singlematch(ms
, s
+ i
, p
, ep
))
381 /* keeps trying to match with the maximum repetitions */
383 const char *res
= match(ms
, (s
+i
), ep
+1);
385 i
--; /* else didn't match; reduce 1 repetition to try again */
391 static const char *min_expand (MatchState
*ms
, const char *s
,
392 const char *p
, const char *ep
) {
394 const char *res
= match(ms
, s
, ep
+1);
397 else if (singlematch(ms
, s
, p
, ep
))
398 s
++; /* try with one more repetition */
404 static const char *start_capture (MatchState
*ms
, const char *s
,
405 const char *p
, int what
) {
407 int level
= ms
->level
;
408 if (level
>= LUA_MAXCAPTURES
) luaL_error(ms
->L
, "too many captures");
409 ms
->capture
[level
].init
= s
;
410 ms
->capture
[level
].len
= what
;
412 if ((res
=match(ms
, s
, p
)) == NULL
) /* match failed? */
413 ms
->level
--; /* undo capture */
418 static const char *end_capture (MatchState
*ms
, const char *s
,
420 int l
= capture_to_close(ms
);
422 ms
->capture
[l
].len
= s
- ms
->capture
[l
].init
; /* close capture */
423 if ((res
= match(ms
, s
, p
)) == NULL
) /* match failed? */
424 ms
->capture
[l
].len
= CAP_UNFINISHED
; /* undo capture */
429 static const char *match_capture (MatchState
*ms
, const char *s
, int l
) {
431 l
= check_capture(ms
, l
);
432 len
= ms
->capture
[l
].len
;
433 if ((size_t)(ms
->src_end
-s
) >= len
&&
434 memcmp(ms
->capture
[l
].init
, s
, len
) == 0)
440 static const char *match (MatchState
*ms
, const char *s
, const char *p
) {
441 if (ms
->matchdepth
-- == 0)
442 luaL_error(ms
->L
, "pattern too complex");
443 init
: /* using goto's to optimize tail recursion */
444 if (p
!= ms
->p_end
) { /* end of pattern? */
446 case '(': { /* start capture */
447 if (*(p
+ 1) == ')') /* position capture? */
448 s
= start_capture(ms
, s
, p
+ 2, CAP_POSITION
);
450 s
= start_capture(ms
, s
, p
+ 1, CAP_UNFINISHED
);
453 case ')': { /* end capture */
454 s
= end_capture(ms
, s
, p
+ 1);
458 if ((p
+ 1) != ms
->p_end
) /* is the `$' the last char in pattern? */
459 goto dflt
; /* no; go to default */
460 s
= (s
== ms
->src_end
) ? s
: NULL
; /* check end of string */
463 case L_ESC
: { /* escaped sequences not in the format class[*+?-]? */
465 case 'b': { /* balanced string? */
466 s
= matchbalance(ms
, s
, p
+ 2);
468 p
+= 4; goto init
; /* return match(ms, s, p + 4); */
469 } /* else fail (s == NULL) */
472 case 'f': { /* frontier? */
473 const char *ep
; char previous
;
476 luaL_error(ms
->L
, "missing " LUA_QL("[") " after "
477 LUA_QL("%%f") " in pattern");
478 ep
= classend(ms
, p
); /* points to what is next */
479 previous
= (s
== ms
->src_init
) ? '\0' : *(s
- 1);
480 if (!matchbracketclass(uchar(previous
), p
, ep
- 1) &&
481 matchbracketclass(uchar(*s
), p
, ep
- 1)) {
482 p
= ep
; goto init
; /* return match(ms, s, ep); */
484 s
= NULL
; /* match failed */
487 case '0': case '1': case '2': case '3':
488 case '4': case '5': case '6': case '7':
489 case '8': case '9': { /* capture results (%0-%9)? */
490 s
= match_capture(ms
, s
, uchar(*(p
+ 1)));
492 p
+= 2; goto init
; /* return match(ms, s, p + 2) */
500 default: dflt
: { /* pattern class plus optional suffix */
501 const char *ep
= classend(ms
, p
); /* points to optional suffix */
502 /* does not match at least once? */
503 if (!singlematch(ms
, s
, p
, ep
)) {
504 if (*ep
== '*' || *ep
== '?' || *ep
== '-') { /* accept empty? */
505 p
= ep
+ 1; goto init
; /* return match(ms, s, ep + 1); */
507 else /* '+' or no suffix */
510 else { /* matched once */
511 switch (*ep
) { /* handle optional suffix */
512 case '?': { /* optional */
514 if ((res
= match(ms
, s
+ 1, ep
+ 1)) != NULL
)
517 p
= ep
+ 1; goto init
; /* else return match(ms, s, ep + 1); */
521 case '+': /* 1 or more repetitions */
522 s
++; /* 1 match already done */
524 case '*': /* 0 or more repetitions */
525 s
= max_expand(ms
, s
, p
, ep
);
527 case '-': /* 0 or more repetitions (minimum) */
528 s
= min_expand(ms
, s
, p
, ep
);
530 default: /* no suffix */
531 s
++; p
= ep
; goto init
; /* return match(ms, s + 1, ep); */
544 static const char *lmemfind (const char *s1
, size_t l1
,
545 const char *s2
, size_t l2
) {
546 if (l2
== 0) return s1
; /* empty strings are everywhere */
547 else if (l2
> l1
) return NULL
; /* avoids a negative `l1' */
549 const char *init
; /* to search for a `*s2' inside `s1' */
550 l2
--; /* 1st char will be checked by `memchr' */
551 l1
= l1
-l2
; /* `s2' cannot be found after that */
552 while (l1
> 0 && (init
= (const char *)memchr(s1
, *s2
, l1
)) != NULL
) {
553 init
++; /* 1st char is already checked */
554 if (memcmp(init
, s2
+1, l2
) == 0)
556 else { /* correct `l1' and `s1' to try again */
561 return NULL
; /* not found */
566 static void push_onecapture (MatchState
*ms
, int i
, const char *s
,
568 if (i
>= ms
->level
) {
569 if (i
== 0) /* ms->level == 0, too */
570 lua_pushlstring(ms
->L
, s
, e
- s
); /* add whole match */
572 luaL_error(ms
->L
, "invalid capture index");
575 ptrdiff_t l
= ms
->capture
[i
].len
;
576 if (l
== CAP_UNFINISHED
) luaL_error(ms
->L
, "unfinished capture");
577 if (l
== CAP_POSITION
)
578 lua_pushinteger(ms
->L
, ms
->capture
[i
].init
- ms
->src_init
+ 1);
580 lua_pushlstring(ms
->L
, ms
->capture
[i
].init
, l
);
585 static int push_captures (MatchState
*ms
, const char *s
, const char *e
) {
587 int nlevels
= (ms
->level
== 0 && s
) ? 1 : ms
->level
;
588 luaL_checkstack(ms
->L
, nlevels
, "too many captures");
589 for (i
= 0; i
< nlevels
; i
++)
590 push_onecapture(ms
, i
, s
, e
);
591 return nlevels
; /* number of strings pushed */
595 /* check whether pattern has no special characters */
596 static int nospecials (const char *p
, size_t l
) {
599 if (strpbrk(p
+ upto
, SPECIALS
))
600 return 0; /* pattern has a special character */
601 upto
+= strlen(p
+ upto
) + 1; /* may have more after \0 */
603 return 1; /* no special chars found */
607 static int str_find_aux (lua_State
*L
, int find
) {
609 const char *s
= luaL_checklstring(L
, 1, &ls
);
610 const char *p
= luaL_checklstring(L
, 2, &lp
);
611 size_t init
= posrelat(luaL_optinteger(L
, 3, 1), ls
);
612 if (init
< 1) init
= 1;
613 else if (init
> ls
+ 1) { /* start after string's end? */
614 lua_pushnil(L
); /* cannot find anything */
617 /* explicit request or no special characters? */
618 if (find
&& (lua_toboolean(L
, 4) || nospecials(p
, lp
))) {
619 /* do a plain search */
620 const char *s2
= lmemfind(s
+ init
- 1, ls
- init
+ 1, p
, lp
);
622 lua_pushinteger(L
, s2
- s
+ 1);
623 lua_pushinteger(L
, s2
- s
+ lp
);
629 const char *s1
= s
+ init
- 1;
630 int anchor
= (*p
== '^');
632 p
++; lp
--; /* skip anchor character */
635 ms
.matchdepth
= MAXCCALLS
;
642 lua_assert(ms
.matchdepth
== MAXCCALLS
);
643 if ((res
=match(&ms
, s1
, p
)) != NULL
) {
645 lua_pushinteger(L
, s1
- s
+ 1); /* start */
646 lua_pushinteger(L
, res
- s
); /* end */
647 return push_captures(&ms
, NULL
, 0) + 2;
650 return push_captures(&ms
, s1
, res
);
652 } while (s1
++ < ms
.src_end
&& !anchor
);
654 lua_pushnil(L
); /* not found */
659 static int str_find (lua_State
*L
) {
660 return str_find_aux(L
, 1);
664 static int str_match (lua_State
*L
) {
665 return str_find_aux(L
, 0);
669 static int gmatch_aux (lua_State
*L
) {
672 const char *s
= lua_tolstring(L
, lua_upvalueindex(1), &ls
);
673 const char *p
= lua_tolstring(L
, lua_upvalueindex(2), &lp
);
676 ms
.matchdepth
= MAXCCALLS
;
680 for (src
= s
+ (size_t)lua_tointeger(L
, lua_upvalueindex(3));
685 lua_assert(ms
.matchdepth
== MAXCCALLS
);
686 if ((e
= match(&ms
, src
, p
)) != NULL
) {
687 lua_Integer newstart
= e
-s
;
688 if (e
== src
) newstart
++; /* empty match? go at least one position */
689 lua_pushinteger(L
, newstart
);
690 lua_replace(L
, lua_upvalueindex(3));
691 return push_captures(&ms
, src
, e
);
694 return 0; /* not found */
698 static int str_gmatch (lua_State
*L
) {
699 luaL_checkstring(L
, 1);
700 luaL_checkstring(L
, 2);
702 lua_pushinteger(L
, 0);
703 lua_pushcclosure(L
, gmatch_aux
, 3);
708 static void add_s (MatchState
*ms
, luaL_Buffer
*b
, const char *s
,
711 const char *news
= lua_tolstring(ms
->L
, 3, &l
);
712 for (i
= 0; i
< l
; i
++) {
713 if (news
[i
] != L_ESC
)
714 luaL_addchar(b
, news
[i
]);
717 if (!isdigit(uchar(news
[i
]))) {
718 if (news
[i
] != L_ESC
)
719 luaL_error(ms
->L
, "invalid use of " LUA_QL("%c")
720 " in replacement string", L_ESC
);
721 luaL_addchar(b
, news
[i
]);
723 else if (news
[i
] == '0')
724 luaL_addlstring(b
, s
, e
- s
);
726 push_onecapture(ms
, news
[i
] - '1', s
, e
);
727 luaL_addvalue(b
); /* add capture to accumulated result */
734 static void add_value (MatchState
*ms
, luaL_Buffer
*b
, const char *s
,
735 const char *e
, int tr
) {
736 lua_State
*L
= ms
->L
;
738 case LUA_TFUNCTION
: {
741 n
= push_captures(ms
, s
, e
);
746 push_onecapture(ms
, 0, s
, e
);
750 default: { /* LUA_TNUMBER or LUA_TSTRING */
755 if (!lua_toboolean(L
, -1)) { /* nil or false? */
757 lua_pushlstring(L
, s
, e
- s
); /* keep original text */
759 else if (!lua_isstring(L
, -1))
760 luaL_error(L
, "invalid replacement value (a %s)", luaL_typename(L
, -1));
761 luaL_addvalue(b
); /* add result to accumulator */
765 static int str_gsub (lua_State
*L
) {
767 const char *src
= luaL_checklstring(L
, 1, &srcl
);
768 const char *p
= luaL_checklstring(L
, 2, &lp
);
769 int tr
= lua_type(L
, 3);
770 size_t max_s
= luaL_optinteger(L
, 4, srcl
+1);
771 int anchor
= (*p
== '^');
775 luaL_argcheck(L
, tr
== LUA_TNUMBER
|| tr
== LUA_TSTRING
||
776 tr
== LUA_TFUNCTION
|| tr
== LUA_TTABLE
, 3,
777 "string/function/table expected");
778 luaL_buffinit(L
, &b
);
780 p
++; lp
--; /* skip anchor character */
783 ms
.matchdepth
= MAXCCALLS
;
785 ms
.src_end
= src
+srcl
;
790 lua_assert(ms
.matchdepth
== MAXCCALLS
);
791 e
= match(&ms
, src
, p
);
794 add_value(&ms
, &b
, src
, e
, tr
);
796 if (e
&& e
>src
) /* non empty match? */
797 src
= e
; /* skip it */
798 else if (src
< ms
.src_end
)
799 luaL_addchar(&b
, *src
++);
803 luaL_addlstring(&b
, src
, ms
.src_end
-src
);
805 lua_pushinteger(L
, n
); /* number of substitutions */
809 /* }====================================================== */
814 ** {======================================================
816 ** =======================================================
820 ** LUA_INTFRMLEN is the length modifier for integer conversions in
821 ** 'string.format'; LUA_INTFRM_T is the integer type corresponding to
822 ** the previous length
824 #if !defined(LUA_INTFRMLEN) /* { */
825 #if defined(LUA_USE_LONGLONG)
827 #define LUA_INTFRMLEN "ll"
828 #define LUA_INTFRM_T long long
832 #define LUA_INTFRMLEN "l"
833 #define LUA_INTFRM_T long
840 ** LUA_FLTFRMLEN is the length modifier for float conversions in
841 ** 'string.format'; LUA_FLTFRM_T is the float type corresponding to
842 ** the previous length
844 #if !defined(LUA_FLTFRMLEN)
846 #define LUA_FLTFRMLEN ""
847 #define LUA_FLTFRM_T double
852 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
854 /* valid flags in a format specification */
855 #define FLAGS "-+ #0"
857 ** maximum size of each format specification (such as '%-099.99d')
858 ** (+10 accounts for %99.99x plus margin of error)
860 #define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
863 static void addquoted (lua_State
*L
, luaL_Buffer
*b
, int arg
) {
865 const char *s
= luaL_checklstring(L
, arg
, &l
);
866 luaL_addchar(b
, '"');
868 if (*s
== '"' || *s
== '\\' || *s
== '\n') {
869 luaL_addchar(b
, '\\');
872 else if (*s
== '\0' || iscntrl(uchar(*s
))) {
874 if (!isdigit(uchar(*(s
+1))))
875 sprintf(buff
, "\\%d", (int)uchar(*s
));
877 sprintf(buff
, "\\%03d", (int)uchar(*s
));
878 luaL_addstring(b
, buff
);
884 luaL_addchar(b
, '"');
887 static const char *scanformat (lua_State
*L
, const char *strfrmt
, char *form
) {
888 const char *p
= strfrmt
;
889 while (*p
!= '\0' && strchr(FLAGS
, *p
) != NULL
) p
++; /* skip flags */
890 if ((size_t)(p
- strfrmt
) >= sizeof(FLAGS
)/sizeof(char))
891 luaL_error(L
, "invalid format (repeated flags)");
892 if (isdigit(uchar(*p
))) p
++; /* skip width */
893 if (isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
896 if (isdigit(uchar(*p
))) p
++; /* skip precision */
897 if (isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
899 if (isdigit(uchar(*p
)))
900 luaL_error(L
, "invalid format (width or precision too long)");
902 memcpy(form
, strfrmt
, (p
- strfrmt
+ 1) * sizeof(char));
903 form
+= p
- strfrmt
+ 1;
910 ** add length modifier into formats
912 static void addlenmod (char *form
, const char *lenmod
) {
913 size_t l
= strlen(form
);
914 size_t lm
= strlen(lenmod
);
915 char spec
= form
[l
- 1];
916 strcpy(form
+ l
- 1, lenmod
);
917 form
[l
+ lm
- 1] = spec
;
922 static int str_format (lua_State
*L
) {
923 int top
= lua_gettop(L
);
926 const char *strfrmt
= luaL_checklstring(L
, arg
, &sfl
);
927 const char *strfrmt_end
= strfrmt
+sfl
;
929 luaL_buffinit(L
, &b
);
930 while (strfrmt
< strfrmt_end
) {
931 if (*strfrmt
!= L_ESC
)
932 luaL_addchar(&b
, *strfrmt
++);
933 else if (*++strfrmt
== L_ESC
)
934 luaL_addchar(&b
, *strfrmt
++); /* %% */
935 else { /* format item */
936 char form
[MAX_FORMAT
]; /* to store the format (`%...') */
937 char *buff
= luaL_prepbuffsize(&b
, MAX_ITEM
); /* to put formatted item */
938 int nb
= 0; /* number of bytes in added item */
940 luaL_argerror(L
, arg
, "no value");
941 strfrmt
= scanformat(L
, strfrmt
, form
);
942 switch (*strfrmt
++) {
944 nb
= str_sprintf(buff
, form
, luaL_checkint(L
, arg
));
947 case 'd': case 'i': {
948 lua_Number n
= luaL_checknumber(L
, arg
);
949 LUA_INTFRM_T ni
= (LUA_INTFRM_T
)n
;
950 lua_Number diff
= n
- (lua_Number
)ni
;
951 luaL_argcheck(L
, -1 < diff
&& diff
< 1, arg
,
952 "not a number in proper range");
953 addlenmod(form
, LUA_INTFRMLEN
);
954 nb
= str_sprintf(buff
, form
, ni
);
957 case 'o': case 'u': case 'x': case 'X': {
958 lua_Number n
= luaL_checknumber(L
, arg
);
959 unsigned LUA_INTFRM_T ni
= (unsigned LUA_INTFRM_T
)n
;
960 lua_Number diff
= n
- (lua_Number
)ni
;
961 luaL_argcheck(L
, -1 < diff
&& diff
< 1, arg
,
962 "not a non-negative number in proper range");
963 addlenmod(form
, LUA_INTFRMLEN
);
964 nb
= str_sprintf(buff
, form
, ni
);
967 #if defined(LUA_USE_FLOAT_FORMATS)
968 case 'e': case 'E': case 'f':
969 #if defined(LUA_USE_AFORMAT)
972 case 'g': case 'G': {
973 addlenmod(form
, LUA_FLTFRMLEN
);
974 nb
= str_sprintf(buff
, form
, (LUA_FLTFRM_T
)luaL_checknumber(L
, arg
));
979 addquoted(L
, &b
, arg
);
984 const char *s
= luaL_tolstring(L
, arg
, &l
);
985 if (!strchr(form
, '.') && l
>= 100) {
986 /* no precision and string is too long to be formatted;
987 keep original string */
992 nb
= str_sprintf(buff
, form
, s
);
993 lua_pop(L
, 1); /* remove result from 'luaL_tolstring' */
997 default: { /* also treat cases `pnLlh' */
998 return luaL_error(L
, "invalid option " LUA_QL("%%%c") " to "
999 LUA_QL("format"), *(strfrmt
- 1));
1002 luaL_addsize(&b
, nb
);
1005 luaL_pushresult(&b
);
1009 /* }====================================================== */
1012 static const luaL_Reg strlib
[] = {
1017 {"format", str_format
},
1018 {"gmatch", str_gmatch
},
1021 {"lower", str_lower
},
1022 {"match", str_match
},
1024 {"reverse", str_reverse
},
1026 {"upper", str_upper
},
1031 static void createmetatable (lua_State
*L
) {
1032 lua_createtable(L
, 0, 1); /* table to be metatable for strings */
1033 lua_pushliteral(L
, ""); /* dummy string */
1034 lua_pushvalue(L
, -2); /* copy table */
1035 lua_setmetatable(L
, -2); /* set table as metatable for strings */
1036 lua_pop(L
, 1); /* pop dummy string */
1037 lua_pushvalue(L
, -2); /* get string library */
1038 lua_setfield(L
, -2, "__index"); /* metatable.__index = string */
1039 lua_pop(L
, 1); /* pop metatable */
1044 ** Open string library
1046 LUAMOD_API
int luaopen_string (lua_State
*L
) {
1047 luaL_newlib(L
, strlib
);