]>
git.proxmox.com Git - ceph.git/blob - ceph/src/lua/src/lstrlib.c
2 ** $Id: lstrlib.c,v 1.239 2015/11/25 16:28:17 roberto Exp $
3 ** Standard library for string operations and pattern-matching
4 ** See Copyright Notice in lua.h
28 ** maximum number of captures that a pattern can do during
29 ** pattern-matching. This limit is arbitrary.
31 #if !defined(LUA_MAXCAPTURES)
32 #define LUA_MAXCAPTURES 32
36 /* macro to 'unsign' a character */
37 #define uchar(c) ((unsigned char)(c))
41 ** Some sizes are better limited to fit in 'int', but must also fit in
42 ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
44 #define MAX_SIZET ((size_t)(~(size_t)0))
47 (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
52 static int str_len (lua_State
*L
) {
54 luaL_checklstring(L
, 1, &l
);
55 lua_pushinteger(L
, (lua_Integer
)l
);
60 /* translate a relative string position: negative means back from end */
61 static lua_Integer
posrelat (lua_Integer pos
, size_t len
) {
62 if (pos
>= 0) return pos
;
63 else if (0u - (size_t)pos
> len
) return 0;
64 else return (lua_Integer
)len
+ pos
+ 1;
68 static int str_sub (lua_State
*L
) {
70 const char *s
= luaL_checklstring(L
, 1, &l
);
71 lua_Integer start
= posrelat(luaL_checkinteger(L
, 2), l
);
72 lua_Integer end
= posrelat(luaL_optinteger(L
, 3, -1), l
);
73 if (start
< 1) start
= 1;
74 if (end
> (lua_Integer
)l
) end
= l
;
76 lua_pushlstring(L
, s
+ start
- 1, (size_t)(end
- start
) + 1);
77 else lua_pushliteral(L
, "");
82 static int str_reverse (lua_State
*L
) {
85 const char *s
= luaL_checklstring(L
, 1, &l
);
86 char *p
= luaL_buffinitsize(L
, &b
, l
);
87 for (i
= 0; i
< l
; i
++)
89 luaL_pushresultsize(&b
, l
);
94 static int str_lower (lua_State
*L
) {
98 const char *s
= luaL_checklstring(L
, 1, &l
);
99 char *p
= luaL_buffinitsize(L
, &b
, l
);
101 p
[i
] = tolower(uchar(s
[i
]));
102 luaL_pushresultsize(&b
, l
);
107 static int str_upper (lua_State
*L
) {
111 const char *s
= luaL_checklstring(L
, 1, &l
);
112 char *p
= luaL_buffinitsize(L
, &b
, l
);
114 p
[i
] = toupper(uchar(s
[i
]));
115 luaL_pushresultsize(&b
, l
);
120 static int str_rep (lua_State
*L
) {
122 const char *s
= luaL_checklstring(L
, 1, &l
);
123 lua_Integer n
= luaL_checkinteger(L
, 2);
124 const char *sep
= luaL_optlstring(L
, 3, "", &lsep
);
125 if (n
<= 0) lua_pushliteral(L
, "");
126 else if (l
+ lsep
< l
|| l
+ lsep
> MAXSIZE
/ n
) /* may overflow? */
127 return luaL_error(L
, "resulting string too large");
129 size_t totallen
= (size_t)n
* l
+ (size_t)(n
- 1) * lsep
;
131 char *p
= luaL_buffinitsize(L
, &b
, totallen
);
132 while (n
-- > 1) { /* first n-1 copies (followed by separator) */
133 memcpy(p
, s
, l
* sizeof(char)); p
+= l
;
134 if (lsep
> 0) { /* empty 'memcpy' is not that cheap */
135 memcpy(p
, sep
, lsep
* sizeof(char));
139 memcpy(p
, s
, l
* sizeof(char)); /* last copy (not followed by separator) */
140 luaL_pushresultsize(&b
, totallen
);
146 static int str_byte (lua_State
*L
) {
148 const char *s
= luaL_checklstring(L
, 1, &l
);
149 lua_Integer posi
= posrelat(luaL_optinteger(L
, 2, 1), l
);
150 lua_Integer pose
= posrelat(luaL_optinteger(L
, 3, posi
), l
);
152 if (posi
< 1) posi
= 1;
153 if (pose
> (lua_Integer
)l
) pose
= l
;
154 if (posi
> pose
) return 0; /* empty interval; return no values */
155 if (pose
- posi
>= INT_MAX
) /* arithmetic overflow? */
156 return luaL_error(L
, "string slice too long");
157 n
= (int)(pose
- posi
) + 1;
158 luaL_checkstack(L
, n
, "string slice too long");
160 lua_pushinteger(L
, uchar(s
[posi
+i
-1]));
165 static int str_char (lua_State
*L
) {
166 int n
= lua_gettop(L
); /* number of arguments */
169 char *p
= luaL_buffinitsize(L
, &b
, n
);
170 for (i
=1; i
<=n
; i
++) {
171 lua_Integer c
= luaL_checkinteger(L
, i
);
172 luaL_argcheck(L
, uchar(c
) == c
, i
, "value out of range");
175 luaL_pushresultsize(&b
, n
);
180 static int writer (lua_State
*L
, const void *b
, size_t size
, void *B
) {
182 luaL_addlstring((luaL_Buffer
*) B
, (const char *)b
, size
);
187 static int str_dump (lua_State
*L
) {
189 int strip
= lua_toboolean(L
, 2);
190 luaL_checktype(L
, 1, LUA_TFUNCTION
);
193 if (lua_dump(L
, writer
, &b
, strip
) != 0)
194 return luaL_error(L
, "unable to dump given function");
202 ** {======================================================
204 ** =======================================================
208 #define CAP_UNFINISHED (-1)
209 #define CAP_POSITION (-2)
212 typedef struct MatchState
{
213 const char *src_init
; /* init of source string */
214 const char *src_end
; /* end ('\0') of source string */
215 const char *p_end
; /* end ('\0') of pattern */
217 size_t nrep
; /* limit to avoid non-linear complexity */
218 int matchdepth
; /* control for recursive depth (to avoid C stack overflow) */
219 int level
; /* total number of captures (finished or unfinished) */
223 } capture
[LUA_MAXCAPTURES
];
227 /* recursive function */
228 static const char *match (MatchState
*ms
, const char *s
, const char *p
);
231 /* maximum recursion depth for 'match' */
232 #if !defined(MAXCCALLS)
233 #define MAXCCALLS 200
238 ** parameters to control the maximum number of operators handled in
239 ** a match (to avoid non-linear complexity). The maximum will be:
240 ** (subject length) * A_REPS + B_REPS
244 #define B_REPS 100000
249 #define SPECIALS "^$*+?.([%-"
252 static int check_capture (MatchState
*ms
, int l
) {
254 if (l
< 0 || l
>= ms
->level
|| ms
->capture
[l
].len
== CAP_UNFINISHED
)
255 return luaL_error(ms
->L
, "invalid capture index %%%d", l
+ 1);
260 static int capture_to_close (MatchState
*ms
) {
261 int level
= ms
->level
;
262 for (level
--; level
>=0; level
--)
263 if (ms
->capture
[level
].len
== CAP_UNFINISHED
) return level
;
264 return luaL_error(ms
->L
, "invalid pattern capture");
268 static const char *classend (MatchState
*ms
, const char *p
) {
272 luaL_error(ms
->L
, "malformed pattern (ends with '%%')");
277 do { /* look for a ']' */
279 luaL_error(ms
->L
, "malformed pattern (missing ']')");
280 if (*(p
++) == L_ESC
&& p
< ms
->p_end
)
281 p
++; /* skip escapes (e.g. '%]') */
292 static int match_class (int c
, int cl
) {
294 switch (tolower(cl
)) {
295 case 'a' : res
= isalpha(c
); break;
296 case 'c' : res
= iscntrl(c
); break;
297 case 'd' : res
= isdigit(c
); break;
298 case 'g' : res
= isgraph(c
); break;
299 case 'l' : res
= islower(c
); break;
300 case 'p' : res
= ispunct(c
); break;
301 case 's' : res
= isspace(c
); break;
302 case 'u' : res
= isupper(c
); break;
303 case 'w' : res
= isalnum(c
); break;
304 case 'x' : res
= isxdigit(c
); break;
305 case 'z' : res
= (c
== 0); break; /* deprecated option */
306 default: return (cl
== c
);
308 return (islower(cl
) ? res
: !res
);
312 static int matchbracketclass (int c
, const char *p
, const char *ec
) {
316 p
++; /* skip the '^' */
321 if (match_class(c
, uchar(*p
)))
324 else if ((*(p
+1) == '-') && (p
+2 < ec
)) {
326 if (uchar(*(p
-2)) <= c
&& c
<= uchar(*p
))
329 else if (uchar(*p
) == c
) return sig
;
335 static int singlematch (MatchState
*ms
, const char *s
, const char *p
,
337 if (s
>= ms
->src_end
)
342 case '.': return 1; /* matches any char */
343 case L_ESC
: return match_class(c
, uchar(*(p
+1)));
344 case '[': return matchbracketclass(c
, p
, ep
-1);
345 default: return (uchar(*p
) == c
);
351 static const char *matchbalance (MatchState
*ms
, const char *s
,
353 if (p
>= ms
->p_end
- 1)
354 luaL_error(ms
->L
, "malformed pattern (missing arguments to '%%b')");
355 if (*s
!= *p
) return NULL
;
360 while (++s
< ms
->src_end
) {
362 if (--cont
== 0) return s
+1;
364 else if (*s
== b
) cont
++;
367 return NULL
; /* string ends out of balance */
371 static const char *max_expand (MatchState
*ms
, const char *s
,
372 const char *p
, const char *ep
) {
373 ptrdiff_t i
= 0; /* counts maximum expand for item */
374 while (singlematch(ms
, s
+ i
, p
, ep
))
376 /* keeps trying to match with the maximum repetitions */
378 const char *res
= match(ms
, (s
+i
), ep
+1);
380 i
--; /* else didn't match; reduce 1 repetition to try again */
386 static const char *min_expand (MatchState
*ms
, const char *s
,
387 const char *p
, const char *ep
) {
389 const char *res
= match(ms
, s
, ep
+1);
392 else if (singlematch(ms
, s
, p
, ep
))
393 s
++; /* try with one more repetition */
399 static const char *start_capture (MatchState
*ms
, const char *s
,
400 const char *p
, int what
) {
402 int level
= ms
->level
;
403 if (level
>= LUA_MAXCAPTURES
) luaL_error(ms
->L
, "too many captures");
404 ms
->capture
[level
].init
= s
;
405 ms
->capture
[level
].len
= what
;
407 if ((res
=match(ms
, s
, p
)) == NULL
) /* match failed? */
408 ms
->level
--; /* undo capture */
413 static const char *end_capture (MatchState
*ms
, const char *s
,
415 int l
= capture_to_close(ms
);
417 ms
->capture
[l
].len
= s
- ms
->capture
[l
].init
; /* close capture */
418 if ((res
= match(ms
, s
, p
)) == NULL
) /* match failed? */
419 ms
->capture
[l
].len
= CAP_UNFINISHED
; /* undo capture */
424 static const char *match_capture (MatchState
*ms
, const char *s
, int l
) {
426 l
= check_capture(ms
, l
);
427 len
= ms
->capture
[l
].len
;
428 if ((size_t)(ms
->src_end
-s
) >= len
&&
429 memcmp(ms
->capture
[l
].init
, s
, len
) == 0)
435 static const char *match (MatchState
*ms
, const char *s
, const char *p
) {
436 if (ms
->matchdepth
-- == 0)
437 luaL_error(ms
->L
, "pattern too complex");
438 init
: /* using goto's to optimize tail recursion */
439 if (p
!= ms
->p_end
) { /* end of pattern? */
441 case '(': { /* start capture */
442 if (*(p
+ 1) == ')') /* position capture? */
443 s
= start_capture(ms
, s
, p
+ 2, CAP_POSITION
);
445 s
= start_capture(ms
, s
, p
+ 1, CAP_UNFINISHED
);
448 case ')': { /* end capture */
449 s
= end_capture(ms
, s
, p
+ 1);
453 if ((p
+ 1) != ms
->p_end
) /* is the '$' the last char in pattern? */
454 goto dflt
; /* no; go to default */
455 s
= (s
== ms
->src_end
) ? s
: NULL
; /* check end of string */
458 case L_ESC
: { /* escaped sequences not in the format class[*+?-]? */
460 case 'b': { /* balanced string? */
461 s
= matchbalance(ms
, s
, p
+ 2);
463 p
+= 4; goto init
; /* return match(ms, s, p + 4); */
464 } /* else fail (s == NULL) */
467 case 'f': { /* frontier? */
468 const char *ep
; char previous
;
471 luaL_error(ms
->L
, "missing '[' after '%%f' in pattern");
472 ep
= classend(ms
, p
); /* points to what is next */
473 previous
= (s
== ms
->src_init
) ? '\0' : *(s
- 1);
474 if (!matchbracketclass(uchar(previous
), p
, ep
- 1) &&
475 matchbracketclass(uchar(*s
), p
, ep
- 1)) {
476 p
= ep
; goto init
; /* return match(ms, s, ep); */
478 s
= NULL
; /* match failed */
481 case '0': case '1': case '2': case '3':
482 case '4': case '5': case '6': case '7':
483 case '8': case '9': { /* capture results (%0-%9)? */
484 s
= match_capture(ms
, s
, uchar(*(p
+ 1)));
486 p
+= 2; goto init
; /* return match(ms, s, p + 2) */
494 default: dflt
: { /* pattern class plus optional suffix */
495 const char *ep
= classend(ms
, p
); /* points to optional suffix */
496 /* does not match at least once? */
497 if (!singlematch(ms
, s
, p
, ep
)) {
498 if (*ep
== '*' || *ep
== '?' || *ep
== '-') { /* accept empty? */
499 p
= ep
+ 1; goto init
; /* return match(ms, s, ep + 1); */
501 else /* '+' or no suffix */
504 else { /* matched once */
506 luaL_error(ms
->L
, "pattern too complex");
507 switch (*ep
) { /* handle optional suffix */
508 case '?': { /* optional */
510 if ((res
= match(ms
, s
+ 1, ep
+ 1)) != NULL
)
513 p
= ep
+ 1; goto init
; /* else return match(ms, s, ep + 1); */
517 case '+': /* 1 or more repetitions */
518 s
++; /* 1 match already done */
520 case '*': /* 0 or more repetitions */
521 s
= max_expand(ms
, s
, p
, ep
);
523 case '-': /* 0 or more repetitions (minimum) */
524 s
= min_expand(ms
, s
, p
, ep
);
526 default: /* no suffix */
527 s
++; p
= ep
; goto init
; /* return match(ms, s + 1, ep); */
540 static const char *lmemfind (const char *s1
, size_t l1
,
541 const char *s2
, size_t l2
) {
542 if (l2
== 0) return s1
; /* empty strings are everywhere */
543 else if (l2
> l1
) return NULL
; /* avoids a negative 'l1' */
545 const char *init
; /* to search for a '*s2' inside 's1' */
546 l2
--; /* 1st char will be checked by 'memchr' */
547 l1
= l1
-l2
; /* 's2' cannot be found after that */
548 while (l1
> 0 && (init
= (const char *)memchr(s1
, *s2
, l1
)) != NULL
) {
549 init
++; /* 1st char is already checked */
550 if (memcmp(init
, s2
+1, l2
) == 0)
552 else { /* correct 'l1' and 's1' to try again */
557 return NULL
; /* not found */
562 static void push_onecapture (MatchState
*ms
, int i
, const char *s
,
564 if (i
>= ms
->level
) {
565 if (i
== 0) /* ms->level == 0, too */
566 lua_pushlstring(ms
->L
, s
, e
- s
); /* add whole match */
568 luaL_error(ms
->L
, "invalid capture index %%%d", i
+ 1);
571 ptrdiff_t l
= ms
->capture
[i
].len
;
572 if (l
== CAP_UNFINISHED
) luaL_error(ms
->L
, "unfinished capture");
573 if (l
== CAP_POSITION
)
574 lua_pushinteger(ms
->L
, (ms
->capture
[i
].init
- ms
->src_init
) + 1);
576 lua_pushlstring(ms
->L
, ms
->capture
[i
].init
, l
);
581 static int push_captures (MatchState
*ms
, const char *s
, const char *e
) {
583 int nlevels
= (ms
->level
== 0 && s
) ? 1 : ms
->level
;
584 luaL_checkstack(ms
->L
, nlevels
, "too many captures");
585 for (i
= 0; i
< nlevels
; i
++)
586 push_onecapture(ms
, i
, s
, e
);
587 return nlevels
; /* number of strings pushed */
591 /* check whether pattern has no special characters */
592 static int nospecials (const char *p
, size_t l
) {
595 if (strpbrk(p
+ upto
, SPECIALS
))
596 return 0; /* pattern has a special character */
597 upto
+= strlen(p
+ upto
) + 1; /* may have more after \0 */
599 return 1; /* no special chars found */
603 static void prepstate (MatchState
*ms
, lua_State
*L
,
604 const char *s
, size_t ls
, const char *p
, size_t lp
) {
606 ms
->matchdepth
= MAXCCALLS
;
608 ms
->src_end
= s
+ ls
;
610 if (ls
< (MAX_SIZET
- B_REPS
) / A_REPS
)
611 ms
->nrep
= A_REPS
* ls
+ B_REPS
;
612 else /* overflow (very long subject) */
613 ms
->nrep
= MAX_SIZET
; /* no limit */
617 static void reprepstate (MatchState
*ms
) {
619 lua_assert(ms
->matchdepth
== MAXCCALLS
);
623 static int str_find_aux (lua_State
*L
, int find
) {
625 const char *s
= luaL_checklstring(L
, 1, &ls
);
626 const char *p
= luaL_checklstring(L
, 2, &lp
);
627 lua_Integer init
= posrelat(luaL_optinteger(L
, 3, 1), ls
);
628 if (init
< 1) init
= 1;
629 else if (init
> (lua_Integer
)ls
+ 1) { /* start after string's end? */
630 lua_pushnil(L
); /* cannot find anything */
633 /* explicit request or no special characters? */
634 if (find
&& (lua_toboolean(L
, 4) || nospecials(p
, lp
))) {
635 /* do a plain search */
636 const char *s2
= lmemfind(s
+ init
- 1, ls
- (size_t)init
+ 1, p
, lp
);
638 lua_pushinteger(L
, (s2
- s
) + 1);
639 lua_pushinteger(L
, (s2
- s
) + lp
);
645 const char *s1
= s
+ init
- 1;
646 int anchor
= (*p
== '^');
648 p
++; lp
--; /* skip anchor character */
650 prepstate(&ms
, L
, s
, ls
, p
, lp
);
654 if ((res
=match(&ms
, s1
, p
)) != NULL
) {
656 lua_pushinteger(L
, (s1
- s
) + 1); /* start */
657 lua_pushinteger(L
, res
- s
); /* end */
658 return push_captures(&ms
, NULL
, 0) + 2;
661 return push_captures(&ms
, s1
, res
);
663 } while (s1
++ < ms
.src_end
&& !anchor
);
665 lua_pushnil(L
); /* not found */
670 static int str_find (lua_State
*L
) {
671 return str_find_aux(L
, 1);
675 static int str_match (lua_State
*L
) {
676 return str_find_aux(L
, 0);
680 /* state for 'gmatch' */
681 typedef struct GMatchState
{
682 const char *src
; /* current position */
683 const char *p
; /* pattern */
684 MatchState ms
; /* match state */
688 static int gmatch_aux (lua_State
*L
) {
689 GMatchState
*gm
= (GMatchState
*)lua_touserdata(L
, lua_upvalueindex(3));
691 for (src
= gm
->src
; src
<= gm
->ms
.src_end
; src
++) {
693 reprepstate(&gm
->ms
);
694 if ((e
= match(&gm
->ms
, src
, gm
->p
)) != NULL
) {
695 if (e
== src
) /* empty match? */
696 gm
->src
=src
+ 1; /* go at least one position */
699 return push_captures(&gm
->ms
, src
, e
);
702 return 0; /* not found */
706 static int gmatch (lua_State
*L
) {
708 const char *s
= luaL_checklstring(L
, 1, &ls
);
709 const char *p
= luaL_checklstring(L
, 2, &lp
);
711 lua_settop(L
, 2); /* keep them on closure to avoid being collected */
712 gm
= (GMatchState
*)lua_newuserdata(L
, sizeof(GMatchState
));
713 prepstate(&gm
->ms
, L
, s
, ls
, p
, lp
);
714 gm
->src
= s
; gm
->p
= p
;
715 lua_pushcclosure(L
, gmatch_aux
, 3);
720 static void add_s (MatchState
*ms
, luaL_Buffer
*b
, const char *s
,
723 lua_State
*L
= ms
->L
;
724 const char *news
= lua_tolstring(L
, 3, &l
);
725 for (i
= 0; i
< l
; i
++) {
726 if (news
[i
] != L_ESC
)
727 luaL_addchar(b
, news
[i
]);
730 if (!isdigit(uchar(news
[i
]))) {
731 if (news
[i
] != L_ESC
)
732 luaL_error(L
, "invalid use of '%c' in replacement string", L_ESC
);
733 luaL_addchar(b
, news
[i
]);
735 else if (news
[i
] == '0')
736 luaL_addlstring(b
, s
, e
- s
);
738 push_onecapture(ms
, news
[i
] - '1', s
, e
);
739 luaL_tolstring(L
, -1, NULL
); /* if number, convert it to string */
740 lua_remove(L
, -2); /* remove original value */
741 luaL_addvalue(b
); /* add capture to accumulated result */
748 static void add_value (MatchState
*ms
, luaL_Buffer
*b
, const char *s
,
749 const char *e
, int tr
) {
750 lua_State
*L
= ms
->L
;
752 case LUA_TFUNCTION
: {
755 n
= push_captures(ms
, s
, e
);
760 push_onecapture(ms
, 0, s
, e
);
764 default: { /* LUA_TNUMBER or LUA_TSTRING */
769 if (!lua_toboolean(L
, -1)) { /* nil or false? */
771 lua_pushlstring(L
, s
, e
- s
); /* keep original text */
773 else if (!lua_isstring(L
, -1))
774 luaL_error(L
, "invalid replacement value (a %s)", luaL_typename(L
, -1));
775 luaL_addvalue(b
); /* add result to accumulator */
779 static int str_gsub (lua_State
*L
) {
781 const char *src
= luaL_checklstring(L
, 1, &srcl
);
782 const char *p
= luaL_checklstring(L
, 2, &lp
);
783 int tr
= lua_type(L
, 3);
784 lua_Integer max_s
= luaL_optinteger(L
, 4, srcl
+ 1);
785 int anchor
= (*p
== '^');
789 luaL_argcheck(L
, tr
== LUA_TNUMBER
|| tr
== LUA_TSTRING
||
790 tr
== LUA_TFUNCTION
|| tr
== LUA_TTABLE
, 3,
791 "string/function/table expected");
792 luaL_buffinit(L
, &b
);
794 p
++; lp
--; /* skip anchor character */
796 prepstate(&ms
, L
, src
, srcl
, p
, lp
);
800 if ((e
= match(&ms
, src
, p
)) != NULL
) {
802 add_value(&ms
, &b
, src
, e
, tr
);
804 if (e
&& e
>src
) /* non empty match? */
805 src
= e
; /* skip it */
806 else if (src
< ms
.src_end
)
807 luaL_addchar(&b
, *src
++);
811 luaL_addlstring(&b
, src
, ms
.src_end
-src
);
813 lua_pushinteger(L
, n
); /* number of substitutions */
817 /* }====================================================== */
822 ** {======================================================
824 ** =======================================================
827 #if !defined(lua_number2strx) /* { */
830 ** Hexadecimal floating-point formatter
836 #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
840 ** Number of bits that goes into the first digit. It can be any value
841 ** between 1 and 4; the following definition tries to align the number
842 ** to nibble boundaries by making what is left after that first digit a
845 #define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1)
849 ** Add integer part of 'x' to buffer and return new 'x'
851 static lua_Number
adddigit (char *buff
, int n
, lua_Number x
) {
852 lua_Number dd
= l_mathop(floor
)(x
); /* get integer part from 'x' */
854 buff
[n
] = (d
< 10 ? d
+ '0' : d
- 10 + 'a'); /* add to buffer */
855 return x
- dd
; /* return what is left */
859 static int num2straux (char *buff
, int sz
, lua_Number x
) {
860 if (x
!= x
|| x
== HUGE_VAL
|| x
== -HUGE_VAL
) /* inf or NaN? */
861 return l_sprintf(buff
, sz
, LUA_NUMBER_FMT
, x
); /* equal to '%g' */
862 else if (x
== 0) { /* can be -0... */
863 /* create "0" or "-0" followed by exponent */
864 return l_sprintf(buff
, sz
, LUA_NUMBER_FMT
"x0p+0", x
);
868 lua_Number m
= l_mathop(frexp
)(x
, &e
); /* 'x' fraction and exponent */
869 int n
= 0; /* character count */
870 if (m
< 0) { /* is number negative? */
871 buff
[n
++] = '-'; /* add signal */
872 m
= -m
; /* make it positive */
874 buff
[n
++] = '0'; buff
[n
++] = 'x'; /* add "0x" */
875 m
= adddigit(buff
, n
++, m
* (1 << L_NBFD
)); /* add first digit */
876 e
-= L_NBFD
; /* this digit goes before the radix point */
877 if (m
> 0) { /* more digits? */
878 buff
[n
++] = lua_getlocaledecpoint(); /* add radix point */
879 do { /* add as many digits as needed */
880 m
= adddigit(buff
, n
++, m
* 16);
883 n
+= l_sprintf(buff
+ n
, sz
- n
, "p%+d", e
); /* add exponent */
890 static int lua_number2strx (lua_State
*L
, char *buff
, int sz
,
891 const char *fmt
, lua_Number x
) {
892 int n
= num2straux(buff
, sz
, x
);
893 if (fmt
[SIZELENMOD
] == 'A') {
895 for (i
= 0; i
< n
; i
++)
896 buff
[i
] = toupper(uchar(buff
[i
]));
898 else if (fmt
[SIZELENMOD
] != 'a')
899 luaL_error(L
, "modifiers for format '%%a'/'%%A' not implemented");
907 ** Maximum size of each formatted item. This maximum size is produced
908 ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.',
909 ** and '\0') + number of decimal digits to represent maxfloat (which
910 ** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra
911 ** expenses", such as locale-dependent stuff)
913 #define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
916 /* valid flags in a format specification */
917 #define FLAGS "-+ #0"
920 ** maximum size of each format specification (such as "%-099.99d")
922 #define MAX_FORMAT 32
925 static void addquoted (lua_State
*L
, luaL_Buffer
*b
, int arg
) {
927 const char *s
= luaL_checklstring(L
, arg
, &l
);
928 luaL_addchar(b
, '"');
930 if (*s
== '"' || *s
== '\\' || *s
== '\n') {
931 luaL_addchar(b
, '\\');
934 else if (*s
== '\0' || iscntrl(uchar(*s
))) {
936 if (!isdigit(uchar(*(s
+1))))
937 l_sprintf(buff
, sizeof(buff
), "\\%d", (int)uchar(*s
));
939 l_sprintf(buff
, sizeof(buff
), "\\%03d", (int)uchar(*s
));
940 luaL_addstring(b
, buff
);
946 luaL_addchar(b
, '"');
949 static const char *scanformat (lua_State
*L
, const char *strfrmt
, char *form
) {
950 const char *p
= strfrmt
;
951 while (*p
!= '\0' && strchr(FLAGS
, *p
) != NULL
) p
++; /* skip flags */
952 if ((size_t)(p
- strfrmt
) >= sizeof(FLAGS
)/sizeof(char))
953 luaL_error(L
, "invalid format (repeated flags)");
954 if (isdigit(uchar(*p
))) p
++; /* skip width */
955 if (isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
958 if (isdigit(uchar(*p
))) p
++; /* skip precision */
959 if (isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
961 if (isdigit(uchar(*p
)))
962 luaL_error(L
, "invalid format (width or precision too long)");
964 memcpy(form
, strfrmt
, ((p
- strfrmt
) + 1) * sizeof(char));
965 form
+= (p
- strfrmt
) + 1;
972 ** add length modifier into formats
974 static void addlenmod (char *form
, const char *lenmod
) {
975 size_t l
= strlen(form
);
976 size_t lm
= strlen(lenmod
);
977 char spec
= form
[l
- 1];
978 strcpy(form
+ l
- 1, lenmod
);
979 form
[l
+ lm
- 1] = spec
;
984 static int str_format (lua_State
*L
) {
985 int top
= lua_gettop(L
);
988 const char *strfrmt
= luaL_checklstring(L
, arg
, &sfl
);
989 const char *strfrmt_end
= strfrmt
+sfl
;
991 luaL_buffinit(L
, &b
);
992 while (strfrmt
< strfrmt_end
) {
993 if (*strfrmt
!= L_ESC
)
994 luaL_addchar(&b
, *strfrmt
++);
995 else if (*++strfrmt
== L_ESC
)
996 luaL_addchar(&b
, *strfrmt
++); /* %% */
997 else { /* format item */
998 char form
[MAX_FORMAT
]; /* to store the format ('%...') */
999 char *buff
= luaL_prepbuffsize(&b
, MAX_ITEM
); /* to put formatted item */
1000 int nb
= 0; /* number of bytes in added item */
1002 luaL_argerror(L
, arg
, "no value");
1003 strfrmt
= scanformat(L
, strfrmt
, form
);
1004 switch (*strfrmt
++) {
1006 nb
= l_sprintf(buff
, MAX_ITEM
, form
, (int)luaL_checkinteger(L
, arg
));
1010 case 'o': case 'u': case 'x': case 'X': {
1011 lua_Integer n
= luaL_checkinteger(L
, arg
);
1012 addlenmod(form
, LUA_INTEGER_FRMLEN
);
1013 nb
= l_sprintf(buff
, MAX_ITEM
, form
, n
);
1017 addlenmod(form
, LUA_NUMBER_FRMLEN
);
1018 nb
= lua_number2strx(L
, buff
, MAX_ITEM
, form
,
1019 luaL_checknumber(L
, arg
));
1021 case 'e': case 'E': case 'f':
1022 case 'g': case 'G': {
1023 addlenmod(form
, LUA_NUMBER_FRMLEN
);
1024 nb
= l_sprintf(buff
, MAX_ITEM
, form
, luaL_checknumber(L
, arg
));
1028 addquoted(L
, &b
, arg
);
1033 const char *s
= luaL_tolstring(L
, arg
, &l
);
1034 if (form
[2] == '\0') /* no modifiers? */
1035 luaL_addvalue(&b
); /* keep entire string */
1037 luaL_argcheck(L
, l
== strlen(s
), arg
, "string contains zeros");
1038 if (!strchr(form
, '.') && l
>= 100) {
1039 /* no precision and string is too long to be formatted */
1040 luaL_addvalue(&b
); /* keep entire string */
1042 else { /* format the string into 'buff' */
1043 nb
= l_sprintf(buff
, MAX_ITEM
, form
, s
);
1044 lua_pop(L
, 1); /* remove result from 'luaL_tolstring' */
1049 default: { /* also treat cases 'pnLlh' */
1050 return luaL_error(L
, "invalid option '%%%c' to 'format'",
1054 lua_assert(nb
< MAX_ITEM
);
1055 luaL_addsize(&b
, nb
);
1058 luaL_pushresult(&b
);
1062 /* }====================================================== */
1066 ** {======================================================
1068 ** =======================================================
1072 /* value used for padding */
1073 #if !defined(LUA_PACKPADBYTE)
1074 #define LUA_PACKPADBYTE 0x00
1077 /* maximum size for the binary representation of an integer */
1078 #define MAXINTSIZE 16
1080 /* number of bits in a character */
1083 /* mask for one character (NB 1's) */
1084 #define MC ((1 << NB) - 1)
1086 /* size of a lua_Integer */
1087 #define SZINT ((int)sizeof(lua_Integer))
1090 /* dummy union to get native endianness */
1091 static const union {
1093 char little
; /* true iff machine is little endian */
1094 } nativeendian
= {1};
1097 /* dummy structure to get native alignment requirements */
1100 union { double d
; void *p
; lua_Integer i
; lua_Number n
; } u
;
1103 #define MAXALIGN (offsetof(struct cD, u))
1107 ** Union for serializing floats
1109 typedef union Ftypes
{
1113 char buff
[5 * sizeof(lua_Number
)]; /* enough for any float type */
1118 ** information to pack/unpack stuff
1120 typedef struct Header
{
1128 ** options for pack/unpack
1130 typedef enum KOption
{
1131 Kint
, /* signed integers */
1132 Kuint
, /* unsigned integers */
1133 Kfloat
, /* floating-point numbers */
1134 Kchar
, /* fixed-length strings */
1135 Kstring
, /* strings with prefixed length */
1136 Kzstr
, /* zero-terminated strings */
1137 Kpadding
, /* padding */
1138 Kpaddalign
, /* padding for alignment */
1139 Knop
/* no-op (configuration or spaces) */
1144 ** Read an integer numeral from string 'fmt' or return 'df' if
1145 ** there is no numeral
1147 static int digit (int c
) { return '0' <= c
&& c
<= '9'; }
1149 static int getnum (const char **fmt
, int df
) {
1150 if (!digit(**fmt
)) /* no number? */
1151 return df
; /* return default value */
1155 a
= a
*10 + (*((*fmt
)++) - '0');
1156 } while (digit(**fmt
) && a
<= ((int)MAXSIZE
- 9)/10);
1163 ** Read an integer numeral and raises an error if it is larger
1164 ** than the maximum size for integers.
1166 static int getnumlimit (Header
*h
, const char **fmt
, int df
) {
1167 int sz
= getnum(fmt
, df
);
1168 if (sz
> MAXINTSIZE
|| sz
<= 0)
1169 luaL_error(h
->L
, "integral size (%d) out of limits [1,%d]",
1176 ** Initialize Header
1178 static void initheader (lua_State
*L
, Header
*h
) {
1180 h
->islittle
= nativeendian
.little
;
1186 ** Read and classify next option. 'size' is filled with option's size.
1188 static KOption
getoption (Header
*h
, const char **fmt
, int *size
) {
1189 int opt
= *((*fmt
)++);
1190 *size
= 0; /* default */
1192 case 'b': *size
= sizeof(char); return Kint
;
1193 case 'B': *size
= sizeof(char); return Kuint
;
1194 case 'h': *size
= sizeof(short); return Kint
;
1195 case 'H': *size
= sizeof(short); return Kuint
;
1196 case 'l': *size
= sizeof(long); return Kint
;
1197 case 'L': *size
= sizeof(long); return Kuint
;
1198 case 'j': *size
= sizeof(lua_Integer
); return Kint
;
1199 case 'J': *size
= sizeof(lua_Integer
); return Kuint
;
1200 case 'T': *size
= sizeof(size_t); return Kuint
;
1201 case 'f': *size
= sizeof(float); return Kfloat
;
1202 case 'd': *size
= sizeof(double); return Kfloat
;
1203 case 'n': *size
= sizeof(lua_Number
); return Kfloat
;
1204 case 'i': *size
= getnumlimit(h
, fmt
, sizeof(int)); return Kint
;
1205 case 'I': *size
= getnumlimit(h
, fmt
, sizeof(int)); return Kuint
;
1206 case 's': *size
= getnumlimit(h
, fmt
, sizeof(size_t)); return Kstring
;
1208 *size
= getnum(fmt
, -1);
1210 luaL_error(h
->L
, "missing size for format option 'c'");
1212 case 'z': return Kzstr
;
1213 case 'x': *size
= 1; return Kpadding
;
1214 case 'X': return Kpaddalign
;
1216 case '<': h
->islittle
= 1; break;
1217 case '>': h
->islittle
= 0; break;
1218 case '=': h
->islittle
= nativeendian
.little
; break;
1219 case '!': h
->maxalign
= getnumlimit(h
, fmt
, MAXALIGN
); break;
1220 default: luaL_error(h
->L
, "invalid format option '%c'", opt
);
1227 ** Read, classify, and fill other details about the next option.
1228 ** 'psize' is filled with option's size, 'notoalign' with its
1229 ** alignment requirements.
1230 ** Local variable 'size' gets the size to be aligned. (Kpadal option
1231 ** always gets its full alignment, other options are limited by
1232 ** the maximum alignment ('maxalign'). Kchar option needs no alignment
1233 ** despite its size.
1235 static KOption
getdetails (Header
*h
, size_t totalsize
,
1236 const char **fmt
, int *psize
, int *ntoalign
) {
1237 KOption opt
= getoption(h
, fmt
, psize
);
1238 int align
= *psize
; /* usually, alignment follows size */
1239 if (opt
== Kpaddalign
) { /* 'X' gets alignment from following option */
1240 if (**fmt
== '\0' || getoption(h
, fmt
, &align
) == Kchar
|| align
== 0)
1241 luaL_argerror(h
->L
, 1, "invalid next option for option 'X'");
1243 if (align
<= 1 || opt
== Kchar
) /* need no alignment? */
1246 if (align
> h
->maxalign
) /* enforce maximum alignment */
1247 align
= h
->maxalign
;
1248 if ((align
& (align
- 1)) != 0) /* is 'align' not a power of 2? */
1249 luaL_argerror(h
->L
, 1, "format asks for alignment not power of 2");
1250 *ntoalign
= (align
- (int)(totalsize
& (align
- 1))) & (align
- 1);
1257 ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
1258 ** The final 'if' handles the case when 'size' is larger than
1259 ** the size of a Lua integer, correcting the extra sign-extension
1260 ** bytes if necessary (by default they would be zeros).
1262 static void packint (luaL_Buffer
*b
, lua_Unsigned n
,
1263 int islittle
, int size
, int neg
) {
1264 char *buff
= luaL_prepbuffsize(b
, size
);
1266 buff
[islittle
? 0 : size
- 1] = (char)(n
& MC
); /* first byte */
1267 for (i
= 1; i
< size
; i
++) {
1269 buff
[islittle
? i
: size
- 1 - i
] = (char)(n
& MC
);
1271 if (neg
&& size
> SZINT
) { /* negative number need sign extension? */
1272 for (i
= SZINT
; i
< size
; i
++) /* correct extra bytes */
1273 buff
[islittle
? i
: size
- 1 - i
] = (char)MC
;
1275 luaL_addsize(b
, size
); /* add result to buffer */
1280 ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
1281 ** given 'islittle' is different from native endianness.
1283 static void copywithendian (volatile char *dest
, volatile const char *src
,
1284 int size
, int islittle
) {
1285 if (islittle
== nativeendian
.little
) {
1287 *(dest
++) = *(src
++);
1292 *(dest
--) = *(src
++);
1297 static int str_pack (lua_State
*L
) {
1300 const char *fmt
= luaL_checkstring(L
, 1); /* format string */
1301 int arg
= 1; /* current argument to pack */
1302 size_t totalsize
= 0; /* accumulate total size of result */
1304 lua_pushnil(L
); /* mark to separate arguments from string buffer */
1305 luaL_buffinit(L
, &b
);
1306 while (*fmt
!= '\0') {
1308 KOption opt
= getdetails(&h
, totalsize
, &fmt
, &size
, &ntoalign
);
1309 totalsize
+= ntoalign
+ size
;
1310 while (ntoalign
-- > 0)
1311 luaL_addchar(&b
, LUA_PACKPADBYTE
); /* fill alignment */
1314 case Kint
: { /* signed integers */
1315 lua_Integer n
= luaL_checkinteger(L
, arg
);
1316 if (size
< SZINT
) { /* need overflow check? */
1317 lua_Integer lim
= (lua_Integer
)1 << ((size
* NB
) - 1);
1318 luaL_argcheck(L
, -lim
<= n
&& n
< lim
, arg
, "integer overflow");
1320 packint(&b
, (lua_Unsigned
)n
, h
.islittle
, size
, (n
< 0));
1323 case Kuint
: { /* unsigned integers */
1324 lua_Integer n
= luaL_checkinteger(L
, arg
);
1325 if (size
< SZINT
) /* need overflow check? */
1326 luaL_argcheck(L
, (lua_Unsigned
)n
< ((lua_Unsigned
)1 << (size
* NB
)),
1327 arg
, "unsigned overflow");
1328 packint(&b
, (lua_Unsigned
)n
, h
.islittle
, size
, 0);
1331 case Kfloat
: { /* floating-point options */
1333 char *buff
= luaL_prepbuffsize(&b
, size
);
1334 lua_Number n
= luaL_checknumber(L
, arg
); /* get argument */
1335 if (size
== sizeof(u
.f
)) u
.f
= (float)n
; /* copy it into 'u' */
1336 else if (size
== sizeof(u
.d
)) u
.d
= (double)n
;
1338 /* move 'u' to final result, correcting endianness if needed */
1339 copywithendian(buff
, u
.buff
, size
, h
.islittle
);
1340 luaL_addsize(&b
, size
);
1343 case Kchar
: { /* fixed-size string */
1345 const char *s
= luaL_checklstring(L
, arg
, &len
);
1346 if ((size_t)size
<= len
) /* string larger than (or equal to) needed? */
1347 luaL_addlstring(&b
, s
, size
); /* truncate string to asked size */
1348 else { /* string smaller than needed */
1349 luaL_addlstring(&b
, s
, len
); /* add it all */
1350 while (len
++ < (size_t)size
) /* pad extra space */
1351 luaL_addchar(&b
, LUA_PACKPADBYTE
);
1355 case Kstring
: { /* strings with length count */
1357 const char *s
= luaL_checklstring(L
, arg
, &len
);
1358 luaL_argcheck(L
, size
>= (int)sizeof(size_t) ||
1359 len
< ((size_t)1 << (size
* NB
)),
1360 arg
, "string length does not fit in given size");
1361 packint(&b
, (lua_Unsigned
)len
, h
.islittle
, size
, 0); /* pack length */
1362 luaL_addlstring(&b
, s
, len
);
1366 case Kzstr
: { /* zero-terminated string */
1368 const char *s
= luaL_checklstring(L
, arg
, &len
);
1369 luaL_argcheck(L
, strlen(s
) == len
, arg
, "string contains zeros");
1370 luaL_addlstring(&b
, s
, len
);
1371 luaL_addchar(&b
, '\0'); /* add zero at the end */
1372 totalsize
+= len
+ 1;
1375 case Kpadding
: luaL_addchar(&b
, LUA_PACKPADBYTE
); /* FALLTHROUGH */
1376 case Kpaddalign
: case Knop
:
1377 arg
--; /* undo increment */
1381 luaL_pushresult(&b
);
1386 static int str_packsize (lua_State
*L
) {
1388 const char *fmt
= luaL_checkstring(L
, 1); /* format string */
1389 size_t totalsize
= 0; /* accumulate total size of result */
1391 while (*fmt
!= '\0') {
1393 KOption opt
= getdetails(&h
, totalsize
, &fmt
, &size
, &ntoalign
);
1394 size
+= ntoalign
; /* total space used by option */
1395 luaL_argcheck(L
, totalsize
<= MAXSIZE
- size
, 1,
1396 "format result too large");
1399 case Kstring
: /* strings with length count */
1400 case Kzstr
: /* zero-terminated string */
1401 luaL_argerror(L
, 1, "variable-length format");
1402 /* call never return, but to avoid warnings: *//* FALLTHROUGH */
1406 lua_pushinteger(L
, (lua_Integer
)totalsize
);
1412 ** Unpack an integer with 'size' bytes and 'islittle' endianness.
1413 ** If size is smaller than the size of a Lua integer and integer
1414 ** is signed, must do sign extension (propagating the sign to the
1415 ** higher bits); if size is larger than the size of a Lua integer,
1416 ** it must check the unread bytes to see whether they do not cause an
1419 static lua_Integer
unpackint (lua_State
*L
, const char *str
,
1420 int islittle
, int size
, int issigned
) {
1421 lua_Unsigned res
= 0;
1423 int limit
= (size
<= SZINT
) ? size
: SZINT
;
1424 for (i
= limit
- 1; i
>= 0; i
--) {
1426 res
|= (lua_Unsigned
)(unsigned char)str
[islittle
? i
: size
- 1 - i
];
1428 if (size
< SZINT
) { /* real size smaller than lua_Integer? */
1429 if (issigned
) { /* needs sign extension? */
1430 lua_Unsigned mask
= (lua_Unsigned
)1 << (size
*NB
- 1);
1431 res
= ((res
^ mask
) - mask
); /* do sign extension */
1434 else if (size
> SZINT
) { /* must check unread bytes */
1435 int mask
= (!issigned
|| (lua_Integer
)res
>= 0) ? 0 : MC
;
1436 for (i
= limit
; i
< size
; i
++) {
1437 if ((unsigned char)str
[islittle
? i
: size
- 1 - i
] != mask
)
1438 luaL_error(L
, "%d-byte integer does not fit into Lua Integer", size
);
1441 return (lua_Integer
)res
;
1445 static int str_unpack (lua_State
*L
) {
1447 const char *fmt
= luaL_checkstring(L
, 1);
1449 const char *data
= luaL_checklstring(L
, 2, &ld
);
1450 size_t pos
= (size_t)posrelat(luaL_optinteger(L
, 3, 1), ld
) - 1;
1451 int n
= 0; /* number of results */
1452 luaL_argcheck(L
, pos
<= ld
, 3, "initial position out of string");
1454 while (*fmt
!= '\0') {
1456 KOption opt
= getdetails(&h
, pos
, &fmt
, &size
, &ntoalign
);
1457 if ((size_t)ntoalign
+ size
> ~pos
|| pos
+ ntoalign
+ size
> ld
)
1458 luaL_argerror(L
, 2, "data string too short");
1459 pos
+= ntoalign
; /* skip alignment */
1460 /* stack space for item + next position */
1461 luaL_checkstack(L
, 2, "too many results");
1466 lua_Integer res
= unpackint(L
, data
+ pos
, h
.islittle
, size
,
1468 lua_pushinteger(L
, res
);
1474 copywithendian(u
.buff
, data
+ pos
, size
, h
.islittle
);
1475 if (size
== sizeof(u
.f
)) num
= (lua_Number
)u
.f
;
1476 else if (size
== sizeof(u
.d
)) num
= (lua_Number
)u
.d
;
1478 lua_pushnumber(L
, num
);
1482 lua_pushlstring(L
, data
+ pos
, size
);
1486 size_t len
= (size_t)unpackint(L
, data
+ pos
, h
.islittle
, size
, 0);
1487 luaL_argcheck(L
, pos
+ len
+ size
<= ld
, 2, "data string too short");
1488 lua_pushlstring(L
, data
+ pos
+ size
, len
);
1489 pos
+= len
; /* skip string */
1493 size_t len
= (int)strlen(data
+ pos
);
1494 lua_pushlstring(L
, data
+ pos
, len
);
1495 pos
+= len
+ 1; /* skip string plus final '\0' */
1498 case Kpaddalign
: case Kpadding
: case Knop
:
1499 n
--; /* undo increment */
1504 lua_pushinteger(L
, pos
+ 1); /* next position */
1508 /* }====================================================== */
1511 static const luaL_Reg strlib
[] = {
1516 {"format", str_format
},
1520 {"lower", str_lower
},
1521 {"match", str_match
},
1523 {"reverse", str_reverse
},
1525 {"upper", str_upper
},
1527 {"packsize", str_packsize
},
1528 {"unpack", str_unpack
},
1533 static void createmetatable (lua_State
*L
) {
1534 lua_createtable(L
, 0, 1); /* table to be metatable for strings */
1535 lua_pushliteral(L
, ""); /* dummy string */
1536 lua_pushvalue(L
, -2); /* copy table */
1537 lua_setmetatable(L
, -2); /* set table as metatable for strings */
1538 lua_pop(L
, 1); /* pop dummy string */
1539 lua_pushvalue(L
, -2); /* get string library */
1540 lua_setfield(L
, -2, "__index"); /* metatable.__index = string */
1541 lua_pop(L
, 1); /* pop metatable */
1546 ** Open string library
1548 LUAMOD_API
int luaopen_string (lua_State
*L
) {
1549 luaL_newlib(L
, strlib
);