summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lstrlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lstrlib.c')
-rw-r--r--apps/plugins/lua/lstrlib.c580
1 files changed, 215 insertions, 365 deletions
diff --git a/apps/plugins/lua/lstrlib.c b/apps/plugins/lua/lstrlib.c
index 04cc2f60d6..3d6103692f 100644
--- a/apps/plugins/lua/lstrlib.c
+++ b/apps/plugins/lua/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $ 2** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -20,58 +20,47 @@
20#include "lualib.h" 20#include "lualib.h"
21 21
22 22
23/*
24** maximum number of captures that a pattern can do during
25** pattern-matching. This limit is arbitrary.
26*/
27#if !defined(LUA_MAXCAPTURES)
28#define LUA_MAXCAPTURES 32
29#endif
30
31
32/* macro to `unsign' a character */ 23/* macro to `unsign' a character */
33#define uchar(c) ((unsigned char)(c)) 24#define uchar(c) ((unsigned char)(c))
34 25
35 26
36 27
37static int str_len (lua_State *L) { 28static int str_len (lua_State *L) {
38 size_t l; 29 size_t l;
39 luaL_checklstring(L, 1, &l); 30 luaL_checklstring(L, 1, &l);
40 lua_pushinteger(L, (lua_Integer)l); 31 lua_pushinteger(L, l);
41 return 1; 32 return 1;
42} 33}
43 34
44 35
45/* translate a relative string position: negative means back from end */ 36static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
46static size_t posrelat (ptrdiff_t pos, size_t len) { 37 /* relative string position: negative means back from end */
47 if (pos >= 0) return (size_t)pos; 38 if (pos < 0) pos += (ptrdiff_t)len + 1;
48 else if (0u - (size_t)pos > len) return 0; 39 return (pos >= 0) ? pos : 0;
49 else return len - ((size_t)-pos) + 1;
50} 40}
51 41
52 42
53static int str_sub (lua_State *L) { 43static int str_sub (lua_State *L) {
54 size_t l; 44 size_t l;
55 const char *s = luaL_checklstring(L, 1, &l); 45 const char *s = luaL_checklstring(L, 1, &l);
56 size_t start = posrelat(luaL_checkinteger(L, 2), l); 46 ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
57 size_t end = posrelat(luaL_optinteger(L, 3, -1), l); 47 ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
58 if (start < 1) start = 1; 48 if (start < 1) start = 1;
59 if (end > l) end = l; 49 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
60 if (start <= end) 50 if (start <= end)
61 lua_pushlstring(L, s + start - 1, end - start + 1); 51 lua_pushlstring(L, s+start-1, end-start+1);
62 else lua_pushliteral(L, ""); 52 else lua_pushliteral(L, "");
63 return 1; 53 return 1;
64} 54}
65 55
66 56
67static int str_reverse (lua_State *L) { 57static int str_reverse (lua_State *L) {
68 size_t l, i; 58 size_t l;
69 luaL_Buffer b; 59 luaL_Buffer b;
70 const char *s = luaL_checklstring(L, 1, &l); 60 const char *s = luaL_checklstring(L, 1, &l);
71 char *p = luaL_buffinitsize(L, &b, l); 61 luaL_buffinit(L, &b);
72 for (i = 0; i < l; i++) 62 while (l--) luaL_addchar(&b, s[l]);
73 p[i] = s[l - i - 1]; 63 luaL_pushresult(&b);
74 luaL_pushresultsize(&b, l);
75 return 1; 64 return 1;
76} 65}
77 66
@@ -81,10 +70,10 @@ static int str_lower (lua_State *L) {
81 size_t i; 70 size_t i;
82 luaL_Buffer b; 71 luaL_Buffer b;
83 const char *s = luaL_checklstring(L, 1, &l); 72 const char *s = luaL_checklstring(L, 1, &l);
84 char *p = luaL_buffinitsize(L, &b, l); 73 luaL_buffinit(L, &b);
85 for (i=0; i<l; i++) 74 for (i=0; i<l; i++)
86 p[i] = tolower(uchar(s[i])); 75 luaL_addchar(&b, tolower(uchar(s[i])));
87 luaL_pushresultsize(&b, l); 76 luaL_pushresult(&b);
88 return 1; 77 return 1;
89} 78}
90 79
@@ -94,38 +83,22 @@ static int str_upper (lua_State *L) {
94 size_t i; 83 size_t i;
95 luaL_Buffer b; 84 luaL_Buffer b;
96 const char *s = luaL_checklstring(L, 1, &l); 85 const char *s = luaL_checklstring(L, 1, &l);
97 char *p = luaL_buffinitsize(L, &b, l); 86 luaL_buffinit(L, &b);
98 for (i=0; i<l; i++) 87 for (i=0; i<l; i++)
99 p[i] = toupper(uchar(s[i])); 88 luaL_addchar(&b, toupper(uchar(s[i])));
100 luaL_pushresultsize(&b, l); 89 luaL_pushresult(&b);
101 return 1; 90 return 1;
102} 91}
103 92
104
105/* reasonable limit to avoid arithmetic overflow */
106#define MAXSIZE ((~(size_t)0) >> 1)
107
108static int str_rep (lua_State *L) { 93static int str_rep (lua_State *L) {
109 size_t l, lsep; 94 size_t l;
95 luaL_Buffer b;
110 const char *s = luaL_checklstring(L, 1, &l); 96 const char *s = luaL_checklstring(L, 1, &l);
111 int n = luaL_checkint(L, 2); 97 int n = luaL_checkint(L, 2);
112 const char *sep = luaL_optlstring(L, 3, "", &lsep); 98 luaL_buffinit(L, &b);
113 if (n <= 0) lua_pushliteral(L, ""); 99 while (n-- > 0)
114 else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ 100 luaL_addlstring(&b, s, l);
115 return luaL_error(L, "resulting string too large"); 101 luaL_pushresult(&b);
116 else {
117 size_t totallen = n * l + (n - 1) * lsep;
118 luaL_Buffer b;
119 char *p = luaL_buffinitsize(L, &b, totallen);
120 while (n-- > 1) { /* first n-1 copies (followed by separator) */
121 memcpy(p, s, l * sizeof(char)); p += l;
122 if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */
123 memcpy(p, sep, lsep * sizeof(char)); p += lsep;
124 }
125 }
126 memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
127 luaL_pushresultsize(&b, totallen);
128 }
129 return 1; 102 return 1;
130} 103}
131 104
@@ -133,15 +106,15 @@ static int str_rep (lua_State *L) {
133static int str_byte (lua_State *L) { 106static int str_byte (lua_State *L) {
134 size_t l; 107 size_t l;
135 const char *s = luaL_checklstring(L, 1, &l); 108 const char *s = luaL_checklstring(L, 1, &l);
136 size_t posi = posrelat(luaL_optinteger(L, 2, 1), l); 109 ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
137 size_t pose = posrelat(luaL_optinteger(L, 3, posi), l); 110 ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
138 int n, i; 111 int n, i;
139 if (posi < 1) posi = 1; 112 if (posi <= 0) posi = 1;
140 if (pose > l) pose = l; 113 if ((size_t)pose > l) pose = l;
141 if (posi > pose) return 0; /* empty interval; return no values */ 114 if (posi > pose) return 0; /* empty interval; return no values */
142 n = (int)(pose - posi + 1); 115 n = (int)(pose - posi + 1);
143 if (posi + n <= pose) /* (size_t -> int) overflow? */ 116 if (posi + n <= pose) /* overflow? */
144 return luaL_error(L, "string slice too long"); 117 luaL_error(L, "string slice too long");
145 luaL_checkstack(L, n, "string slice too long"); 118 luaL_checkstack(L, n, "string slice too long");
146 for (i=0; i<n; i++) 119 for (i=0; i<n; i++)
147 lua_pushinteger(L, uchar(s[posi+i-1])); 120 lua_pushinteger(L, uchar(s[posi+i-1]));
@@ -153,13 +126,13 @@ static int str_char (lua_State *L) {
153 int n = lua_gettop(L); /* number of arguments */ 126 int n = lua_gettop(L); /* number of arguments */
154 int i; 127 int i;
155 luaL_Buffer b; 128 luaL_Buffer b;
156 char *p = luaL_buffinitsize(L, &b, n); 129 luaL_buffinit(L, &b);
157 for (i=1; i<=n; i++) { 130 for (i=1; i<=n; i++) {
158 int c = luaL_checkint(L, i); 131 int c = luaL_checkint(L, i);
159 luaL_argcheck(L, uchar(c) == c, i, "value out of range"); 132 luaL_argcheck(L, uchar(c) == c, i, "invalid value");
160 p[i - 1] = uchar(c); 133 luaL_addchar(&b, uchar(c));
161 } 134 }
162 luaL_pushresultsize(&b, n); 135 luaL_pushresult(&b);
163 return 1; 136 return 1;
164} 137}
165 138
@@ -177,7 +150,7 @@ static int str_dump (lua_State *L) {
177 lua_settop(L, 1); 150 lua_settop(L, 1);
178 luaL_buffinit(L,&b); 151 luaL_buffinit(L,&b);
179 if (lua_dump(L, writer, &b) != 0) 152 if (lua_dump(L, writer, &b) != 0)
180 return luaL_error(L, "unable to dump given function"); 153 luaL_error(L, "unable to dump given function");
181 luaL_pushresult(&b); 154 luaL_pushresult(&b);
182 return 1; 155 return 1;
183} 156}
@@ -194,12 +167,9 @@ static int str_dump (lua_State *L) {
194#define CAP_UNFINISHED (-1) 167#define CAP_UNFINISHED (-1)
195#define CAP_POSITION (-2) 168#define CAP_POSITION (-2)
196 169
197
198typedef struct MatchState { 170typedef struct MatchState {
199 int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
200 const char *src_init; /* init of source string */ 171 const char *src_init; /* init of source string */
201 const char *src_end; /* end ('\0') of source string */ 172 const char *src_end; /* end (`\0') of source string */
202 const char *p_end; /* end ('\0') of pattern */
203 lua_State *L; 173 lua_State *L;
204 int level; /* total number of captures (finished or unfinished) */ 174 int level; /* total number of captures (finished or unfinished) */
205 struct { 175 struct {
@@ -209,16 +179,6 @@ typedef struct MatchState {
209} MatchState; 179} MatchState;
210 180
211 181
212/* recursive function */
213static const char *match (MatchState *ms, const char *s, const char *p);
214
215
216/* maximum recursion depth for 'match' */
217#if !defined(MAXCCALLS)
218#define MAXCCALLS 200
219#endif
220
221
222#define L_ESC '%' 182#define L_ESC '%'
223#define SPECIALS "^$*+?.([%-" 183#define SPECIALS "^$*+?.([%-"
224 184
@@ -226,7 +186,7 @@ static const char *match (MatchState *ms, const char *s, const char *p);
226static int check_capture (MatchState *ms, int l) { 186static int check_capture (MatchState *ms, int l) {
227 l -= '1'; 187 l -= '1';
228 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) 188 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
229 return luaL_error(ms->L, "invalid capture index %%%d", l + 1); 189 return luaL_error(ms->L, "invalid capture index");
230 return l; 190 return l;
231} 191}
232 192
@@ -242,16 +202,16 @@ static int capture_to_close (MatchState *ms) {
242static const char *classend (MatchState *ms, const char *p) { 202static const char *classend (MatchState *ms, const char *p) {
243 switch (*p++) { 203 switch (*p++) {
244 case L_ESC: { 204 case L_ESC: {
245 if (p == ms->p_end) 205 if (*p == '\0')
246 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); 206 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
247 return p+1; 207 return p+1;
248 } 208 }
249 case '[': { 209 case '[': {
250 if (*p == '^') p++; 210 if (*p == '^') p++;
251 do { /* look for a `]' */ 211 do { /* look for a `]' */
252 if (p == ms->p_end) 212 if (*p == '\0')
253 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); 213 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
254 if (*(p++) == L_ESC && p < ms->p_end) 214 if (*(p++) == L_ESC && *p != '\0')
255 p++; /* skip escapes (e.g. `%]') */ 215 p++; /* skip escapes (e.g. `%]') */
256 } while (*p != ']'); 216 } while (*p != ']');
257 return p+1; 217 return p+1;
@@ -269,14 +229,13 @@ static int match_class (int c, int cl) {
269 case 'a' : res = isalpha(c); break; 229 case 'a' : res = isalpha(c); break;
270 case 'c' : res = iscntrl(c); break; 230 case 'c' : res = iscntrl(c); break;
271 case 'd' : res = isdigit(c); break; 231 case 'd' : res = isdigit(c); break;
272 case 'g' : res = isgraph(c); break;
273 case 'l' : res = islower(c); break; 232 case 'l' : res = islower(c); break;
274 case 'p' : res = ispunct(c); break; 233 case 'p' : res = ispunct(c); break;
275 case 's' : res = isspace(c); break; 234 case 's' : res = isspace(c); break;
276 case 'u' : res = isupper(c); break; 235 case 'u' : res = isupper(c); break;
277 case 'w' : res = isalnum(c); break; 236 case 'w' : res = isalnum(c); break;
278 case 'x' : res = isxdigit(c); break; 237 case 'x' : res = isxdigit(c); break;
279 case 'z' : res = (c == 0); break; /* deprecated option */ 238 case 'z' : res = (c == 0); break;
280 default: return (cl == c); 239 default: return (cl == c);
281 } 240 }
282 return (islower(cl) ? res : !res); 241 return (islower(cl) ? res : !res);
@@ -306,27 +265,23 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
306} 265}
307 266
308 267
309static int singlematch (MatchState *ms, const char *s, const char *p, 268static int singlematch (int c, const char *p, const char *ep) {
310 const char *ep) { 269 switch (*p) {
311 if (s >= ms->src_end) 270 case '.': return 1; /* matches any char */
312 return 0; 271 case L_ESC: return match_class(c, uchar(*(p+1)));
313 else { 272 case '[': return matchbracketclass(c, p, ep-1);
314 int c = uchar(*s); 273 default: return (uchar(*p) == c);
315 switch (*p) {
316 case '.': return 1; /* matches any char */
317 case L_ESC: return match_class(c, uchar(*(p+1)));
318 case '[': return matchbracketclass(c, p, ep-1);
319 default: return (uchar(*p) == c);
320 }
321 } 274 }
322} 275}
323 276
324 277
278static const char *match (MatchState *ms, const char *s, const char *p);
279
280
325static const char *matchbalance (MatchState *ms, const char *s, 281static const char *matchbalance (MatchState *ms, const char *s,
326 const char *p) { 282 const char *p) {
327 if (p >= ms->p_end - 1) 283 if (*p == 0 || *(p+1) == 0)
328 luaL_error(ms->L, "malformed pattern " 284 luaL_error(ms->L, "unbalanced pattern");
329 "(missing arguments to " LUA_QL("%%b") ")");
330 if (*s != *p) return NULL; 285 if (*s != *p) return NULL;
331 else { 286 else {
332 int b = *p; 287 int b = *p;
@@ -346,7 +301,7 @@ static const char *matchbalance (MatchState *ms, const char *s,
346static const char *max_expand (MatchState *ms, const char *s, 301static const char *max_expand (MatchState *ms, const char *s,
347 const char *p, const char *ep) { 302 const char *p, const char *ep) {
348 ptrdiff_t i = 0; /* counts maximum expand for item */ 303 ptrdiff_t i = 0; /* counts maximum expand for item */
349 while (singlematch(ms, s + i, p, ep)) 304 while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
350 i++; 305 i++;
351 /* keeps trying to match with the maximum repetitions */ 306 /* keeps trying to match with the maximum repetitions */
352 while (i>=0) { 307 while (i>=0) {
@@ -364,7 +319,7 @@ static const char *min_expand (MatchState *ms, const char *s,
364 const char *res = match(ms, s, ep+1); 319 const char *res = match(ms, s, ep+1);
365 if (res != NULL) 320 if (res != NULL)
366 return res; 321 return res;
367 else if (singlematch(ms, s, p, ep)) 322 else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
368 s++; /* try with one more repetition */ 323 s++; /* try with one more repetition */
369 else return NULL; 324 else return NULL;
370 } 325 }
@@ -408,105 +363,80 @@ static const char *match_capture (MatchState *ms, const char *s, int l) {
408 363
409 364
410static const char *match (MatchState *ms, const char *s, const char *p) { 365static const char *match (MatchState *ms, const char *s, const char *p) {
411 if (ms->matchdepth-- == 0)
412 luaL_error(ms->L, "pattern too complex");
413 init: /* using goto's to optimize tail recursion */ 366 init: /* using goto's to optimize tail recursion */
414 if (p != ms->p_end) { /* end of pattern? */ 367 switch (*p) {
415 switch (*p) { 368 case '(': { /* start capture */
416 case '(': { /* start capture */ 369 if (*(p+1) == ')') /* position capture? */
417 if (*(p + 1) == ')') /* position capture? */ 370 return start_capture(ms, s, p+2, CAP_POSITION);
418 s = start_capture(ms, s, p + 2, CAP_POSITION); 371 else
419 else 372 return start_capture(ms, s, p+1, CAP_UNFINISHED);
420 s = start_capture(ms, s, p + 1, CAP_UNFINISHED); 373 }
421 break; 374 case ')': { /* end capture */
422 } 375 return end_capture(ms, s, p+1);
423 case ')': { /* end capture */ 376 }
424 s = end_capture(ms, s, p + 1); 377 case L_ESC: {
425 break; 378 switch (*(p+1)) {
426 } 379 case 'b': { /* balanced string? */
427 case '$': { 380 s = matchbalance(ms, s, p+2);
428 if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */ 381 if (s == NULL) return NULL;
429 goto dflt; /* no; go to default */ 382 p+=4; goto init; /* else return match(ms, s, p+4); */
430 s = (s == ms->src_end) ? s : NULL; /* check end of string */ 383 }
431 break; 384 case 'f': { /* frontier? */
432 } 385 const char *ep; char previous;
433 case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ 386 p += 2;
434 switch (*(p + 1)) { 387 if (*p != '[')
435 case 'b': { /* balanced string? */ 388 luaL_error(ms->L, "missing " LUA_QL("[") " after "
436 s = matchbalance(ms, s, p + 2); 389 LUA_QL("%%f") " in pattern");
437 if (s != NULL) { 390 ep = classend(ms, p); /* points to what is next */
438 p += 4; goto init; /* return match(ms, s, p + 4); */ 391 previous = (s == ms->src_init) ? '\0' : *(s-1);
439 } /* else fail (s == NULL) */ 392 if (matchbracketclass(uchar(previous), p, ep-1) ||
440 break; 393 !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
441 } 394 p=ep; goto init; /* else return match(ms, s, ep); */
442 case 'f': { /* frontier? */ 395 }
443 const char *ep; char previous; 396 default: {
444 p += 2; 397 if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
445 if (*p != '[') 398 s = match_capture(ms, s, uchar(*(p+1)));
446 luaL_error(ms->L, "missing " LUA_QL("[") " after " 399 if (s == NULL) return NULL;
447 LUA_QL("%%f") " in pattern"); 400 p+=2; goto init; /* else return match(ms, s, p+2) */
448 ep = classend(ms, p); /* points to what is next */
449 previous = (s == ms->src_init) ? '\0' : *(s - 1);
450 if (!matchbracketclass(uchar(previous), p, ep - 1) &&
451 matchbracketclass(uchar(*s), p, ep - 1)) {
452 p = ep; goto init; /* return match(ms, s, ep); */
453 }
454 s = NULL; /* match failed */
455 break;
456 }
457 case '0': case '1': case '2': case '3':
458 case '4': case '5': case '6': case '7':
459 case '8': case '9': { /* capture results (%0-%9)? */
460 s = match_capture(ms, s, uchar(*(p + 1)));
461 if (s != NULL) {
462 p += 2; goto init; /* return match(ms, s, p + 2) */
463 }
464 break;
465 } 401 }
466 default: goto dflt; 402 goto dflt; /* case default */
467 } 403 }
468 break;
469 } 404 }
470 default: dflt: { /* pattern class plus optional suffix */ 405 }
471 const char *ep = classend(ms, p); /* points to optional suffix */ 406 case '\0': { /* end of pattern */
472 /* does not match at least once? */ 407 return s; /* match succeeded */
473 if (!singlematch(ms, s, p, ep)) { 408 }
474 if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */ 409 case '$': {
475 p = ep + 1; goto init; /* return match(ms, s, ep + 1); */ 410 if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
476 } 411 return (s == ms->src_end) ? s : NULL; /* check end of string */
477 else /* '+' or no suffix */ 412 else goto dflt;
478 s = NULL; /* fail */ 413 }
414 default: dflt: { /* it is a pattern item */
415 const char *ep = classend(ms, p); /* points to what is next */
416 int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
417 switch (*ep) {
418 case '?': { /* optional */
419 const char *res;
420 if (m && ((res=match(ms, s+1, ep+1)) != NULL))
421 return res;
422 p=ep+1; goto init; /* else return match(ms, s, ep+1); */
479 } 423 }
480 else { /* matched once */ 424 case '*': { /* 0 or more repetitions */
481 switch (*ep) { /* handle optional suffix */ 425 return max_expand(ms, s, p, ep);
482 case '?': { /* optional */ 426 }
483 const char *res; 427 case '+': { /* 1 or more repetitions */
484 if ((res = match(ms, s + 1, ep + 1)) != NULL) 428 return (m ? max_expand(ms, s+1, p, ep) : NULL);
485 s = res; 429 }
486 else { 430 case '-': { /* 0 or more repetitions (minimum) */
487 p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */ 431 return min_expand(ms, s, p, ep);
488 } 432 }
489 break; 433 default: {
490 } 434 if (!m) return NULL;
491 case '+': /* 1 or more repetitions */ 435 s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
492 s++; /* 1 match already done */
493 /* go through */
494 case '*': /* 0 or more repetitions */
495 s = max_expand(ms, s, p, ep);
496 break;
497 case '-': /* 0 or more repetitions (minimum) */
498 s = min_expand(ms, s, p, ep);
499 break;
500 default: /* no suffix */
501 s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
502 }
503 } 436 }
504 break;
505 } 437 }
506 } 438 }
507 } 439 }
508 ms->matchdepth++;
509 return s;
510} 440}
511 441
512 442
@@ -562,58 +492,37 @@ static int push_captures (MatchState *ms, const char *s, const char *e) {
562} 492}
563 493
564 494
565/* check whether pattern has no special characters */
566static int nospecials (const char *p, size_t l) {
567 size_t upto = 0;
568 do {
569 if (strpbrk(p + upto, SPECIALS))
570 return 0; /* pattern has a special character */
571 upto += strlen(p + upto) + 1; /* may have more after \0 */
572 } while (upto <= l);
573 return 1; /* no special chars found */
574}
575
576
577static int str_find_aux (lua_State *L, int find) { 495static int str_find_aux (lua_State *L, int find) {
578 size_t ls, lp; 496 size_t l1, l2;
579 const char *s = luaL_checklstring(L, 1, &ls); 497 const char *s = luaL_checklstring(L, 1, &l1);
580 const char *p = luaL_checklstring(L, 2, &lp); 498 const char *p = luaL_checklstring(L, 2, &l2);
581 size_t init = posrelat(luaL_optinteger(L, 3, 1), ls); 499 ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
582 if (init < 1) init = 1; 500 if (init < 0) init = 0;
583 else if (init > ls + 1) { /* start after string's end? */ 501 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
584 lua_pushnil(L); /* cannot find anything */ 502 if (find && (lua_toboolean(L, 4) || /* explicit request? */
585 return 1; 503 strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
586 }
587 /* explicit request or no special characters? */
588 if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
589 /* do a plain search */ 504 /* do a plain search */
590 const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); 505 const char *s2 = lmemfind(s+init, l1-init, p, l2);
591 if (s2) { 506 if (s2) {
592 lua_pushinteger(L, s2 - s + 1); 507 lua_pushinteger(L, s2-s+1);
593 lua_pushinteger(L, s2 - s + lp); 508 lua_pushinteger(L, s2-s+l2);
594 return 2; 509 return 2;
595 } 510 }
596 } 511 }
597 else { 512 else {
598 MatchState ms; 513 MatchState ms;
599 const char *s1 = s + init - 1; 514 int anchor = (*p == '^') ? (p++, 1) : 0;
600 int anchor = (*p == '^'); 515 const char *s1=s+init;
601 if (anchor) {
602 p++; lp--; /* skip anchor character */
603 }
604 ms.L = L; 516 ms.L = L;
605 ms.matchdepth = MAXCCALLS;
606 ms.src_init = s; 517 ms.src_init = s;
607 ms.src_end = s + ls; 518 ms.src_end = s+l1;
608 ms.p_end = p + lp;
609 do { 519 do {
610 const char *res; 520 const char *res;
611 ms.level = 0; 521 ms.level = 0;
612 lua_assert(ms.matchdepth == MAXCCALLS);
613 if ((res=match(&ms, s1, p)) != NULL) { 522 if ((res=match(&ms, s1, p)) != NULL) {
614 if (find) { 523 if (find) {
615 lua_pushinteger(L, s1 - s + 1); /* start */ 524 lua_pushinteger(L, s1-s+1); /* start */
616 lua_pushinteger(L, res - s); /* end */ 525 lua_pushinteger(L, res-s); /* end */
617 return push_captures(&ms, NULL, 0) + 2; 526 return push_captures(&ms, NULL, 0) + 2;
618 } 527 }
619 else 528 else
@@ -638,21 +547,18 @@ static int str_match (lua_State *L) {
638 547
639static int gmatch_aux (lua_State *L) { 548static int gmatch_aux (lua_State *L) {
640 MatchState ms; 549 MatchState ms;
641 size_t ls, lp; 550 size_t ls;
642 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); 551 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
643 const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp); 552 const char *p = lua_tostring(L, lua_upvalueindex(2));
644 const char *src; 553 const char *src;
645 ms.L = L; 554 ms.L = L;
646 ms.matchdepth = MAXCCALLS;
647 ms.src_init = s; 555 ms.src_init = s;
648 ms.src_end = s+ls; 556 ms.src_end = s+ls;
649 ms.p_end = p + lp;
650 for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); 557 for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
651 src <= ms.src_end; 558 src <= ms.src_end;
652 src++) { 559 src++) {
653 const char *e; 560 const char *e;
654 ms.level = 0; 561 ms.level = 0;
655 lua_assert(ms.matchdepth == MAXCCALLS);
656 if ((e = match(&ms, src, p)) != NULL) { 562 if ((e = match(&ms, src, p)) != NULL) {
657 lua_Integer newstart = e-s; 563 lua_Integer newstart = e-s;
658 if (e == src) newstart++; /* empty match? go at least one position */ 564 if (e == src) newstart++; /* empty match? go at least one position */
@@ -675,6 +581,12 @@ static int gmatch (lua_State *L) {
675} 581}
676 582
677 583
584static int gfind_nodef (lua_State *L) {
585 return luaL_error(L, LUA_QL("string.gfind") " was renamed to "
586 LUA_QL("string.gmatch"));
587}
588
589
678static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, 590static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
679 const char *e) { 591 const char *e) {
680 size_t l, i; 592 size_t l, i;
@@ -684,12 +596,8 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
684 luaL_addchar(b, news[i]); 596 luaL_addchar(b, news[i]);
685 else { 597 else {
686 i++; /* skip ESC */ 598 i++; /* skip ESC */
687 if (!isdigit(uchar(news[i]))) { 599 if (!isdigit(uchar(news[i])))
688 if (news[i] != L_ESC)
689 luaL_error(ms->L, "invalid use of " LUA_QL("%c")
690 " in replacement string", L_ESC);
691 luaL_addchar(b, news[i]); 600 luaL_addchar(b, news[i]);
692 }
693 else if (news[i] == '0') 601 else if (news[i] == '0')
694 luaL_addlstring(b, s, e - s); 602 luaL_addlstring(b, s, e - s);
695 else { 603 else {
@@ -702,9 +610,14 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
702 610
703 611
704static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, 612static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
705 const char *e, int tr) { 613 const char *e) {
706 lua_State *L = ms->L; 614 lua_State *L = ms->L;
707 switch (tr) { 615 switch (lua_type(L, 3)) {
616 case LUA_TNUMBER:
617 case LUA_TSTRING: {
618 add_s(ms, b, s, e);
619 return;
620 }
708 case LUA_TFUNCTION: { 621 case LUA_TFUNCTION: {
709 int n; 622 int n;
710 lua_pushvalue(L, 3); 623 lua_pushvalue(L, 3);
@@ -717,51 +630,41 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
717 lua_gettable(L, 3); 630 lua_gettable(L, 3);
718 break; 631 break;
719 } 632 }
720 default: { /* LUA_TNUMBER or LUA_TSTRING */
721 add_s(ms, b, s, e);
722 return;
723 }
724 } 633 }
725 if (!lua_toboolean(L, -1)) { /* nil or false? */ 634 if (!lua_toboolean(L, -1)) { /* nil or false? */
726 lua_pop(L, 1); 635 lua_pop(L, 1);
727 lua_pushlstring(L, s, e - s); /* keep original text */ 636 lua_pushlstring(L, s, e - s); /* keep original text */
728 } 637 }
729 else if (!lua_isstring(L, -1)) 638 else if (!lua_isstring(L, -1))
730 luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); 639 luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
731 luaL_addvalue(b); /* add result to accumulator */ 640 luaL_addvalue(b); /* add result to accumulator */
732} 641}
733 642
734 643
735static int str_gsub (lua_State *L) { 644static int str_gsub (lua_State *L) {
736 size_t srcl, lp; 645 size_t srcl;
737 const char *src = luaL_checklstring(L, 1, &srcl); 646 const char *src = luaL_checklstring(L, 1, &srcl);
738 const char *p = luaL_checklstring(L, 2, &lp); 647 const char *p = luaL_checkstring(L, 2);
739 int tr = lua_type(L, 3); 648 int tr = lua_type(L, 3);
740 size_t max_s = luaL_optinteger(L, 4, srcl+1); 649 int max_s = luaL_optint(L, 4, srcl+1);
741 int anchor = (*p == '^'); 650 int anchor = (*p == '^') ? (p++, 1) : 0;
742 size_t n = 0; 651 int n = 0;
743 MatchState ms; 652 MatchState ms;
744 luaL_Buffer b; 653 luaL_Buffer b;
745 luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || 654 luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
746 tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, 655 tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
747 "string/function/table expected"); 656 "string/function/table expected");
748 luaL_buffinit(L, &b); 657 luaL_buffinit(L, &b);
749 if (anchor) {
750 p++; lp--; /* skip anchor character */
751 }
752 ms.L = L; 658 ms.L = L;
753 ms.matchdepth = MAXCCALLS;
754 ms.src_init = src; 659 ms.src_init = src;
755 ms.src_end = src+srcl; 660 ms.src_end = src+srcl;
756 ms.p_end = p + lp;
757 while (n < max_s) { 661 while (n < max_s) {
758 const char *e; 662 const char *e;
759 ms.level = 0; 663 ms.level = 0;
760 lua_assert(ms.matchdepth == MAXCCALLS);
761 e = match(&ms, src, p); 664 e = match(&ms, src, p);
762 if (e) { 665 if (e) {
763 n++; 666 n++;
764 add_value(&ms, &b, src, e, tr); 667 add_value(&ms, &b, src, e);
765 } 668 }
766 if (e && e>src) /* non empty match? */ 669 if (e && e>src) /* non empty match? */
767 src = e; /* skip it */ 670 src = e; /* skip it */
@@ -779,46 +682,6 @@ static int str_gsub (lua_State *L) {
779/* }====================================================== */ 682/* }====================================================== */
780 683
781 684
782
783/*
784** {======================================================
785** STRING FORMAT
786** =======================================================
787*/
788
789/*
790** LUA_INTFRMLEN is the length modifier for integer conversions in
791** 'string.format'; LUA_INTFRM_T is the integer type corresponding to
792** the previous length
793*/
794#if !defined(LUA_INTFRMLEN) /* { */
795#if defined(LUA_USE_LONGLONG)
796
797#define LUA_INTFRMLEN "ll"
798#define LUA_INTFRM_T long long
799
800#else
801
802#define LUA_INTFRMLEN "l"
803#define LUA_INTFRM_T long
804
805#endif
806#endif /* } */
807
808
809/*
810** LUA_FLTFRMLEN is the length modifier for float conversions in
811** 'string.format'; LUA_FLTFRM_T is the float type corresponding to
812** the previous length
813*/
814#if !defined(LUA_FLTFRMLEN)
815
816#define LUA_FLTFRMLEN ""
817#define LUA_FLTFRM_T double
818
819#endif
820
821
822/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ 685/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
823#define MAX_ITEM 512 686#define MAX_ITEM 512
824/* valid flags in a format specification */ 687/* valid flags in a format specification */
@@ -835,20 +698,25 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
835 const char *s = luaL_checklstring(L, arg, &l); 698 const char *s = luaL_checklstring(L, arg, &l);
836 luaL_addchar(b, '"'); 699 luaL_addchar(b, '"');
837 while (l--) { 700 while (l--) {
838 if (*s == '"' || *s == '\\' || *s == '\n') { 701 switch (*s) {
839 luaL_addchar(b, '\\'); 702 case '"': case '\\': case '\n': {
840 luaL_addchar(b, *s); 703 luaL_addchar(b, '\\');
841 } 704 luaL_addchar(b, *s);
842 else if (*s == '\0' || iscntrl(uchar(*s))) { 705 break;
843 char buff[10]; 706 }
844 if (!isdigit(uchar(*(s+1)))) 707 case '\r': {
845 snprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); 708 luaL_addlstring(b, "\\r", 2);
846 else 709 break;
847 snprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); 710 }
848 luaL_addstring(b, buff); 711 case '\0': {
712 luaL_addlstring(b, "\\000", 4);
713 break;
714 }
715 default: {
716 luaL_addchar(b, *s);
717 break;
718 }
849 } 719 }
850 else
851 luaL_addchar(b, *s);
852 s++; 720 s++;
853 } 721 }
854 luaL_addchar(b, '"'); 722 luaL_addchar(b, '"');
@@ -857,7 +725,7 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
857static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { 725static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
858 const char *p = strfrmt; 726 const char *p = strfrmt;
859 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ 727 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
860 if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char)) 728 if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
861 luaL_error(L, "invalid format (repeated flags)"); 729 luaL_error(L, "invalid format (repeated flags)");
862 if (isdigit(uchar(*p))) p++; /* skip width */ 730 if (isdigit(uchar(*p))) p++; /* skip width */
863 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ 731 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
@@ -869,28 +737,23 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
869 if (isdigit(uchar(*p))) 737 if (isdigit(uchar(*p)))
870 luaL_error(L, "invalid format (width or precision too long)"); 738 luaL_error(L, "invalid format (width or precision too long)");
871 *(form++) = '%'; 739 *(form++) = '%';
872 memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char)); 740 strncpy(form, strfrmt, p - strfrmt + 1);
873 form += p - strfrmt + 1; 741 form += p - strfrmt + 1;
874 *form = '\0'; 742 *form = '\0';
875 return p; 743 return p;
876} 744}
877 745
878 746
879/* 747static void addintlen (char *form) {
880** add length modifier into formats
881*/
882static void addlenmod (char *form, const char *lenmod) {
883 size_t l = strlen(form); 748 size_t l = strlen(form);
884 size_t lm = strlen(lenmod);
885 char spec = form[l - 1]; 749 char spec = form[l - 1];
886 strcpy(form + l - 1, lenmod); 750 strcpy(form + l - 1, LUA_INTFRMLEN);
887 form[l + lm - 1] = spec; 751 form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
888 form[l + lm] = '\0'; 752 form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
889} 753}
890 754
891 755
892static int str_format (lua_State *L) { 756static int str_format (lua_State *L) {
893 int top = lua_gettop(L);
894 int arg = 1; 757 int arg = 1;
895 size_t sfl; 758 size_t sfl;
896 const char *strfrmt = luaL_checklstring(L, arg, &sfl); 759 const char *strfrmt = luaL_checklstring(L, arg, &sfl);
@@ -904,61 +767,45 @@ static int str_format (lua_State *L) {
904 luaL_addchar(&b, *strfrmt++); /* %% */ 767 luaL_addchar(&b, *strfrmt++); /* %% */
905 else { /* format item */ 768 else { /* format item */
906 char form[MAX_FORMAT]; /* to store the format (`%...') */ 769 char form[MAX_FORMAT]; /* to store the format (`%...') */
907 char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ 770 char buff[MAX_ITEM]; /* to store the formatted item */
908 int nb = 0; /* number of bytes in added item */ 771 arg++;
909 if (++arg > top)
910 luaL_argerror(L, arg, "no value");
911 strfrmt = scanformat(L, strfrmt, form); 772 strfrmt = scanformat(L, strfrmt, form);
912 switch (*strfrmt++) { 773 switch (*strfrmt++) {
913 case 'c': { 774 case 'c': {
914 nb = snprintf(buff, MAX_ITEM, form, luaL_checkint(L, arg)); 775 snprintf(buff, MAX_ITEM, form, (int)luaL_checknumber(L, arg));
915 break; 776 break;
916 } 777 }
917 case 'd': case 'i': { 778 case 'd': case 'i': {
918 lua_Number n = luaL_checknumber(L, arg); 779 addintlen(form);
919 LUA_INTFRM_T ni = (LUA_INTFRM_T)n; 780 snprintf(buff, MAX_ITEM, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
920 lua_Number diff = n - (lua_Number)ni;
921 luaL_argcheck(L, -1 < diff && diff < 1, arg,
922 "not a number in proper range");
923 addlenmod(form, LUA_INTFRMLEN);
924 nb = snprintf(buff, MAX_ITEM, form, ni);
925 break; 781 break;
926 } 782 }
927 case 'o': case 'u': case 'x': case 'X': { 783 case 'o': case 'u': case 'x': case 'X': {
928 lua_Number n = luaL_checknumber(L, arg); 784 addintlen(form);
929 unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n; 785 snprintf(buff, MAX_ITEM, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
930 lua_Number diff = n - (lua_Number)ni;
931 luaL_argcheck(L, -1 < diff && diff < 1, arg,
932 "not a non-negative number in proper range");
933 addlenmod(form, LUA_INTFRMLEN);
934 nb = snprintf(buff, MAX_ITEM, form, ni);
935 break; 786 break;
936 } 787 }
937 case 'e': case 'E': case 'f': 788 case 'e': case 'E': case 'f':
938#if defined(LUA_USE_AFORMAT)
939 case 'a': case 'A':
940#endif
941 case 'g': case 'G': { 789 case 'g': case 'G': {
942 addlenmod(form, LUA_FLTFRMLEN); 790 snprintf(buff, MAX_ITEM, form, (double)luaL_checknumber(L, arg));
943 nb = snprintf(buff, MAX_ITEM, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
944 break; 791 break;
945 } 792 }
946 case 'q': { 793 case 'q': {
947 addquoted(L, &b, arg); 794 addquoted(L, &b, arg);
948 break; 795 continue; /* skip the 'addsize' at the end */
949 } 796 }
950 case 's': { 797 case 's': {
951 size_t l; 798 size_t l;
952 const char *s = luaL_tolstring(L, arg, &l); 799 const char *s = luaL_checklstring(L, arg, &l);
953 if (!strchr(form, '.') && l >= 100) { 800 if (!strchr(form, '.') && l >= 100) {
954 /* no precision and string is too long to be formatted; 801 /* no precision and string is too long to be formatted;
955 keep original string */ 802 keep original string */
803 lua_pushvalue(L, arg);
956 luaL_addvalue(&b); 804 luaL_addvalue(&b);
957 break; 805 continue; /* skip the `addsize' at the end */
958 } 806 }
959 else { 807 else {
960 nb = snprintf(buff, MAX_ITEM, form, s); 808 snprintf(buff, MAX_ITEM, form, s);
961 lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
962 break; 809 break;
963 } 810 }
964 } 811 }
@@ -967,15 +814,13 @@ static int str_format (lua_State *L) {
967 LUA_QL("format"), *(strfrmt - 1)); 814 LUA_QL("format"), *(strfrmt - 1));
968 } 815 }
969 } 816 }
970 luaL_addsize(&b, nb); 817 luaL_addlstring(&b, buff, strlen(buff));
971 } 818 }
972 } 819 }
973 luaL_pushresult(&b); 820 luaL_pushresult(&b);
974 return 1; 821 return 1;
975} 822}
976 823
977/* }====================================================== */
978
979 824
980static const luaL_Reg strlib[] = { 825static const luaL_Reg strlib[] = {
981 {"byte", str_byte}, 826 {"byte", str_byte},
@@ -983,6 +828,7 @@ static const luaL_Reg strlib[] = {
983 {"dump", str_dump}, 828 {"dump", str_dump},
984 {"find", str_find}, 829 {"find", str_find},
985 {"format", str_format}, 830 {"format", str_format},
831 {"gfind", gfind_nodef},
986 {"gmatch", gmatch}, 832 {"gmatch", gmatch},
987 {"gsub", str_gsub}, 833 {"gsub", str_gsub},
988 {"len", str_len}, 834 {"len", str_len},
@@ -997,13 +843,13 @@ static const luaL_Reg strlib[] = {
997 843
998 844
999static void createmetatable (lua_State *L) { 845static void createmetatable (lua_State *L) {
1000 lua_createtable(L, 0, 1); /* table to be metatable for strings */ 846 lua_createtable(L, 0, 1); /* create metatable for strings */
1001 lua_pushliteral(L, ""); /* dummy string */ 847 lua_pushliteral(L, ""); /* dummy string */
1002 lua_pushvalue(L, -2); /* copy table */ 848 lua_pushvalue(L, -2);
1003 lua_setmetatable(L, -2); /* set table as metatable for strings */ 849 lua_setmetatable(L, -2); /* set string metatable */
1004 lua_pop(L, 1); /* pop dummy string */ 850 lua_pop(L, 1); /* pop dummy string */
1005 lua_pushvalue(L, -2); /* get string library */ 851 lua_pushvalue(L, -2); /* string library... */
1006 lua_setfield(L, -2, "__index"); /* metatable.__index = string */ 852 lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
1007 lua_pop(L, 1); /* pop metatable */ 853 lua_pop(L, 1); /* pop metatable */
1008} 854}
1009 855
@@ -1011,8 +857,12 @@ static void createmetatable (lua_State *L) {
1011/* 857/*
1012** Open string library 858** Open string library
1013*/ 859*/
1014LUAMOD_API int luaopen_string (lua_State *L) { 860LUALIB_API int luaopen_string (lua_State *L) {
1015 luaL_newlib(L, strlib); 861 luaL_register(L, LUA_STRLIBNAME, strlib);
862#if defined(LUA_COMPAT_GFIND)
863 lua_getfield(L, -1, "gmatch");
864 lua_setfield(L, -2, "gfind");
865#endif
1016 createmetatable(L); 866 createmetatable(L);
1017 return 1; 867 return 1;
1018} 868}