diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-03-02 14:40:08 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-03-02 14:40:08 -0300 |
| commit | 9e8a46daa28e902b4a6d74134d96e80411789175 (patch) | |
| tree | 47baeb68536230d3c2baea8754d296d5f6659bbe | |
| parent | 7b84f9e65c39542d16dc2b24bdfe5c07496f2042 (diff) | |
| download | lua-9e8a46daa28e902b4a6d74134d96e80411789175.tar.gz lua-9e8a46daa28e902b4a6d74134d96e80411789175.tar.bz2 lua-9e8a46daa28e902b4a6d74134d96e80411789175.zip | |
details (identation)
Diffstat (limited to '')
| -rw-r--r-- | lstrlib.c | 49 |
1 files changed, 28 insertions, 21 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstrlib.c,v 1.64 2001/02/22 18:59:59 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.65 2001/02/23 17:17:25 roberto Exp roberto $ |
| 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 | */ |
| @@ -162,13 +162,16 @@ static int capture_to_close (MatchState *ms) { | |||
| 162 | static const l_char *luaI_classend (MatchState *ms, const l_char *p) { | 162 | static const l_char *luaI_classend (MatchState *ms, const l_char *p) { |
| 163 | switch (*p++) { | 163 | switch (*p++) { |
| 164 | case ESC: | 164 | case ESC: |
| 165 | if (*p == l_c('\0')) lua_error(ms->L, l_s("malformed pattern (ends with `%')")); | 165 | if (*p == l_c('\0')) |
| 166 | lua_error(ms->L, l_s("malformed pattern (ends with `%')")); | ||
| 166 | return p+1; | 167 | return p+1; |
| 167 | case l_c('['): | 168 | case l_c('['): |
| 168 | if (*p == l_c('^')) p++; | 169 | if (*p == l_c('^')) p++; |
| 169 | do { /* look for a `]' */ | 170 | do { /* look for a `]' */ |
| 170 | if (*p == l_c('\0')) lua_error(ms->L, l_s("malformed pattern (missing `]')")); | 171 | if (*p == l_c('\0')) |
| 171 | if (*(p++) == ESC && *p != l_c('\0')) p++; /* skip escapes (e.g. `%]') */ | 172 | lua_error(ms->L, l_s("malformed pattern (missing `]')")); |
| 173 | if (*(p++) == ESC && *p != l_c('\0')) | ||
| 174 | p++; /* skip escapes (e.g. `%]') */ | ||
| 172 | } while (*p != l_c(']')); | 175 | } while (*p != l_c(']')); |
| 173 | return p+1; | 176 | return p+1; |
| 174 | default: | 177 | default: |
| @@ -196,19 +199,19 @@ static int match_class (l_char c, l_char cl) { | |||
| 196 | } | 199 | } |
| 197 | 200 | ||
| 198 | 201 | ||
| 199 | static int matchbracketclass (l_char c, const l_char *p, const l_char *endclass) { | 202 | static int matchbracketclass (l_char c, const l_char *p, const l_char *ec) { |
| 200 | int sig = 1; | 203 | int sig = 1; |
| 201 | if (*(p+1) == l_c('^')) { | 204 | if (*(p+1) == l_c('^')) { |
| 202 | sig = 0; | 205 | sig = 0; |
| 203 | p++; /* skip the `^' */ | 206 | p++; /* skip the `^' */ |
| 204 | } | 207 | } |
| 205 | while (++p < endclass) { | 208 | while (++p < ec) { |
| 206 | if (*p == ESC) { | 209 | if (*p == ESC) { |
| 207 | p++; | 210 | p++; |
| 208 | if (match_class(c, *p)) | 211 | if (match_class(c, *p)) |
| 209 | return sig; | 212 | return sig; |
| 210 | } | 213 | } |
| 211 | else if ((*(p+1) == l_c('-')) && (p+2 < endclass)) { | 214 | else if ((*(p+1) == l_c('-')) && (p+2 < ec)) { |
| 212 | p+=2; | 215 | p+=2; |
| 213 | if (uchar(*(p-2)) <= uchar(c) && uchar(c) <= uchar(*p)) | 216 | if (uchar(*(p-2)) <= uchar(c) && uchar(c) <= uchar(*p)) |
| 214 | return sig; | 217 | return sig; |
| @@ -236,7 +239,8 @@ static int luaI_singlematch (l_char c, const l_char *p, const l_char *ep) { | |||
| 236 | static const l_char *match (MatchState *ms, const l_char *s, const l_char *p); | 239 | static const l_char *match (MatchState *ms, const l_char *s, const l_char *p); |
| 237 | 240 | ||
| 238 | 241 | ||
| 239 | static const l_char *matchbalance (MatchState *ms, const l_char *s, const l_char *p) { | 242 | static const l_char *matchbalance (MatchState *ms, const l_char *s, |
| 243 | const l_char *p) { | ||
| 240 | if (*p == 0 || *(p+1) == 0) | 244 | if (*p == 0 || *(p+1) == 0) |
| 241 | lua_error(ms->L, l_s("unbalanced pattern")); | 245 | lua_error(ms->L, l_s("unbalanced pattern")); |
| 242 | if (*s != *p) return NULL; | 246 | if (*s != *p) return NULL; |
| @@ -255,8 +259,8 @@ static const l_char *matchbalance (MatchState *ms, const l_char *s, const l_char | |||
| 255 | } | 259 | } |
| 256 | 260 | ||
| 257 | 261 | ||
| 258 | static const l_char *max_expand (MatchState *ms, const l_char *s, const l_char *p, | 262 | static const l_char *max_expand (MatchState *ms, const l_char *s, |
| 259 | const l_char *ep) { | 263 | const l_char *p, const l_char *ep) { |
| 260 | sint32 i = 0; /* counts maximum expand for item */ | 264 | sint32 i = 0; /* counts maximum expand for item */ |
| 261 | while ((s+i)<ms->src_end && luaI_singlematch(*(s+i), p, ep)) | 265 | while ((s+i)<ms->src_end && luaI_singlematch(*(s+i), p, ep)) |
| 262 | i++; | 266 | i++; |
| @@ -270,8 +274,8 @@ static const l_char *max_expand (MatchState *ms, const l_char *s, const l_char * | |||
| 270 | } | 274 | } |
| 271 | 275 | ||
| 272 | 276 | ||
| 273 | static const l_char *min_expand (MatchState *ms, const l_char *s, const l_char *p, | 277 | static const l_char *min_expand (MatchState *ms, const l_char *s, |
| 274 | const l_char *ep) { | 278 | const l_char *p, const l_char *ep) { |
| 275 | for (;;) { | 279 | for (;;) { |
| 276 | const l_char *res = match(ms, s, ep+1); | 280 | const l_char *res = match(ms, s, ep+1); |
| 277 | if (res != NULL) | 281 | if (res != NULL) |
| @@ -284,7 +288,7 @@ static const l_char *min_expand (MatchState *ms, const l_char *s, const l_char * | |||
| 284 | 288 | ||
| 285 | 289 | ||
| 286 | static const l_char *start_capture (MatchState *ms, const l_char *s, | 290 | static const l_char *start_capture (MatchState *ms, const l_char *s, |
| 287 | const l_char *p, int what) { | 291 | const l_char *p, int what) { |
| 288 | const l_char *res; | 292 | const l_char *res; |
| 289 | int level = ms->level; | 293 | int level = ms->level; |
| 290 | if (level >= MAX_CAPTURES) lua_error(ms->L, l_s("too many captures")); | 294 | if (level >= MAX_CAPTURES) lua_error(ms->L, l_s("too many captures")); |
| @@ -297,7 +301,8 @@ static const l_char *start_capture (MatchState *ms, const l_char *s, | |||
| 297 | } | 301 | } |
| 298 | 302 | ||
| 299 | 303 | ||
| 300 | static const l_char *end_capture (MatchState *ms, const l_char *s, const l_char *p) { | 304 | static const l_char *end_capture (MatchState *ms, const l_char *s, |
| 305 | const l_char *p) { | ||
| 301 | int l = capture_to_close(ms); | 306 | int l = capture_to_close(ms); |
| 302 | const l_char *res; | 307 | const l_char *res; |
| 303 | ms->capture[l].len = s - ms->capture[l].init; /* close capture */ | 308 | ms->capture[l].len = s - ms->capture[l].init; /* close capture */ |
| @@ -307,9 +312,10 @@ static const l_char *end_capture (MatchState *ms, const l_char *s, const l_char | |||
| 307 | } | 312 | } |
| 308 | 313 | ||
| 309 | 314 | ||
| 310 | static const l_char *match_capture (MatchState *ms, const l_char *s, int level) { | 315 | static const l_char *match_capture (MatchState *ms, const l_char *s, int l) { |
| 311 | int l = check_capture(ms, level); | 316 | size_t len; |
| 312 | size_t len = ms->capture[l].len; | 317 | l = check_capture(ms, l); |
| 318 | len = ms->capture[l].len; | ||
| 313 | if ((size_t)(ms->src_end-s) >= len && | 319 | if ((size_t)(ms->src_end-s) >= len && |
| 314 | memcmp(ms->capture[l].init, s, len) == 0) | 320 | memcmp(ms->capture[l].init, s, len) == 0) |
| 315 | return s+len; | 321 | return s+len; |
| @@ -372,7 +378,7 @@ static const l_char *match (MatchState *ms, const l_char *s, const l_char *p) { | |||
| 372 | 378 | ||
| 373 | 379 | ||
| 374 | static const l_char *lmemfind (const l_char *s1, size_t l1, | 380 | static const l_char *lmemfind (const l_char *s1, size_t l1, |
| 375 | const l_char *s2, size_t l2) { | 381 | const l_char *s2, size_t l2) { |
| 376 | if (l2 == 0) return s1; /* empty strings are everywhere */ | 382 | if (l2 == 0) return s1; /* empty strings are everywhere */ |
| 377 | else if (l2 > l1) return NULL; /* avoids a negative `l1' */ | 383 | else if (l2 > l1) return NULL; /* avoids a negative `l1' */ |
| 378 | else { | 384 | else { |
| @@ -548,8 +554,8 @@ static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) { | |||
| 548 | } | 554 | } |
| 549 | 555 | ||
| 550 | 556 | ||
| 551 | static const l_char *scanformat (lua_State *L, const l_char *strfrmt, l_char *form, | 557 | static const l_char *scanformat (lua_State *L, const l_char *strfrmt, |
| 552 | int *hasprecision) { | 558 | l_char *form, int *hasprecision) { |
| 553 | const l_char *p = strfrmt; | 559 | const l_char *p = strfrmt; |
| 554 | while (strchr(l_s("-+ #0"), *p)) p++; /* skip flags */ | 560 | while (strchr(l_s("-+ #0"), *p)) p++; /* skip flags */ |
| 555 | if (isdigit(uchar(*p))) p++; /* skip width */ | 561 | if (isdigit(uchar(*p))) p++; /* skip width */ |
| @@ -596,7 +602,8 @@ static int str_format (lua_State *L) { | |||
| 596 | case l_c('o'): case l_c('u'): case l_c('x'): case l_c('X'): | 602 | case l_c('o'): case l_c('u'): case l_c('x'): case l_c('X'): |
| 597 | sprintf(buff, form, (unsigned int)luaL_check_number(L, arg)); | 603 | sprintf(buff, form, (unsigned int)luaL_check_number(L, arg)); |
| 598 | break; | 604 | break; |
| 599 | case l_c('e'): case l_c('E'): case l_c('f'): case l_c('g'): case l_c('G'): | 605 | case l_c('e'): case l_c('E'): case l_c('f'): |
| 606 | case l_c('g'): case l_c('G'): | ||
| 600 | sprintf(buff, form, luaL_check_number(L, arg)); | 607 | sprintf(buff, form, luaL_check_number(L, arg)); |
| 601 | break; | 608 | break; |
| 602 | case l_c('q'): | 609 | case l_c('q'): |
