annotate src/lua/lstrlib.c @ 296:659a9c84c785

restored deposit-one-item, working on getting rival's name into item computer.
author Robert McIntyre <rlm@mit.edu>
date Fri, 30 Mar 2012 18:41:39 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $
rlm@1 3 ** Standard library for string operations and pattern-matching
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <ctype.h>
rlm@1 9 #include <stddef.h>
rlm@1 10 #include <stdio.h>
rlm@1 11 #include <stdlib.h>
rlm@1 12 #include <string.h>
rlm@1 13
rlm@1 14 #define lstrlib_c
rlm@1 15 #define LUA_LIB
rlm@1 16
rlm@1 17 #include "lua.h"
rlm@1 18
rlm@1 19 #include "lauxlib.h"
rlm@1 20 #include "lualib.h"
rlm@1 21
rlm@1 22
rlm@1 23 /* macro to `unsign' a character */
rlm@1 24 #define uchar(c) ((unsigned char)(c))
rlm@1 25
rlm@1 26
rlm@1 27
rlm@1 28 static int str_len (lua_State *L) {
rlm@1 29 size_t l;
rlm@1 30 luaL_checklstring(L, 1, &l);
rlm@1 31 lua_pushinteger(L, l);
rlm@1 32 return 1;
rlm@1 33 }
rlm@1 34
rlm@1 35
rlm@1 36 static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
rlm@1 37 /* relative string position: negative means back from end */
rlm@1 38 if (pos < 0) pos += (ptrdiff_t)len + 1;
rlm@1 39 return (pos >= 0) ? pos : 0;
rlm@1 40 }
rlm@1 41
rlm@1 42
rlm@1 43 static int str_sub (lua_State *L) {
rlm@1 44 size_t l;
rlm@1 45 const char *s = luaL_checklstring(L, 1, &l);
rlm@1 46 ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
rlm@1 47 ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
rlm@1 48 if (start < 1) start = 1;
rlm@1 49 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
rlm@1 50 if (start <= end)
rlm@1 51 lua_pushlstring(L, s+start-1, end-start+1);
rlm@1 52 else lua_pushliteral(L, "");
rlm@1 53 return 1;
rlm@1 54 }
rlm@1 55
rlm@1 56
rlm@1 57 static int str_reverse (lua_State *L) {
rlm@1 58 size_t l;
rlm@1 59 luaL_Buffer b;
rlm@1 60 const char *s = luaL_checklstring(L, 1, &l);
rlm@1 61 luaL_buffinit(L, &b);
rlm@1 62 while (l--) luaL_addchar(&b, s[l]);
rlm@1 63 luaL_pushresult(&b);
rlm@1 64 return 1;
rlm@1 65 }
rlm@1 66
rlm@1 67
rlm@1 68 static int str_lower (lua_State *L) {
rlm@1 69 size_t l;
rlm@1 70 size_t i;
rlm@1 71 luaL_Buffer b;
rlm@1 72 const char *s = luaL_checklstring(L, 1, &l);
rlm@1 73 luaL_buffinit(L, &b);
rlm@1 74 for (i=0; i<l; i++)
rlm@1 75 luaL_addchar(&b, tolower(uchar(s[i])));
rlm@1 76 luaL_pushresult(&b);
rlm@1 77 return 1;
rlm@1 78 }
rlm@1 79
rlm@1 80
rlm@1 81 static int str_upper (lua_State *L) {
rlm@1 82 size_t l;
rlm@1 83 size_t i;
rlm@1 84 luaL_Buffer b;
rlm@1 85 const char *s = luaL_checklstring(L, 1, &l);
rlm@1 86 luaL_buffinit(L, &b);
rlm@1 87 for (i=0; i<l; i++)
rlm@1 88 luaL_addchar(&b, toupper(uchar(s[i])));
rlm@1 89 luaL_pushresult(&b);
rlm@1 90 return 1;
rlm@1 91 }
rlm@1 92
rlm@1 93 static int str_rep (lua_State *L) {
rlm@1 94 size_t l;
rlm@1 95 luaL_Buffer b;
rlm@1 96 const char *s = luaL_checklstring(L, 1, &l);
rlm@1 97 int n = luaL_checkint(L, 2);
rlm@1 98 luaL_buffinit(L, &b);
rlm@1 99 while (n-- > 0)
rlm@1 100 luaL_addlstring(&b, s, l);
rlm@1 101 luaL_pushresult(&b);
rlm@1 102 return 1;
rlm@1 103 }
rlm@1 104
rlm@1 105
rlm@1 106 static int str_byte (lua_State *L) {
rlm@1 107 size_t l;
rlm@1 108 const char *s = luaL_checklstring(L, 1, &l);
rlm@1 109 ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
rlm@1 110 ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
rlm@1 111 int n, i;
rlm@1 112 if (posi <= 0) posi = 1;
rlm@1 113 if ((size_t)pose > l) pose = l;
rlm@1 114 if (posi > pose) return 0; /* empty interval; return no values */
rlm@1 115 n = (int)(pose - posi + 1);
rlm@1 116 if (posi + n <= pose) /* overflow? */
rlm@1 117 luaL_error(L, "string slice too long");
rlm@1 118 luaL_checkstack(L, n, "string slice too long");
rlm@1 119 for (i=0; i<n; i++)
rlm@1 120 lua_pushinteger(L, uchar(s[posi+i-1]));
rlm@1 121 return n;
rlm@1 122 }
rlm@1 123
rlm@1 124
rlm@1 125 static int str_char (lua_State *L) {
rlm@1 126 int n = lua_gettop(L); /* number of arguments */
rlm@1 127 int i;
rlm@1 128 luaL_Buffer b;
rlm@1 129 luaL_buffinit(L, &b);
rlm@1 130 for (i=1; i<=n; i++) {
rlm@1 131 int c = luaL_checkint(L, i);
rlm@1 132 luaL_argcheck(L, uchar(c) == c, i, "invalid value");
rlm@1 133 luaL_addchar(&b, uchar(c));
rlm@1 134 }
rlm@1 135 luaL_pushresult(&b);
rlm@1 136 return 1;
rlm@1 137 }
rlm@1 138
rlm@1 139
rlm@1 140 static int writer (lua_State *L, const void* b, size_t size, void* B) {
rlm@1 141 (void)L;
rlm@1 142 luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
rlm@1 143 return 0;
rlm@1 144 }
rlm@1 145
rlm@1 146
rlm@1 147 static int str_dump (lua_State *L) {
rlm@1 148 luaL_Buffer b;
rlm@1 149 luaL_checktype(L, 1, LUA_TFUNCTION);
rlm@1 150 lua_settop(L, 1);
rlm@1 151 luaL_buffinit(L,&b);
rlm@1 152 if (lua_dump(L, writer, &b) != 0)
rlm@1 153 luaL_error(L, "unable to dump given function");
rlm@1 154 luaL_pushresult(&b);
rlm@1 155 return 1;
rlm@1 156 }
rlm@1 157
rlm@1 158
rlm@1 159
rlm@1 160 /*
rlm@1 161 ** {======================================================
rlm@1 162 ** PATTERN MATCHING
rlm@1 163 ** =======================================================
rlm@1 164 */
rlm@1 165
rlm@1 166
rlm@1 167 #define CAP_UNFINISHED (-1)
rlm@1 168 #define CAP_POSITION (-2)
rlm@1 169
rlm@1 170 typedef struct MatchState {
rlm@1 171 const char *src_init; /* init of source string */
rlm@1 172 const char *src_end; /* end (`\0') of source string */
rlm@1 173 lua_State *L;
rlm@1 174 int level; /* total number of captures (finished or unfinished) */
rlm@1 175 struct {
rlm@1 176 const char *init;
rlm@1 177 ptrdiff_t len;
rlm@1 178 } capture[LUA_MAXCAPTURES];
rlm@1 179 } MatchState;
rlm@1 180
rlm@1 181
rlm@1 182 #define L_ESC '%'
rlm@1 183 #define SPECIALS "^$*+?.([%-"
rlm@1 184
rlm@1 185
rlm@1 186 static int check_capture (MatchState *ms, int l) {
rlm@1 187 l -= '1';
rlm@1 188 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
rlm@1 189 return luaL_error(ms->L, "invalid capture index");
rlm@1 190 return l;
rlm@1 191 }
rlm@1 192
rlm@1 193
rlm@1 194 static int capture_to_close (MatchState *ms) {
rlm@1 195 int level = ms->level;
rlm@1 196 for (level--; level>=0; level--)
rlm@1 197 if (ms->capture[level].len == CAP_UNFINISHED) return level;
rlm@1 198 return luaL_error(ms->L, "invalid pattern capture");
rlm@1 199 }
rlm@1 200
rlm@1 201
rlm@1 202 static const char *classend (MatchState *ms, const char *p) {
rlm@1 203 switch (*p++) {
rlm@1 204 case L_ESC: {
rlm@1 205 if (*p == '\0')
rlm@1 206 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
rlm@1 207 return p+1;
rlm@1 208 }
rlm@1 209 case '[': {
rlm@1 210 if (*p == '^') p++;
rlm@1 211 do { /* look for a `]' */
rlm@1 212 if (*p == '\0')
rlm@1 213 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
rlm@1 214 if (*(p++) == L_ESC && *p != '\0')
rlm@1 215 p++; /* skip escapes (e.g. `%]') */
rlm@1 216 } while (*p != ']');
rlm@1 217 return p+1;
rlm@1 218 }
rlm@1 219 default: {
rlm@1 220 return p;
rlm@1 221 }
rlm@1 222 }
rlm@1 223 }
rlm@1 224
rlm@1 225
rlm@1 226 static int match_class (int c, int cl) {
rlm@1 227 int res;
rlm@1 228 switch (tolower(cl)) {
rlm@1 229 case 'a' : res = isalpha(c); break;
rlm@1 230 case 'c' : res = iscntrl(c); break;
rlm@1 231 case 'd' : res = isdigit(c); break;
rlm@1 232 case 'l' : res = islower(c); break;
rlm@1 233 case 'p' : res = ispunct(c); break;
rlm@1 234 case 's' : res = isspace(c); break;
rlm@1 235 case 'u' : res = isupper(c); break;
rlm@1 236 case 'w' : res = isalnum(c); break;
rlm@1 237 case 'x' : res = isxdigit(c); break;
rlm@1 238 case 'z' : res = (c == 0); break;
rlm@1 239 default: return (cl == c);
rlm@1 240 }
rlm@1 241 return (islower(cl) ? res : !res);
rlm@1 242 }
rlm@1 243
rlm@1 244
rlm@1 245 static int matchbracketclass (int c, const char *p, const char *ec) {
rlm@1 246 int sig = 1;
rlm@1 247 if (*(p+1) == '^') {
rlm@1 248 sig = 0;
rlm@1 249 p++; /* skip the `^' */
rlm@1 250 }
rlm@1 251 while (++p < ec) {
rlm@1 252 if (*p == L_ESC) {
rlm@1 253 p++;
rlm@1 254 if (match_class(c, uchar(*p)))
rlm@1 255 return sig;
rlm@1 256 }
rlm@1 257 else if ((*(p+1) == '-') && (p+2 < ec)) {
rlm@1 258 p+=2;
rlm@1 259 if (uchar(*(p-2)) <= c && c <= uchar(*p))
rlm@1 260 return sig;
rlm@1 261 }
rlm@1 262 else if (uchar(*p) == c) return sig;
rlm@1 263 }
rlm@1 264 return !sig;
rlm@1 265 }
rlm@1 266
rlm@1 267
rlm@1 268 static int singlematch (int c, const char *p, const char *ep) {
rlm@1 269 switch (*p) {
rlm@1 270 case '.': return 1; /* matches any char */
rlm@1 271 case L_ESC: return match_class(c, uchar(*(p+1)));
rlm@1 272 case '[': return matchbracketclass(c, p, ep-1);
rlm@1 273 default: return (uchar(*p) == c);
rlm@1 274 }
rlm@1 275 }
rlm@1 276
rlm@1 277
rlm@1 278 static const char *match (MatchState *ms, const char *s, const char *p);
rlm@1 279
rlm@1 280
rlm@1 281 static const char *matchbalance (MatchState *ms, const char *s,
rlm@1 282 const char *p) {
rlm@1 283 if (*p == 0 || *(p+1) == 0)
rlm@1 284 luaL_error(ms->L, "unbalanced pattern");
rlm@1 285 if (*s != *p) return NULL;
rlm@1 286 else {
rlm@1 287 int b = *p;
rlm@1 288 int e = *(p+1);
rlm@1 289 int cont = 1;
rlm@1 290 while (++s < ms->src_end) {
rlm@1 291 if (*s == e) {
rlm@1 292 if (--cont == 0) return s+1;
rlm@1 293 }
rlm@1 294 else if (*s == b) cont++;
rlm@1 295 }
rlm@1 296 }
rlm@1 297 return NULL; /* string ends out of balance */
rlm@1 298 }
rlm@1 299
rlm@1 300
rlm@1 301 static const char *max_expand (MatchState *ms, const char *s,
rlm@1 302 const char *p, const char *ep) {
rlm@1 303 ptrdiff_t i = 0; /* counts maximum expand for item */
rlm@1 304 while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
rlm@1 305 i++;
rlm@1 306 /* keeps trying to match with the maximum repetitions */
rlm@1 307 while (i>=0) {
rlm@1 308 const char *res = match(ms, (s+i), ep+1);
rlm@1 309 if (res) return res;
rlm@1 310 i--; /* else didn't match; reduce 1 repetition to try again */
rlm@1 311 }
rlm@1 312 return NULL;
rlm@1 313 }
rlm@1 314
rlm@1 315
rlm@1 316 static const char *min_expand (MatchState *ms, const char *s,
rlm@1 317 const char *p, const char *ep) {
rlm@1 318 for (;;) {
rlm@1 319 const char *res = match(ms, s, ep+1);
rlm@1 320 if (res != NULL)
rlm@1 321 return res;
rlm@1 322 else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
rlm@1 323 s++; /* try with one more repetition */
rlm@1 324 else return NULL;
rlm@1 325 }
rlm@1 326 }
rlm@1 327
rlm@1 328
rlm@1 329 static const char *start_capture (MatchState *ms, const char *s,
rlm@1 330 const char *p, int what) {
rlm@1 331 const char *res;
rlm@1 332 int level = ms->level;
rlm@1 333 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
rlm@1 334 ms->capture[level].init = s;
rlm@1 335 ms->capture[level].len = what;
rlm@1 336 ms->level = level+1;
rlm@1 337 if ((res=match(ms, s, p)) == NULL) /* match failed? */
rlm@1 338 ms->level--; /* undo capture */
rlm@1 339 return res;
rlm@1 340 }
rlm@1 341
rlm@1 342
rlm@1 343 static const char *end_capture (MatchState *ms, const char *s,
rlm@1 344 const char *p) {
rlm@1 345 int l = capture_to_close(ms);
rlm@1 346 const char *res;
rlm@1 347 ms->capture[l].len = s - ms->capture[l].init; /* close capture */
rlm@1 348 if ((res = match(ms, s, p)) == NULL) /* match failed? */
rlm@1 349 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
rlm@1 350 return res;
rlm@1 351 }
rlm@1 352
rlm@1 353
rlm@1 354 static const char *match_capture (MatchState *ms, const char *s, int l) {
rlm@1 355 size_t len;
rlm@1 356 l = check_capture(ms, l);
rlm@1 357 len = ms->capture[l].len;
rlm@1 358 if ((size_t)(ms->src_end-s) >= len &&
rlm@1 359 memcmp(ms->capture[l].init, s, len) == 0)
rlm@1 360 return s+len;
rlm@1 361 else return NULL;
rlm@1 362 }
rlm@1 363
rlm@1 364
rlm@1 365 static const char *match (MatchState *ms, const char *s, const char *p) {
rlm@1 366 init: /* using goto's to optimize tail recursion */
rlm@1 367 switch (*p) {
rlm@1 368 case '(': { /* start capture */
rlm@1 369 if (*(p+1) == ')') /* position capture? */
rlm@1 370 return start_capture(ms, s, p+2, CAP_POSITION);
rlm@1 371 else
rlm@1 372 return start_capture(ms, s, p+1, CAP_UNFINISHED);
rlm@1 373 }
rlm@1 374 case ')': { /* end capture */
rlm@1 375 return end_capture(ms, s, p+1);
rlm@1 376 }
rlm@1 377 case L_ESC: {
rlm@1 378 switch (*(p+1)) {
rlm@1 379 case 'b': { /* balanced string? */
rlm@1 380 s = matchbalance(ms, s, p+2);
rlm@1 381 if (s == NULL) return NULL;
rlm@1 382 p+=4; goto init; /* else return match(ms, s, p+4); */
rlm@1 383 }
rlm@1 384 case 'f': { /* frontier? */
rlm@1 385 const char *ep; char previous;
rlm@1 386 p += 2;
rlm@1 387 if (*p != '[')
rlm@1 388 luaL_error(ms->L, "missing " LUA_QL("[") " after "
rlm@1 389 LUA_QL("%%f") " in pattern");
rlm@1 390 ep = classend(ms, p); /* points to what is next */
rlm@1 391 previous = (s == ms->src_init) ? '\0' : *(s-1);
rlm@1 392 if (matchbracketclass(uchar(previous), p, ep-1) ||
rlm@1 393 !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
rlm@1 394 p=ep; goto init; /* else return match(ms, s, ep); */
rlm@1 395 }
rlm@1 396 default: {
rlm@1 397 if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
rlm@1 398 s = match_capture(ms, s, uchar(*(p+1)));
rlm@1 399 if (s == NULL) return NULL;
rlm@1 400 p+=2; goto init; /* else return match(ms, s, p+2) */
rlm@1 401 }
rlm@1 402 goto dflt; /* case default */
rlm@1 403 }
rlm@1 404 }
rlm@1 405 }
rlm@1 406 case '\0': { /* end of pattern */
rlm@1 407 return s; /* match succeeded */
rlm@1 408 }
rlm@1 409 case '$': {
rlm@1 410 if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
rlm@1 411 return (s == ms->src_end) ? s : NULL; /* check end of string */
rlm@1 412 else goto dflt;
rlm@1 413 }
rlm@1 414 default: dflt: { /* it is a pattern item */
rlm@1 415 const char *ep = classend(ms, p); /* points to what is next */
rlm@1 416 int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
rlm@1 417 switch (*ep) {
rlm@1 418 case '?': { /* optional */
rlm@1 419 const char *res;
rlm@1 420 if (m && ((res=match(ms, s+1, ep+1)) != NULL))
rlm@1 421 return res;
rlm@1 422 p=ep+1; goto init; /* else return match(ms, s, ep+1); */
rlm@1 423 }
rlm@1 424 case '*': { /* 0 or more repetitions */
rlm@1 425 return max_expand(ms, s, p, ep);
rlm@1 426 }
rlm@1 427 case '+': { /* 1 or more repetitions */
rlm@1 428 return (m ? max_expand(ms, s+1, p, ep) : NULL);
rlm@1 429 }
rlm@1 430 case '-': { /* 0 or more repetitions (minimum) */
rlm@1 431 return min_expand(ms, s, p, ep);
rlm@1 432 }
rlm@1 433 default: {
rlm@1 434 if (!m) return NULL;
rlm@1 435 s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
rlm@1 436 }
rlm@1 437 }
rlm@1 438 }
rlm@1 439 }
rlm@1 440 }
rlm@1 441
rlm@1 442
rlm@1 443
rlm@1 444 static const char *lmemfind (const char *s1, size_t l1,
rlm@1 445 const char *s2, size_t l2) {
rlm@1 446 if (l2 == 0) return s1; /* empty strings are everywhere */
rlm@1 447 else if (l2 > l1) return NULL; /* avoids a negative `l1' */
rlm@1 448 else {
rlm@1 449 const char *init; /* to search for a `*s2' inside `s1' */
rlm@1 450 l2--; /* 1st char will be checked by `memchr' */
rlm@1 451 l1 = l1-l2; /* `s2' cannot be found after that */
rlm@1 452 while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
rlm@1 453 init++; /* 1st char is already checked */
rlm@1 454 if (memcmp(init, s2+1, l2) == 0)
rlm@1 455 return init-1;
rlm@1 456 else { /* correct `l1' and `s1' to try again */
rlm@1 457 l1 -= init-s1;
rlm@1 458 s1 = init;
rlm@1 459 }
rlm@1 460 }
rlm@1 461 return NULL; /* not found */
rlm@1 462 }
rlm@1 463 }
rlm@1 464
rlm@1 465
rlm@1 466 static void push_onecapture (MatchState *ms, int i, const char *s,
rlm@1 467 const char *e) {
rlm@1 468 if (i >= ms->level) {
rlm@1 469 if (i == 0) /* ms->level == 0, too */
rlm@1 470 lua_pushlstring(ms->L, s, e - s); /* add whole match */
rlm@1 471 else
rlm@1 472 luaL_error(ms->L, "invalid capture index");
rlm@1 473 }
rlm@1 474 else {
rlm@1 475 ptrdiff_t l = ms->capture[i].len;
rlm@1 476 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
rlm@1 477 if (l == CAP_POSITION)
rlm@1 478 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
rlm@1 479 else
rlm@1 480 lua_pushlstring(ms->L, ms->capture[i].init, l);
rlm@1 481 }
rlm@1 482 }
rlm@1 483
rlm@1 484
rlm@1 485 static int push_captures (MatchState *ms, const char *s, const char *e) {
rlm@1 486 int i;
rlm@1 487 int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
rlm@1 488 luaL_checkstack(ms->L, nlevels, "too many captures");
rlm@1 489 for (i = 0; i < nlevels; i++)
rlm@1 490 push_onecapture(ms, i, s, e);
rlm@1 491 return nlevels; /* number of strings pushed */
rlm@1 492 }
rlm@1 493
rlm@1 494
rlm@1 495 static int str_find_aux (lua_State *L, int find) {
rlm@1 496 size_t l1, l2;
rlm@1 497 const char *s = luaL_checklstring(L, 1, &l1);
rlm@1 498 const char *p = luaL_checklstring(L, 2, &l2);
rlm@1 499 ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
rlm@1 500 if (init < 0) init = 0;
rlm@1 501 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
rlm@1 502 if (find && (lua_toboolean(L, 4) || /* explicit request? */
rlm@1 503 strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
rlm@1 504 /* do a plain search */
rlm@1 505 const char *s2 = lmemfind(s+init, l1-init, p, l2);
rlm@1 506 if (s2) {
rlm@1 507 lua_pushinteger(L, s2-s+1);
rlm@1 508 lua_pushinteger(L, s2-s+l2);
rlm@1 509 return 2;
rlm@1 510 }
rlm@1 511 }
rlm@1 512 else {
rlm@1 513 MatchState ms;
rlm@1 514 int anchor = (*p == '^') ? (p++, 1) : 0;
rlm@1 515 const char *s1=s+init;
rlm@1 516 ms.L = L;
rlm@1 517 ms.src_init = s;
rlm@1 518 ms.src_end = s+l1;
rlm@1 519 do {
rlm@1 520 const char *res;
rlm@1 521 ms.level = 0;
rlm@1 522 if ((res=match(&ms, s1, p)) != NULL) {
rlm@1 523 if (find) {
rlm@1 524 lua_pushinteger(L, s1-s+1); /* start */
rlm@1 525 lua_pushinteger(L, res-s); /* end */
rlm@1 526 return push_captures(&ms, NULL, 0) + 2;
rlm@1 527 }
rlm@1 528 else
rlm@1 529 return push_captures(&ms, s1, res);
rlm@1 530 }
rlm@1 531 } while (s1++ < ms.src_end && !anchor);
rlm@1 532 }
rlm@1 533 lua_pushnil(L); /* not found */
rlm@1 534 return 1;
rlm@1 535 }
rlm@1 536
rlm@1 537
rlm@1 538 static int str_find (lua_State *L) {
rlm@1 539 return str_find_aux(L, 1);
rlm@1 540 }
rlm@1 541
rlm@1 542
rlm@1 543 static int str_match (lua_State *L) {
rlm@1 544 return str_find_aux(L, 0);
rlm@1 545 }
rlm@1 546
rlm@1 547
rlm@1 548 static int gmatch_aux (lua_State *L) {
rlm@1 549 MatchState ms;
rlm@1 550 size_t ls;
rlm@1 551 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
rlm@1 552 const char *p = lua_tostring(L, lua_upvalueindex(2));
rlm@1 553 const char *src;
rlm@1 554 ms.L = L;
rlm@1 555 ms.src_init = s;
rlm@1 556 ms.src_end = s+ls;
rlm@1 557 for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
rlm@1 558 src <= ms.src_end;
rlm@1 559 src++) {
rlm@1 560 const char *e;
rlm@1 561 ms.level = 0;
rlm@1 562 if ((e = match(&ms, src, p)) != NULL) {
rlm@1 563 lua_Integer newstart = e-s;
rlm@1 564 if (e == src) newstart++; /* empty match? go at least one position */
rlm@1 565 lua_pushinteger(L, newstart);
rlm@1 566 lua_replace(L, lua_upvalueindex(3));
rlm@1 567 return push_captures(&ms, src, e);
rlm@1 568 }
rlm@1 569 }
rlm@1 570 return 0; /* not found */
rlm@1 571 }
rlm@1 572
rlm@1 573
rlm@1 574 static int gmatch (lua_State *L) {
rlm@1 575 luaL_checkstring(L, 1);
rlm@1 576 luaL_checkstring(L, 2);
rlm@1 577 lua_settop(L, 2);
rlm@1 578 lua_pushinteger(L, 0);
rlm@1 579 lua_pushcclosure(L, gmatch_aux, 3);
rlm@1 580 return 1;
rlm@1 581 }
rlm@1 582
rlm@1 583
rlm@1 584 static int gfind_nodef (lua_State *L) {
rlm@1 585 return luaL_error(L, LUA_QL("string.gfind") " was renamed to "
rlm@1 586 LUA_QL("string.gmatch"));
rlm@1 587 }
rlm@1 588
rlm@1 589
rlm@1 590 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
rlm@1 591 const char *e) {
rlm@1 592 size_t l, i;
rlm@1 593 const char *news = lua_tolstring(ms->L, 3, &l);
rlm@1 594 for (i = 0; i < l; i++) {
rlm@1 595 if (news[i] != L_ESC)
rlm@1 596 luaL_addchar(b, news[i]);
rlm@1 597 else {
rlm@1 598 i++; /* skip ESC */
rlm@1 599 if (!isdigit(uchar(news[i])))
rlm@1 600 luaL_addchar(b, news[i]);
rlm@1 601 else if (news[i] == '0')
rlm@1 602 luaL_addlstring(b, s, e - s);
rlm@1 603 else {
rlm@1 604 push_onecapture(ms, news[i] - '1', s, e);
rlm@1 605 luaL_addvalue(b); /* add capture to accumulated result */
rlm@1 606 }
rlm@1 607 }
rlm@1 608 }
rlm@1 609 }
rlm@1 610
rlm@1 611
rlm@1 612 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
rlm@1 613 const char *e) {
rlm@1 614 lua_State *L = ms->L;
rlm@1 615 switch (lua_type(L, 3)) {
rlm@1 616 case LUA_TNUMBER:
rlm@1 617 case LUA_TSTRING: {
rlm@1 618 add_s(ms, b, s, e);
rlm@1 619 return;
rlm@1 620 }
rlm@1 621 case LUA_TFUNCTION: {
rlm@1 622 int n;
rlm@1 623 lua_pushvalue(L, 3);
rlm@1 624 n = push_captures(ms, s, e);
rlm@1 625 lua_call(L, n, 1);
rlm@1 626 break;
rlm@1 627 }
rlm@1 628 case LUA_TTABLE: {
rlm@1 629 push_onecapture(ms, 0, s, e);
rlm@1 630 lua_gettable(L, 3);
rlm@1 631 break;
rlm@1 632 }
rlm@1 633 }
rlm@1 634 if (!lua_toboolean(L, -1)) { /* nil or false? */
rlm@1 635 lua_pop(L, 1);
rlm@1 636 lua_pushlstring(L, s, e - s); /* keep original text */
rlm@1 637 }
rlm@1 638 else if (!lua_isstring(L, -1))
rlm@1 639 luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
rlm@1 640 luaL_addvalue(b); /* add result to accumulator */
rlm@1 641 }
rlm@1 642
rlm@1 643
rlm@1 644 static int str_gsub (lua_State *L) {
rlm@1 645 size_t srcl;
rlm@1 646 const char *src = luaL_checklstring(L, 1, &srcl);
rlm@1 647 const char *p = luaL_checkstring(L, 2);
rlm@1 648 int tr = lua_type(L, 3);
rlm@1 649 int max_s = luaL_optint(L, 4, srcl+1);
rlm@1 650 int anchor = (*p == '^') ? (p++, 1) : 0;
rlm@1 651 int n = 0;
rlm@1 652 MatchState ms;
rlm@1 653 luaL_Buffer b;
rlm@1 654 luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
rlm@1 655 tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
rlm@1 656 "string/function/table expected");
rlm@1 657 luaL_buffinit(L, &b);
rlm@1 658 ms.L = L;
rlm@1 659 ms.src_init = src;
rlm@1 660 ms.src_end = src+srcl;
rlm@1 661 while (n < max_s) {
rlm@1 662 const char *e;
rlm@1 663 ms.level = 0;
rlm@1 664 e = match(&ms, src, p);
rlm@1 665 if (e) {
rlm@1 666 n++;
rlm@1 667 add_value(&ms, &b, src, e);
rlm@1 668 }
rlm@1 669 if (e && e>src) /* non empty match? */
rlm@1 670 src = e; /* skip it */
rlm@1 671 else if (src < ms.src_end)
rlm@1 672 luaL_addchar(&b, *src++);
rlm@1 673 else break;
rlm@1 674 if (anchor) break;
rlm@1 675 }
rlm@1 676 luaL_addlstring(&b, src, ms.src_end-src);
rlm@1 677 luaL_pushresult(&b);
rlm@1 678 lua_pushinteger(L, n); /* number of substitutions */
rlm@1 679 return 2;
rlm@1 680 }
rlm@1 681
rlm@1 682 /* }====================================================== */
rlm@1 683
rlm@1 684
rlm@1 685 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
rlm@1 686 #define MAX_ITEM 512
rlm@1 687 /* valid flags in a format specification */
rlm@1 688 #define FLAGS "-+ #0"
rlm@1 689 /*
rlm@1 690 ** maximum size of each format specification (such as '%-099.99d')
rlm@1 691 ** (+10 accounts for %99.99x plus margin of error)
rlm@1 692 */
rlm@1 693 #define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
rlm@1 694
rlm@1 695
rlm@1 696 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
rlm@1 697 size_t l;
rlm@1 698 const char *s = luaL_checklstring(L, arg, &l);
rlm@1 699 luaL_addchar(b, '"');
rlm@1 700 while (l--) {
rlm@1 701 switch (*s) {
rlm@1 702 case '"': case '\\': case '\n': {
rlm@1 703 luaL_addchar(b, '\\');
rlm@1 704 luaL_addchar(b, *s);
rlm@1 705 break;
rlm@1 706 }
rlm@1 707 case '\r': {
rlm@1 708 luaL_addlstring(b, "\\r", 2);
rlm@1 709 break;
rlm@1 710 }
rlm@1 711 case '\0': {
rlm@1 712 luaL_addlstring(b, "\\000", 4);
rlm@1 713 break;
rlm@1 714 }
rlm@1 715 default: {
rlm@1 716 luaL_addchar(b, *s);
rlm@1 717 break;
rlm@1 718 }
rlm@1 719 }
rlm@1 720 s++;
rlm@1 721 }
rlm@1 722 luaL_addchar(b, '"');
rlm@1 723 }
rlm@1 724
rlm@1 725 static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
rlm@1 726 const char *p = strfrmt;
rlm@1 727 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
rlm@1 728 if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
rlm@1 729 luaL_error(L, "invalid format (repeated flags)");
rlm@1 730 if (isdigit(uchar(*p))) p++; /* skip width */
rlm@1 731 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
rlm@1 732 if (*p == '.') {
rlm@1 733 p++;
rlm@1 734 if (isdigit(uchar(*p))) p++; /* skip precision */
rlm@1 735 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
rlm@1 736 }
rlm@1 737 if (isdigit(uchar(*p)))
rlm@1 738 luaL_error(L, "invalid format (width or precision too long)");
rlm@1 739 *(form++) = '%';
rlm@1 740 strncpy(form, strfrmt, p - strfrmt + 1);
rlm@1 741 form += p - strfrmt + 1;
rlm@1 742 *form = '\0';
rlm@1 743 return p;
rlm@1 744 }
rlm@1 745
rlm@1 746
rlm@1 747 static void addintlen (char *form) {
rlm@1 748 size_t l = strlen(form);
rlm@1 749 char spec = form[l - 1];
rlm@1 750 strcpy(form + l - 1, LUA_INTFRMLEN);
rlm@1 751 form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
rlm@1 752 form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
rlm@1 753 }
rlm@1 754
rlm@1 755
rlm@1 756 static int str_format (lua_State *L) {
rlm@1 757 int arg = 1;
rlm@1 758 size_t sfl;
rlm@1 759 const char *strfrmt = luaL_checklstring(L, arg, &sfl);
rlm@1 760 const char *strfrmt_end = strfrmt+sfl;
rlm@1 761 luaL_Buffer b;
rlm@1 762 luaL_buffinit(L, &b);
rlm@1 763 while (strfrmt < strfrmt_end) {
rlm@1 764 if (*strfrmt != L_ESC)
rlm@1 765 luaL_addchar(&b, *strfrmt++);
rlm@1 766 else if (*++strfrmt == L_ESC)
rlm@1 767 luaL_addchar(&b, *strfrmt++); /* %% */
rlm@1 768 else { /* format item */
rlm@1 769 char form[MAX_FORMAT]; /* to store the format (`%...') */
rlm@1 770 char buff[MAX_ITEM]; /* to store the formatted item */
rlm@1 771 arg++;
rlm@1 772 strfrmt = scanformat(L, strfrmt, form);
rlm@1 773 switch (*strfrmt++) {
rlm@1 774 case 'c': {
rlm@1 775 sprintf(buff, form, (int)luaL_checknumber(L, arg));
rlm@1 776 break;
rlm@1 777 }
rlm@1 778 case 'd': case 'i': {
rlm@1 779 addintlen(form);
rlm@1 780 sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
rlm@1 781 break;
rlm@1 782 }
rlm@1 783 case 'o': case 'u': case 'x': case 'X': {
rlm@1 784 addintlen(form);
rlm@1 785 sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
rlm@1 786 break;
rlm@1 787 }
rlm@1 788 case 'e': case 'E': case 'f':
rlm@1 789 case 'g': case 'G': {
rlm@1 790 sprintf(buff, form, (double)luaL_checknumber(L, arg));
rlm@1 791 break;
rlm@1 792 }
rlm@1 793 case 'q': {
rlm@1 794 addquoted(L, &b, arg);
rlm@1 795 continue; /* skip the 'addsize' at the end */
rlm@1 796 }
rlm@1 797 case 's': {
rlm@1 798 size_t l;
rlm@1 799 const char *s = luaL_checklstring(L, arg, &l);
rlm@1 800 if (!strchr(form, '.') && l >= 100) {
rlm@1 801 /* no precision and string is too long to be formatted;
rlm@1 802 keep original string */
rlm@1 803 lua_pushvalue(L, arg);
rlm@1 804 luaL_addvalue(&b);
rlm@1 805 continue; /* skip the `addsize' at the end */
rlm@1 806 }
rlm@1 807 else {
rlm@1 808 sprintf(buff, form, s);
rlm@1 809 break;
rlm@1 810 }
rlm@1 811 }
rlm@1 812 default: { /* also treat cases `pnLlh' */
rlm@1 813 return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
rlm@1 814 LUA_QL("format"), *(strfrmt - 1));
rlm@1 815 }
rlm@1 816 }
rlm@1 817 luaL_addlstring(&b, buff, strlen(buff));
rlm@1 818 }
rlm@1 819 }
rlm@1 820 luaL_pushresult(&b);
rlm@1 821 return 1;
rlm@1 822 }
rlm@1 823
rlm@1 824
rlm@1 825 static const luaL_Reg strlib[] = {
rlm@1 826 {"byte", str_byte},
rlm@1 827 {"char", str_char},
rlm@1 828 {"dump", str_dump},
rlm@1 829 {"find", str_find},
rlm@1 830 {"format", str_format},
rlm@1 831 {"gfind", gfind_nodef},
rlm@1 832 {"gmatch", gmatch},
rlm@1 833 {"gsub", str_gsub},
rlm@1 834 {"len", str_len},
rlm@1 835 {"lower", str_lower},
rlm@1 836 {"match", str_match},
rlm@1 837 {"rep", str_rep},
rlm@1 838 {"reverse", str_reverse},
rlm@1 839 {"sub", str_sub},
rlm@1 840 {"upper", str_upper},
rlm@1 841 {NULL, NULL}
rlm@1 842 };
rlm@1 843
rlm@1 844
rlm@1 845 static void createmetatable (lua_State *L) {
rlm@1 846 lua_createtable(L, 0, 1); /* create metatable for strings */
rlm@1 847 lua_pushliteral(L, ""); /* dummy string */
rlm@1 848 lua_pushvalue(L, -2);
rlm@1 849 lua_setmetatable(L, -2); /* set string metatable */
rlm@1 850 lua_pop(L, 1); /* pop dummy string */
rlm@1 851 lua_pushvalue(L, -2); /* string library... */
rlm@1 852 lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
rlm@1 853 lua_pop(L, 1); /* pop metatable */
rlm@1 854 }
rlm@1 855
rlm@1 856
rlm@1 857 /*
rlm@1 858 ** Open string library
rlm@1 859 */
rlm@1 860 LUALIB_API int luaopen_string (lua_State *L) {
rlm@1 861 luaL_register(L, LUA_STRLIBNAME, strlib);
rlm@1 862 #if defined(LUA_COMPAT_GFIND)
rlm@1 863 lua_getfield(L, -1, "gmatch");
rlm@1 864 lua_setfield(L, -2, "gfind");
rlm@1 865 #endif
rlm@1 866 createmetatable(L);
rlm@1 867 return 1;
rlm@1 868 }
rlm@1 869