1 /* $NetBSD: lstrlib.c,v 1.9 2015/10/08 13:40:16 mbalmer Exp $ */
4 ** Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 roberto Exp
5 ** Standard library for string operations and pattern-matching
6 ** See Copyright Notice in lua.h
32 ** maximum number of captures that a pattern can do during
33 ** pattern-matching. This limit is arbitrary.
35 #if !defined(LUA_MAXCAPTURES)
36 #define LUA_MAXCAPTURES 32
40 /* macro to 'unsign' a character */
41 #define uchar(c) ((unsigned char)(c))
45 ** Some sizes are better limited to fit in 'int', but must also fit in
46 ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
49 (sizeof(size_t) < sizeof(int) ? (~(size_t)0) : (size_t)(INT_MAX))
54 static int str_len (lua_State
*L
) {
56 luaL_checklstring(L
, 1, &l
);
57 lua_pushinteger(L
, (lua_Integer
)l
);
62 /* translate a relative string position: negative means back from end */
63 static lua_Integer
posrelat (lua_Integer pos
, size_t len
) {
64 if (pos
>= 0) return pos
;
65 else if (0u - (size_t)pos
> len
) return 0;
66 else return (lua_Integer
)len
+ pos
+ 1;
70 static int str_sub (lua_State
*L
) {
72 const char *s
= luaL_checklstring(L
, 1, &l
);
73 lua_Integer start
= posrelat(luaL_checkinteger(L
, 2), l
);
74 lua_Integer end
= posrelat(luaL_optinteger(L
, 3, -1), l
);
75 if (start
< 1) start
= 1;
76 if (end
> (lua_Integer
)l
) end
= l
;
78 lua_pushlstring(L
, s
+ start
- 1, (size_t)(end
- start
) + 1);
79 else lua_pushliteral(L
, "");
84 static int str_reverse (lua_State
*L
) {
87 const char *s
= luaL_checklstring(L
, 1, &l
);
88 char *p
= luaL_buffinitsize(L
, &b
, l
);
89 for (i
= 0; i
< l
; i
++)
91 luaL_pushresultsize(&b
, l
);
96 static int str_lower (lua_State
*L
) {
100 const char *s
= luaL_checklstring(L
, 1, &l
);
101 char *p
= luaL_buffinitsize(L
, &b
, l
);
103 p
[i
] = tolower(uchar(s
[i
]));
104 luaL_pushresultsize(&b
, l
);
109 static int str_upper (lua_State
*L
) {
113 const char *s
= luaL_checklstring(L
, 1, &l
);
114 char *p
= luaL_buffinitsize(L
, &b
, l
);
116 p
[i
] = toupper(uchar(s
[i
]));
117 luaL_pushresultsize(&b
, l
);
122 static int str_rep (lua_State
*L
) {
124 const char *s
= luaL_checklstring(L
, 1, &l
);
125 lua_Integer n
= luaL_checkinteger(L
, 2);
126 const char *sep
= luaL_optlstring(L
, 3, "", &lsep
);
127 if (n
<= 0) lua_pushliteral(L
, "");
128 else if (l
+ lsep
< l
|| l
+ lsep
> MAXSIZE
/ n
) /* may overflow? */
129 return luaL_error(L
, "resulting string too large");
131 size_t totallen
= (size_t)n
* l
+ (size_t)(n
- 1) * lsep
;
133 char *p
= luaL_buffinitsize(L
, &b
, totallen
);
134 while (n
-- > 1) { /* first n-1 copies (followed by separator) */
135 memcpy(p
, s
, l
* sizeof(char)); p
+= l
;
136 if (lsep
> 0) { /* empty 'memcpy' is not that cheap */
137 memcpy(p
, sep
, lsep
* sizeof(char));
141 memcpy(p
, s
, l
* sizeof(char)); /* last copy (not followed by separator) */
142 luaL_pushresultsize(&b
, totallen
);
148 static int str_byte (lua_State
*L
) {
150 const char *s
= luaL_checklstring(L
, 1, &l
);
151 lua_Integer posi
= posrelat(luaL_optinteger(L
, 2, 1), l
);
152 lua_Integer pose
= posrelat(luaL_optinteger(L
, 3, posi
), l
);
154 if (posi
< 1) posi
= 1;
155 if (pose
> (lua_Integer
)l
) pose
= l
;
156 if (posi
> pose
) return 0; /* empty interval; return no values */
157 if (pose
- posi
>= INT_MAX
) /* arithmetic overflow? */
158 return luaL_error(L
, "string slice too long");
159 n
= (int)(pose
- posi
) + 1;
160 luaL_checkstack(L
, n
, "string slice too long");
162 lua_pushinteger(L
, uchar(s
[posi
+i
-1]));
167 static int str_char (lua_State
*L
) {
168 int n
= lua_gettop(L
); /* number of arguments */
171 char *p
= luaL_buffinitsize(L
, &b
, n
);
172 for (i
=1; i
<=n
; i
++) {
173 lua_Integer c
= luaL_checkinteger(L
, i
);
174 luaL_argcheck(L
, uchar(c
) == c
, i
, "value out of range");
177 luaL_pushresultsize(&b
, n
);
182 static int writer (lua_State
*L
, const void *b
, size_t size
, void *B
) {
184 luaL_addlstring((luaL_Buffer
*) B
, (const char *)b
, size
);
189 static int str_dump (lua_State
*L
) {
191 int strip
= lua_toboolean(L
, 2);
192 luaL_checktype(L
, 1, LUA_TFUNCTION
);
195 if (lua_dump(L
, writer
, &b
, strip
) != 0)
196 return luaL_error(L
, "unable to dump given function");
204 ** {======================================================
206 ** =======================================================
210 #define CAP_UNFINISHED (-1)
211 #define CAP_POSITION (-2)
214 typedef struct MatchState
{
215 int matchdepth
; /* control for recursive depth (to avoid C stack overflow) */
216 const char *src_init
; /* init of source string */
217 const char *src_end
; /* end ('\0') of source string */
218 const char *p_end
; /* end ('\0') of pattern */
220 int level
; /* total number of captures (finished or unfinished) */
224 } capture
[LUA_MAXCAPTURES
];
228 /* recursive function */
229 static const char *match (MatchState
*ms
, const char *s
, const char *p
);
232 /* maximum recursion depth for 'match' */
233 #if !defined(MAXCCALLS)
234 #define MAXCCALLS 200
239 #define SPECIALS "^$*+?.([%-"
242 static int check_capture (MatchState
*ms
, int l
) {
244 if (l
< 0 || l
>= ms
->level
|| ms
->capture
[l
].len
== CAP_UNFINISHED
)
245 return luaL_error(ms
->L
, "invalid capture index %%%d", l
+ 1);
250 static int capture_to_close (MatchState
*ms
) {
251 int level
= ms
->level
;
252 for (level
--; level
>=0; level
--)
253 if (ms
->capture
[level
].len
== CAP_UNFINISHED
) return level
;
254 return luaL_error(ms
->L
, "invalid pattern capture");
258 static const char *classend (MatchState
*ms
, const char *p
) {
262 luaL_error(ms
->L
, "malformed pattern (ends with '%%')");
267 do { /* look for a ']' */
269 luaL_error(ms
->L
, "malformed pattern (missing ']')");
270 if (*(p
++) == L_ESC
&& p
< ms
->p_end
)
271 p
++; /* skip escapes (e.g. '%]') */
282 static int match_class (int c
, int cl
) {
284 switch (tolower(cl
)) {
285 case 'a' : res
= isalpha(c
); break;
286 case 'c' : res
= iscntrl(c
); break;
287 case 'd' : res
= isdigit(c
); break;
288 case 'g' : res
= isgraph(c
); break;
289 case 'l' : res
= islower(c
); break;
290 case 'p' : res
= ispunct(c
); break;
291 case 's' : res
= isspace(c
); break;
292 case 'u' : res
= isupper(c
); break;
293 case 'w' : res
= isalnum(c
); break;
294 case 'x' : res
= isxdigit(c
); break;
295 case 'z' : res
= (c
== 0); break; /* deprecated option */
296 default: return (cl
== c
);
298 return (islower(cl
) ? res
: !res
);
302 static int matchbracketclass (int c
, const char *p
, const char *ec
) {
306 p
++; /* skip the '^' */
311 if (match_class(c
, uchar(*p
)))
314 else if ((*(p
+1) == '-') && (p
+2 < ec
)) {
316 if (uchar(*(p
-2)) <= c
&& c
<= uchar(*p
))
319 else if (uchar(*p
) == c
) return sig
;
325 static int singlematch (MatchState
*ms
, const char *s
, const char *p
,
327 if (s
>= ms
->src_end
)
332 case '.': return 1; /* matches any char */
333 case L_ESC
: return match_class(c
, uchar(*(p
+1)));
334 case '[': return matchbracketclass(c
, p
, ep
-1);
335 default: return (uchar(*p
) == c
);
341 static const char *matchbalance (MatchState
*ms
, const char *s
,
343 if (p
>= ms
->p_end
- 1)
344 luaL_error(ms
->L
, "malformed pattern (missing arguments to '%%b')");
345 if (*s
!= *p
) return NULL
;
350 while (++s
< ms
->src_end
) {
352 if (--cont
== 0) return s
+1;
354 else if (*s
== b
) cont
++;
357 return NULL
; /* string ends out of balance */
361 static const char *max_expand (MatchState
*ms
, const char *s
,
362 const char *p
, const char *ep
) {
363 ptrdiff_t i
= 0; /* counts maximum expand for item */
364 while (singlematch(ms
, s
+ i
, p
, ep
))
366 /* keeps trying to match with the maximum repetitions */
368 const char *res
= match(ms
, (s
+i
), ep
+1);
370 i
--; /* else didn't match; reduce 1 repetition to try again */
376 static const char *min_expand (MatchState
*ms
, const char *s
,
377 const char *p
, const char *ep
) {
379 const char *res
= match(ms
, s
, ep
+1);
382 else if (singlematch(ms
, s
, p
, ep
))
383 s
++; /* try with one more repetition */
389 static const char *start_capture (MatchState
*ms
, const char *s
,
390 const char *p
, int what
) {
392 int level
= ms
->level
;
393 if (level
>= LUA_MAXCAPTURES
) luaL_error(ms
->L
, "too many captures");
394 ms
->capture
[level
].init
= s
;
395 ms
->capture
[level
].len
= what
;
397 if ((res
=match(ms
, s
, p
)) == NULL
) /* match failed? */
398 ms
->level
--; /* undo capture */
403 static const char *end_capture (MatchState
*ms
, const char *s
,
405 int l
= capture_to_close(ms
);
407 ms
->capture
[l
].len
= s
- ms
->capture
[l
].init
; /* close capture */
408 if ((res
= match(ms
, s
, p
)) == NULL
) /* match failed? */
409 ms
->capture
[l
].len
= CAP_UNFINISHED
; /* undo capture */
414 static const char *match_capture (MatchState
*ms
, const char *s
, int l
) {
416 l
= check_capture(ms
, l
);
417 len
= ms
->capture
[l
].len
;
418 if ((size_t)(ms
->src_end
-s
) >= len
&&
419 memcmp(ms
->capture
[l
].init
, s
, len
) == 0)
425 static const char *match (MatchState
*ms
, const char *s
, const char *p
) {
426 if (ms
->matchdepth
-- == 0)
427 luaL_error(ms
->L
, "pattern too complex");
428 init
: /* using goto's to optimize tail recursion */
429 if (p
!= ms
->p_end
) { /* end of pattern? */
431 case '(': { /* start capture */
432 if (*(p
+ 1) == ')') /* position capture? */
433 s
= start_capture(ms
, s
, p
+ 2, CAP_POSITION
);
435 s
= start_capture(ms
, s
, p
+ 1, CAP_UNFINISHED
);
438 case ')': { /* end capture */
439 s
= end_capture(ms
, s
, p
+ 1);
443 if ((p
+ 1) != ms
->p_end
) /* is the '$' the last char in pattern? */
444 goto dflt
; /* no; go to default */
445 s
= (s
== ms
->src_end
) ? s
: NULL
; /* check end of string */
448 case L_ESC
: { /* escaped sequences not in the format class[*+?-]? */
450 case 'b': { /* balanced string? */
451 s
= matchbalance(ms
, s
, p
+ 2);
453 p
+= 4; goto init
; /* return match(ms, s, p + 4); */
454 } /* else fail (s == NULL) */
457 case 'f': { /* frontier? */
458 const char *ep
; char previous
;
461 luaL_error(ms
->L
, "missing '[' after '%%f' in pattern");
462 ep
= classend(ms
, p
); /* points to what is next */
463 previous
= (s
== ms
->src_init
) ? '\0' : *(s
- 1);
464 if (!matchbracketclass(uchar(previous
), p
, ep
- 1) &&
465 matchbracketclass(uchar(*s
), p
, ep
- 1)) {
466 p
= ep
; goto init
; /* return match(ms, s, ep); */
468 s
= NULL
; /* match failed */
471 case '0': case '1': case '2': case '3':
472 case '4': case '5': case '6': case '7':
473 case '8': case '9': { /* capture results (%0-%9)? */
474 s
= match_capture(ms
, s
, uchar(*(p
+ 1)));
476 p
+= 2; goto init
; /* return match(ms, s, p + 2) */
484 default: dflt
: { /* pattern class plus optional suffix */
485 const char *ep
= classend(ms
, p
); /* points to optional suffix */
486 /* does not match at least once? */
487 if (!singlematch(ms
, s
, p
, ep
)) {
488 if (*ep
== '*' || *ep
== '?' || *ep
== '-') { /* accept empty? */
489 p
= ep
+ 1; goto init
; /* return match(ms, s, ep + 1); */
491 else /* '+' or no suffix */
494 else { /* matched once */
495 switch (*ep
) { /* handle optional suffix */
496 case '?': { /* optional */
498 if ((res
= match(ms
, s
+ 1, ep
+ 1)) != NULL
)
501 p
= ep
+ 1; goto init
; /* else return match(ms, s, ep + 1); */
505 case '+': /* 1 or more repetitions */
506 s
++; /* 1 match already done */
508 case '*': /* 0 or more repetitions */
509 s
= max_expand(ms
, s
, p
, ep
);
511 case '-': /* 0 or more repetitions (minimum) */
512 s
= min_expand(ms
, s
, p
, ep
);
514 default: /* no suffix */
515 s
++; p
= ep
; goto init
; /* return match(ms, s + 1, ep); */
528 static const char *lmemfind (const char *s1
, size_t l1
,
529 const char *s2
, size_t l2
) {
530 if (l2
== 0) return s1
; /* empty strings are everywhere */
531 else if (l2
> l1
) return NULL
; /* avoids a negative 'l1' */
533 const char *init
; /* to search for a '*s2' inside 's1' */
534 l2
--; /* 1st char will be checked by 'memchr' */
535 l1
= l1
-l2
; /* 's2' cannot be found after that */
536 while (l1
> 0 && (init
= (const char *)memchr(s1
, *s2
, l1
)) != NULL
) {
537 init
++; /* 1st char is already checked */
538 if (memcmp(init
, s2
+1, l2
) == 0)
540 else { /* correct 'l1' and 's1' to try again */
545 return NULL
; /* not found */
550 static void push_onecapture (MatchState
*ms
, int i
, const char *s
,
552 if (i
>= ms
->level
) {
553 if (i
== 0) /* ms->level == 0, too */
554 lua_pushlstring(ms
->L
, s
, e
- s
); /* add whole match */
556 luaL_error(ms
->L
, "invalid capture index %%%d", i
+ 1);
559 ptrdiff_t l
= ms
->capture
[i
].len
;
560 if (l
== CAP_UNFINISHED
) luaL_error(ms
->L
, "unfinished capture");
561 if (l
== CAP_POSITION
)
562 lua_pushinteger(ms
->L
, (ms
->capture
[i
].init
- ms
->src_init
) + 1);
564 lua_pushlstring(ms
->L
, ms
->capture
[i
].init
, l
);
569 static int push_captures (MatchState
*ms
, const char *s
, const char *e
) {
571 int nlevels
= (ms
->level
== 0 && s
) ? 1 : ms
->level
;
572 luaL_checkstack(ms
->L
, nlevels
, "too many captures");
573 for (i
= 0; i
< nlevels
; i
++)
574 push_onecapture(ms
, i
, s
, e
);
575 return nlevels
; /* number of strings pushed */
579 /* check whether pattern has no special characters */
580 static int nospecials (const char *p
, size_t l
) {
583 if (strpbrk(p
+ upto
, SPECIALS
))
584 return 0; /* pattern has a special character */
585 upto
+= strlen(p
+ upto
) + 1; /* may have more after \0 */
587 return 1; /* no special chars found */
591 static int str_find_aux (lua_State
*L
, int find
) {
593 const char *s
= luaL_checklstring(L
, 1, &ls
);
594 const char *p
= luaL_checklstring(L
, 2, &lp
);
595 lua_Integer init
= posrelat(luaL_optinteger(L
, 3, 1), ls
);
596 if (init
< 1) init
= 1;
597 else if (init
> (lua_Integer
)ls
+ 1) { /* start after string's end? */
598 lua_pushnil(L
); /* cannot find anything */
601 /* explicit request or no special characters? */
602 if (find
&& (lua_toboolean(L
, 4) || nospecials(p
, lp
))) {
603 /* do a plain search */
604 const char *s2
= lmemfind(s
+ init
- 1, ls
- (size_t)init
+ 1, p
, lp
);
606 lua_pushinteger(L
, (s2
- s
) + 1);
607 lua_pushinteger(L
, (s2
- s
) + lp
);
613 const char *s1
= s
+ init
- 1;
614 int anchor
= (*p
== '^');
616 p
++; lp
--; /* skip anchor character */
619 ms
.matchdepth
= MAXCCALLS
;
626 lua_assert(ms
.matchdepth
== MAXCCALLS
);
627 if ((res
=match(&ms
, s1
, p
)) != NULL
) {
629 lua_pushinteger(L
, (s1
- s
) + 1); /* start */
630 lua_pushinteger(L
, res
- s
); /* end */
631 return push_captures(&ms
, NULL
, 0) + 2;
634 return push_captures(&ms
, s1
, res
);
636 } while (s1
++ < ms
.src_end
&& !anchor
);
638 lua_pushnil(L
); /* not found */
643 static int str_find (lua_State
*L
) {
644 return str_find_aux(L
, 1);
648 static int str_match (lua_State
*L
) {
649 return str_find_aux(L
, 0);
653 static int gmatch_aux (lua_State
*L
) {
656 const char *s
= lua_tolstring(L
, lua_upvalueindex(1), &ls
);
657 const char *p
= lua_tolstring(L
, lua_upvalueindex(2), &lp
);
660 ms
.matchdepth
= MAXCCALLS
;
664 for (src
= s
+ (size_t)lua_tointeger(L
, lua_upvalueindex(3));
669 lua_assert(ms
.matchdepth
== MAXCCALLS
);
670 if ((e
= match(&ms
, src
, p
)) != NULL
) {
671 lua_Integer newstart
= e
-s
;
672 if (e
== src
) newstart
++; /* empty match? go at least one position */
673 lua_pushinteger(L
, newstart
);
674 lua_replace(L
, lua_upvalueindex(3));
675 return push_captures(&ms
, src
, e
);
678 return 0; /* not found */
682 static int gmatch (lua_State
*L
) {
683 luaL_checkstring(L
, 1);
684 luaL_checkstring(L
, 2);
686 lua_pushinteger(L
, 0);
687 lua_pushcclosure(L
, gmatch_aux
, 3);
692 static void add_s (MatchState
*ms
, luaL_Buffer
*b
, const char *s
,
695 lua_State
*L
= ms
->L
;
696 const char *news
= lua_tolstring(L
, 3, &l
);
697 for (i
= 0; i
< l
; i
++) {
698 if (news
[i
] != L_ESC
)
699 luaL_addchar(b
, news
[i
]);
702 if (!isdigit(uchar(news
[i
]))) {
703 if (news
[i
] != L_ESC
)
704 luaL_error(L
, "invalid use of '%c' in replacement string", L_ESC
);
705 luaL_addchar(b
, news
[i
]);
707 else if (news
[i
] == '0')
708 luaL_addlstring(b
, s
, e
- s
);
710 push_onecapture(ms
, news
[i
] - '1', s
, e
);
711 luaL_tolstring(L
, -1, NULL
); /* if number, convert it to string */
712 lua_remove(L
, -2); /* remove original value */
713 luaL_addvalue(b
); /* add capture to accumulated result */
720 static void add_value (MatchState
*ms
, luaL_Buffer
*b
, const char *s
,
721 const char *e
, int tr
) {
722 lua_State
*L
= ms
->L
;
724 case LUA_TFUNCTION
: {
727 n
= push_captures(ms
, s
, e
);
732 push_onecapture(ms
, 0, s
, e
);
736 default: { /* LUA_TNUMBER or LUA_TSTRING */
741 if (!lua_toboolean(L
, -1)) { /* nil or false? */
743 lua_pushlstring(L
, s
, e
- s
); /* keep original text */
745 else if (!lua_isstring(L
, -1))
746 luaL_error(L
, "invalid replacement value (a %s)", luaL_typename(L
, -1));
747 luaL_addvalue(b
); /* add result to accumulator */
751 static int str_gsub (lua_State
*L
) {
753 const char *src
= luaL_checklstring(L
, 1, &srcl
);
754 const char *p
= luaL_checklstring(L
, 2, &lp
);
755 int tr
= lua_type(L
, 3);
756 lua_Integer max_s
= luaL_optinteger(L
, 4, srcl
+ 1);
757 int anchor
= (*p
== '^');
761 luaL_argcheck(L
, tr
== LUA_TNUMBER
|| tr
== LUA_TSTRING
||
762 tr
== LUA_TFUNCTION
|| tr
== LUA_TTABLE
, 3,
763 "string/function/table expected");
764 luaL_buffinit(L
, &b
);
766 p
++; lp
--; /* skip anchor character */
769 ms
.matchdepth
= MAXCCALLS
;
771 ms
.src_end
= src
+srcl
;
776 lua_assert(ms
.matchdepth
== MAXCCALLS
);
777 e
= match(&ms
, src
, p
);
780 add_value(&ms
, &b
, src
, e
, tr
);
782 if (e
&& e
>src
) /* non empty match? */
783 src
= e
; /* skip it */
784 else if (src
< ms
.src_end
)
785 luaL_addchar(&b
, *src
++);
789 luaL_addlstring(&b
, src
, ms
.src_end
-src
);
791 lua_pushinteger(L
, n
); /* number of substitutions */
795 /* }====================================================== */
800 ** {======================================================
802 ** =======================================================
805 #if !defined(lua_number2strx) /* { */
808 ** Hexadecimal floating-point formatter
814 #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
818 ** Number of bits that goes into the first digit. It can be any value
819 ** between 1 and 4; the following definition tries to align the number
820 ** to nibble boundaries by making what is left after that first digit a
823 #define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1)
827 ** Add integer part of 'x' to buffer and return new 'x'
829 static lua_Number
adddigit (char *buff
, int n
, lua_Number x
) {
830 lua_Number dd
= l_mathop(floor
)(x
); /* get integer part from 'x' */
832 buff
[n
] = (d
< 10 ? d
+ '0' : d
- 10 + 'a'); /* add to buffer */
833 return x
- dd
; /* return what is left */
837 static int num2straux (char *buff
, lua_Number x
) {
838 if (x
!= x
|| x
== HUGE_VAL
|| x
== -HUGE_VAL
) /* inf or NaN? */
839 return sprintf(buff
, LUA_NUMBER_FMT
, x
); /* equal to '%g' */
840 else if (x
== 0) { /* can be -0... */
841 sprintf(buff
, LUA_NUMBER_FMT
, x
);
842 strcat(buff
, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */
847 lua_Number m
= l_mathop(frexp
)(x
, &e
); /* 'x' fraction and exponent */
848 int n
= 0; /* character count */
849 if (m
< 0) { /* is number negative? */
850 buff
[n
++] = '-'; /* add signal */
851 m
= -m
; /* make it positive */
853 buff
[n
++] = '0'; buff
[n
++] = 'x'; /* add "0x" */
854 m
= adddigit(buff
, n
++, m
* (1 << L_NBFD
)); /* add first digit */
855 e
-= L_NBFD
; /* this digit goes before the radix point */
856 if (m
> 0) { /* more digits? */
857 buff
[n
++] = lua_getlocaledecpoint(); /* add radix point */
858 do { /* add as many digits as needed */
859 m
= adddigit(buff
, n
++, m
* 16);
862 n
+= sprintf(buff
+ n
, "p%+d", e
); /* add exponent */
868 static int lua_number2strx (lua_State
*L
, char *buff
, const char *fmt
,
870 int n
= num2straux(buff
, x
);
871 if (fmt
[SIZELENMOD
] == 'A') {
873 for (i
= 0; i
< n
; i
++)
874 buff
[i
] = toupper(uchar(buff
[i
]));
876 else if (fmt
[SIZELENMOD
] != 'a')
877 luaL_error(L
, "modifiers for format '%%a'/'%%A' not implemented");
885 ** Maximum size of each formatted item. This maximum size is produced
886 ** by format('%.99f', minfloat), and is equal to 99 + 2 ('-' and '.') +
887 ** number of decimal digits to represent minfloat.
890 #define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
892 #define MAX_ITEM (120)
895 /* valid flags in a format specification */
896 #define FLAGS "-+ #0"
899 ** maximum size of each format specification (such as "%-099.99d")
901 #define MAX_FORMAT 32
904 static void addquoted (lua_State
*L
, luaL_Buffer
*b
, int arg
) {
906 const char *s
= luaL_checklstring(L
, arg
, &l
);
907 luaL_addchar(b
, '"');
909 if (*s
== '"' || *s
== '\\' || *s
== '\n') {
910 luaL_addchar(b
, '\\');
913 else if (*s
== '\0' || iscntrl(uchar(*s
))) {
915 if (!isdigit(uchar(*(s
+1))))
916 sprintf(buff
, "\\%d", (int)uchar(*s
));
918 sprintf(buff
, "\\%03d", (int)uchar(*s
));
919 luaL_addstring(b
, buff
);
925 luaL_addchar(b
, '"');
928 static const char *scanformat (lua_State
*L
, const char *strfrmt
, char *form
) {
929 const char *p
= strfrmt
;
930 while (*p
!= '\0' && strchr(FLAGS
, *p
) != NULL
) p
++; /* skip flags */
931 if ((size_t)(p
- strfrmt
) >= sizeof(FLAGS
)/sizeof(char))
932 luaL_error(L
, "invalid format (repeated flags)");
933 if (isdigit(uchar(*p
))) p
++; /* skip width */
934 if (isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
937 if (isdigit(uchar(*p
))) p
++; /* skip precision */
938 if (isdigit(uchar(*p
))) p
++; /* (2 digits at most) */
940 if (isdigit(uchar(*p
)))
941 luaL_error(L
, "invalid format (width or precision too long)");
943 memcpy(form
, strfrmt
, ((p
- strfrmt
) + 1) * sizeof(char));
944 form
+= (p
- strfrmt
) + 1;
951 ** add length modifier into formats
953 static void addlenmod (char *form
, const char *lenmod
) {
954 size_t l
= strlen(form
);
955 size_t lm
= strlen(lenmod
);
956 char spec
= form
[l
- 1];
957 strcpy(form
+ l
- 1, lenmod
);
958 form
[l
+ lm
- 1] = spec
;
963 static int str_format (lua_State
*L
) {
964 int top
= lua_gettop(L
);
967 const char *strfrmt
= luaL_checklstring(L
, arg
, &sfl
);
968 const char *strfrmt_end
= strfrmt
+sfl
;
970 luaL_buffinit(L
, &b
);
971 while (strfrmt
< strfrmt_end
) {
972 if (*strfrmt
!= L_ESC
)
973 luaL_addchar(&b
, *strfrmt
++);
974 else if (*++strfrmt
== L_ESC
)
975 luaL_addchar(&b
, *strfrmt
++); /* %% */
976 else { /* format item */
977 char form
[MAX_FORMAT
]; /* to store the format ('%...') */
978 char *buff
= luaL_prepbuffsize(&b
, MAX_ITEM
); /* to put formatted item */
979 int nb
= 0; /* number of bytes in added item */
981 luaL_argerror(L
, arg
, "no value");
982 strfrmt
= scanformat(L
, strfrmt
, form
);
983 switch (*strfrmt
++) {
985 nb
= sprintf(buff
, form
, (int)luaL_checkinteger(L
, arg
));
989 case 'o': case 'u': case 'x': case 'X': {
990 lua_Integer n
= luaL_checkinteger(L
, arg
);
991 addlenmod(form
, LUA_INTEGER_FRMLEN
);
992 nb
= sprintf(buff
, form
, n
);
997 addlenmod(form
, LUA_NUMBER_FRMLEN
);
998 nb
= lua_number2strx(L
, buff
, form
, luaL_checknumber(L
, arg
));
1000 case 'e': case 'E': case 'f':
1001 case 'g': case 'G': {
1002 addlenmod(form
, LUA_NUMBER_FRMLEN
);
1003 nb
= sprintf(buff
, form
, luaL_checknumber(L
, arg
));
1008 addquoted(L
, &b
, arg
);
1013 const char *s
= luaL_tolstring(L
, arg
, &l
);
1014 if (!strchr(form
, '.') && l
>= 100) {
1015 /* no precision and string is too long to be formatted;
1016 keep original string */
1020 nb
= sprintf(buff
, form
, s
);
1021 lua_pop(L
, 1); /* remove result from 'luaL_tolstring' */
1025 default: { /* also treat cases 'pnLlh' */
1026 return luaL_error(L
, "invalid option '%%%c' to 'format'",
1030 luaL_addsize(&b
, nb
);
1033 luaL_pushresult(&b
);
1037 /* }====================================================== */
1041 ** {======================================================
1043 ** =======================================================
1047 /* value used for padding */
1048 #if !defined(LUA_PACKPADBYTE)
1049 #define LUA_PACKPADBYTE 0x00
1052 /* maximum size for the binary representation of an integer */
1053 #define MAXINTSIZE 16
1055 /* number of bits in a character */
1058 /* mask for one character (NB 1's) */
1059 #define MC ((1 << NB) - 1)
1061 /* size of a lua_Integer */
1062 #define SZINT ((int)sizeof(lua_Integer))
1065 /* dummy union to get native endianness */
1066 static const union {
1068 char little
; /* true iff machine is little endian */
1069 } nativeendian
= {1};
1072 /* dummy structure to get native alignment requirements */
1076 union { double d
; void *p
; lua_Integer i
; lua_Number n
; } u
;
1078 union { void *p
; lua_Integer i
; lua_Number n
; } u
;
1082 #define MAXALIGN (offsetof(struct cD, u))
1087 ** Union for serializing floats
1089 typedef union Ftypes
{
1093 char buff
[5 * sizeof(lua_Number
)]; /* enough for any float type */
1099 ** information to pack/unpack stuff
1101 typedef struct Header
{
1109 ** options for pack/unpack
1111 typedef enum KOption
{
1112 Kint
, /* signed integers */
1113 Kuint
, /* unsigned integers */
1115 Kfloat
, /* floating-point numbers */
1117 Kchar
, /* fixed-length strings */
1118 Kstring
, /* strings with prefixed length */
1119 Kzstr
, /* zero-terminated strings */
1120 Kpadding
, /* padding */
1121 Kpaddalign
, /* padding for alignment */
1122 Knop
/* no-op (configuration or spaces) */
1127 ** Read an integer numeral from string 'fmt' or return 'df' if
1128 ** there is no numeral
1130 static int digit (int c
) { return '0' <= c
&& c
<= '9'; }
1132 static int getnum (const char **fmt
, int df
) {
1133 if (!digit(**fmt
)) /* no number? */
1134 return df
; /* return default value */
1138 a
= a
*10 + (*((*fmt
)++) - '0');
1139 } while (digit(**fmt
) && a
<= ((int)MAXSIZE
- 9)/10);
1146 ** Read an integer numeral and raises an error if it is larger
1147 ** than the maximum size for integers.
1149 static int getnumlimit (Header
*h
, const char **fmt
, int df
) {
1150 int sz
= getnum(fmt
, df
);
1151 if (sz
> MAXINTSIZE
|| sz
<= 0)
1152 luaL_error(h
->L
, "integral size (%d) out of limits [1,%d]",
1159 ** Initialize Header
1161 static void initheader (lua_State
*L
, Header
*h
) {
1163 h
->islittle
= nativeendian
.little
;
1169 ** Read and classify next option. 'size' is filled with option's size.
1171 static KOption
getoption (Header
*h
, const char **fmt
, int *size
) {
1172 int opt
= *((*fmt
)++);
1173 *size
= 0; /* default */
1175 case 'b': *size
= sizeof(char); return Kint
;
1176 case 'B': *size
= sizeof(char); return Kuint
;
1177 case 'h': *size
= sizeof(short); return Kint
;
1178 case 'H': *size
= sizeof(short); return Kuint
;
1179 case 'l': *size
= sizeof(long); return Kint
;
1180 case 'L': *size
= sizeof(long); return Kuint
;
1181 case 'j': *size
= sizeof(lua_Integer
); return Kint
;
1182 case 'J': *size
= sizeof(lua_Integer
); return Kuint
;
1183 case 'T': *size
= sizeof(size_t); return Kuint
;
1185 case 'f': *size
= sizeof(float); return Kfloat
;
1186 case 'd': *size
= sizeof(double); return Kfloat
;
1187 case 'n': *size
= sizeof(lua_Number
); return Kfloat
;
1189 case 'n': *size
= sizeof(lua_Number
); return Kint
;
1191 case 'i': *size
= getnumlimit(h
, fmt
, sizeof(int)); return Kint
;
1192 case 'I': *size
= getnumlimit(h
, fmt
, sizeof(int)); return Kuint
;
1193 case 's': *size
= getnumlimit(h
, fmt
, sizeof(size_t)); return Kstring
;
1195 *size
= getnum(fmt
, -1);
1197 luaL_error(h
->L
, "missing size for format option 'c'");
1199 case 'z': return Kzstr
;
1200 case 'x': *size
= 1; return Kpadding
;
1201 case 'X': return Kpaddalign
;
1203 case '<': h
->islittle
= 1; break;
1204 case '>': h
->islittle
= 0; break;
1205 case '=': h
->islittle
= nativeendian
.little
; break;
1206 case '!': h
->maxalign
= getnumlimit(h
, fmt
, MAXALIGN
); break;
1207 default: luaL_error(h
->L
, "invalid format option '%c'", opt
);
1214 ** Read, classify, and fill other details about the next option.
1215 ** 'psize' is filled with option's size, 'notoalign' with its
1216 ** alignment requirements.
1217 ** Local variable 'size' gets the size to be aligned. (Kpadal option
1218 ** always gets its full alignment, other options are limited by
1219 ** the maximum alignment ('maxalign'). Kchar option needs no alignment
1220 ** despite its size.
1222 static KOption
getdetails (Header
*h
, size_t totalsize
,
1223 const char **fmt
, int *psize
, int *ntoalign
) {
1224 KOption opt
= getoption(h
, fmt
, psize
);
1225 int align
= *psize
; /* usually, alignment follows size */
1226 if (opt
== Kpaddalign
) { /* 'X' gets alignment from following option */
1227 if (**fmt
== '\0' || getoption(h
, fmt
, &align
) == Kchar
|| align
== 0)
1228 luaL_argerror(h
->L
, 1, "invalid next option for option 'X'");
1230 if (align
<= 1 || opt
== Kchar
) /* need no alignment? */
1233 if (align
> h
->maxalign
) /* enforce maximum alignment */
1234 align
= h
->maxalign
;
1235 if ((align
& (align
- 1)) != 0) /* is 'align' not a power of 2? */
1236 luaL_argerror(h
->L
, 1, "format asks for alignment not power of 2");
1237 *ntoalign
= (align
- (int)(totalsize
& (align
- 1))) & (align
- 1);
1244 ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
1245 ** The final 'if' handles the case when 'size' is larger than
1246 ** the size of a Lua integer, correcting the extra sign-extension
1247 ** bytes if necessary (by default they would be zeros).
1249 static void packint (luaL_Buffer
*b
, lua_Unsigned n
,
1250 int islittle
, int size
, int neg
) {
1251 char *buff
= luaL_prepbuffsize(b
, size
);
1253 buff
[islittle
? 0 : size
- 1] = (char)(n
& MC
); /* first byte */
1254 for (i
= 1; i
< size
; i
++) {
1256 buff
[islittle
? i
: size
- 1 - i
] = (char)(n
& MC
);
1258 if (neg
&& size
> SZINT
) { /* negative number need sign extension? */
1259 for (i
= SZINT
; i
< size
; i
++) /* correct extra bytes */
1260 buff
[islittle
? i
: size
- 1 - i
] = (char)MC
;
1262 luaL_addsize(b
, size
); /* add result to buffer */
1268 ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
1269 ** given 'islittle' is different from native endianness.
1271 static void copywithendian (volatile char *dest
, volatile const char *src
,
1272 int size
, int islittle
) {
1273 if (islittle
== nativeendian
.little
) {
1275 *(dest
++) = *(src
++);
1280 *(dest
--) = *(src
++);
1286 static int str_pack (lua_State
*L
) {
1289 const char *fmt
= luaL_checkstring(L
, 1); /* format string */
1290 int arg
= 1; /* current argument to pack */
1291 size_t totalsize
= 0; /* accumulate total size of result */
1293 lua_pushnil(L
); /* mark to separate arguments from string buffer */
1294 luaL_buffinit(L
, &b
);
1295 while (*fmt
!= '\0') {
1297 KOption opt
= getdetails(&h
, totalsize
, &fmt
, &size
, &ntoalign
);
1298 totalsize
+= ntoalign
+ size
;
1299 while (ntoalign
-- > 0)
1300 luaL_addchar(&b
, LUA_PACKPADBYTE
); /* fill alignment */
1303 case Kint
: { /* signed integers */
1304 lua_Integer n
= luaL_checkinteger(L
, arg
);
1305 if (size
< SZINT
) { /* need overflow check? */
1306 lua_Integer lim
= (lua_Integer
)1 << ((size
* NB
) - 1);
1307 luaL_argcheck(L
, -lim
<= n
&& n
< lim
, arg
, "integer overflow");
1309 packint(&b
, (lua_Unsigned
)n
, h
.islittle
, size
, (n
< 0));
1312 case Kuint
: { /* unsigned integers */
1313 lua_Integer n
= luaL_checkinteger(L
, arg
);
1314 if (size
< SZINT
) /* need overflow check? */
1315 luaL_argcheck(L
, (lua_Unsigned
)n
< ((lua_Unsigned
)1 << (size
* NB
)),
1316 arg
, "unsigned overflow");
1317 packint(&b
, (lua_Unsigned
)n
, h
.islittle
, size
, 0);
1321 case Kfloat
: { /* floating-point options */
1323 char *buff
= luaL_prepbuffsize(&b
, size
);
1324 lua_Number n
= luaL_checknumber(L
, arg
); /* get argument */
1325 if (size
== sizeof(u
.f
)) u
.f
= (float)n
; /* copy it into 'u' */
1326 else if (size
== sizeof(u
.d
)) u
.d
= (double)n
;
1328 /* move 'u' to final result, correcting endianness if needed */
1329 copywithendian(buff
, u
.buff
, size
, h
.islittle
);
1330 luaL_addsize(&b
, size
);
1334 case Kchar
: { /* fixed-size string */
1336 const char *s
= luaL_checklstring(L
, arg
, &len
);
1337 luaL_argcheck(L
, len
== (size_t)size
, arg
, "wrong length");
1338 luaL_addlstring(&b
, s
, size
);
1341 case Kstring
: { /* strings with length count */
1343 const char *s
= luaL_checklstring(L
, arg
, &len
);
1344 luaL_argcheck(L
, size
>= (int)sizeof(size_t) ||
1345 len
< ((size_t)1 << (size
* NB
)),
1346 arg
, "string length does not fit in given size");
1347 packint(&b
, (lua_Unsigned
)len
, h
.islittle
, size
, 0); /* pack length */
1348 luaL_addlstring(&b
, s
, len
);
1352 case Kzstr
: { /* zero-terminated string */
1354 const char *s
= luaL_checklstring(L
, arg
, &len
);
1355 luaL_argcheck(L
, strlen(s
) == len
, arg
, "string contains zeros");
1356 luaL_addlstring(&b
, s
, len
);
1357 luaL_addchar(&b
, '\0'); /* add zero at the end */
1358 totalsize
+= len
+ 1;
1361 case Kpadding
: luaL_addchar(&b
, LUA_PACKPADBYTE
); /* FALLTHROUGH */
1362 case Kpaddalign
: case Knop
:
1363 arg
--; /* undo increment */
1367 luaL_pushresult(&b
);
1372 static int str_packsize (lua_State
*L
) {
1374 const char *fmt
= luaL_checkstring(L
, 1); /* format string */
1375 size_t totalsize
= 0; /* accumulate total size of result */
1377 while (*fmt
!= '\0') {
1379 KOption opt
= getdetails(&h
, totalsize
, &fmt
, &size
, &ntoalign
);
1380 size
+= ntoalign
; /* total space used by option */
1381 luaL_argcheck(L
, totalsize
<= MAXSIZE
- size
, 1,
1382 "format result too large");
1385 case Kstring
: /* strings with length count */
1386 case Kzstr
: /* zero-terminated string */
1387 luaL_argerror(L
, 1, "variable-length format");
1392 lua_pushinteger(L
, (lua_Integer
)totalsize
);
1398 ** Unpack an integer with 'size' bytes and 'islittle' endianness.
1399 ** If size is smaller than the size of a Lua integer and integer
1400 ** is signed, must do sign extension (propagating the sign to the
1401 ** higher bits); if size is larger than the size of a Lua integer,
1402 ** it must check the unread bytes to see whether they do not cause an
1405 static lua_Integer
unpackint (lua_State
*L
, const char *str
,
1406 int islittle
, int size
, int issigned
) {
1407 lua_Unsigned res
= 0;
1409 int limit
= (size
<= SZINT
) ? size
: SZINT
;
1410 for (i
= limit
- 1; i
>= 0; i
--) {
1412 res
|= (lua_Unsigned
)(unsigned char)str
[islittle
? i
: size
- 1 - i
];
1414 if (size
< SZINT
) { /* real size smaller than lua_Integer? */
1415 if (issigned
) { /* needs sign extension? */
1416 lua_Unsigned mask
= (lua_Unsigned
)1 << (size
*NB
- 1);
1417 res
= ((res
^ mask
) - mask
); /* do sign extension */
1420 else if (size
> SZINT
) { /* must check unread bytes */
1421 int mask
= (!issigned
|| (lua_Integer
)res
>= 0) ? 0 : MC
;
1422 for (i
= limit
; i
< size
; i
++) {
1423 if ((unsigned char)str
[islittle
? i
: size
- 1 - i
] != mask
)
1424 luaL_error(L
, "%d-byte integer does not fit into Lua Integer", size
);
1427 return (lua_Integer
)res
;
1431 static int str_unpack (lua_State
*L
) {
1433 const char *fmt
= luaL_checkstring(L
, 1);
1435 const char *data
= luaL_checklstring(L
, 2, &ld
);
1436 size_t pos
= (size_t)posrelat(luaL_optinteger(L
, 3, 1), ld
) - 1;
1437 int n
= 0; /* number of results */
1438 luaL_argcheck(L
, pos
<= ld
, 3, "initial position out of string");
1440 while (*fmt
!= '\0') {
1442 KOption opt
= getdetails(&h
, pos
, &fmt
, &size
, &ntoalign
);
1443 if ((size_t)ntoalign
+ size
> ~pos
|| pos
+ ntoalign
+ size
> ld
)
1444 luaL_argerror(L
, 2, "data string too short");
1445 pos
+= ntoalign
; /* skip alignment */
1446 /* stack space for item + next position */
1447 luaL_checkstack(L
, 2, "too many results");
1452 lua_Integer res
= unpackint(L
, data
+ pos
, h
.islittle
, size
,
1454 lua_pushinteger(L
, res
);
1461 copywithendian(u
.buff
, data
+ pos
, size
, h
.islittle
);
1462 if (size
== sizeof(u
.f
)) num
= (lua_Number
)u
.f
;
1463 else if (size
== sizeof(u
.d
)) num
= (lua_Number
)u
.d
;
1465 lua_pushnumber(L
, num
);
1470 lua_pushlstring(L
, data
+ pos
, size
);
1474 size_t len
= (size_t)unpackint(L
, data
+ pos
, h
.islittle
, size
, 0);
1475 luaL_argcheck(L
, pos
+ len
+ size
<= ld
, 2, "data string too short");
1476 lua_pushlstring(L
, data
+ pos
+ size
, len
);
1477 pos
+= len
; /* skip string */
1481 size_t len
= (int)strlen(data
+ pos
);
1482 lua_pushlstring(L
, data
+ pos
, len
);
1483 pos
+= len
+ 1; /* skip string plus final '\0' */
1486 case Kpaddalign
: case Kpadding
: case Knop
:
1487 n
--; /* undo increment */
1492 lua_pushinteger(L
, pos
+ 1); /* next position */
1496 /* }====================================================== */
1499 static const luaL_Reg strlib
[] = {
1504 {"format", str_format
},
1508 {"lower", str_lower
},
1509 {"match", str_match
},
1511 {"reverse", str_reverse
},
1513 {"upper", str_upper
},
1515 {"packsize", str_packsize
},
1516 {"unpack", str_unpack
},
1521 static void createmetatable (lua_State
*L
) {
1522 lua_createtable(L
, 0, 1); /* table to be metatable for strings */
1523 lua_pushliteral(L
, ""); /* dummy string */
1524 lua_pushvalue(L
, -2); /* copy table */
1525 lua_setmetatable(L
, -2); /* set table as metatable for strings */
1526 lua_pop(L
, 1); /* pop dummy string */
1527 lua_pushvalue(L
, -2); /* get string library */
1528 lua_setfield(L
, -2, "__index"); /* metatable.__index = string */
1529 lua_pop(L
, 1); /* pop metatable */
1534 ** Open string library
1536 LUAMOD_API
int luaopen_string (lua_State
*L
) {
1537 luaL_newlib(L
, strlib
);