diff options
| -rw-r--r-- | strlib.c | 32 |
1 files changed, 22 insertions, 10 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** String library to LUA | 3 | ** String library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_strlib="$Id: strlib.c,v 1.33 1996/11/20 13:47:59 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.34 1996/11/22 13:08:02 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| @@ -195,7 +195,7 @@ static void str_ascii (void) | |||
| 195 | /* pattern matching */ | 195 | /* pattern matching */ |
| 196 | 196 | ||
| 197 | #define ESC '%' | 197 | #define ESC '%' |
| 198 | #define SPECIALS "^$*?.([%" | 198 | #define SPECIALS "^$*?.([%-" |
| 199 | 199 | ||
| 200 | static char *bracket_end (char *p) | 200 | static char *bracket_end (char *p) |
| 201 | { | 201 | { |
| @@ -367,6 +367,17 @@ static char *match (char *s, char *p, int level) | |||
| 367 | return res; | 367 | return res; |
| 368 | p=ep+1; goto init; /* else return match(s, ep+1, level); */ | 368 | p=ep+1; goto init; /* else return match(s, ep+1, level); */ |
| 369 | } | 369 | } |
| 370 | case '-': { /* repetition */ | ||
| 371 | char *res; | ||
| 372 | if ((res = match(s, ep+1, level)) != 0) | ||
| 373 | return res; | ||
| 374 | else if (m) { | ||
| 375 | s++; | ||
| 376 | goto init; /* return match(s+1, p, level); */ | ||
| 377 | } | ||
| 378 | else | ||
| 379 | return NULL; | ||
| 380 | } | ||
| 370 | case '?': { /* optional */ | 381 | case '?': { /* optional */ |
| 371 | char *res; | 382 | char *res; |
| 372 | if (m && (res = match(s+1, ep+1, level))) | 383 | if (m && (res = match(s+1, ep+1, level))) |
| @@ -447,20 +458,21 @@ static void str_gsub (void) | |||
| 447 | char *src = lua_check_string(1, "gsub"); | 458 | char *src = lua_check_string(1, "gsub"); |
| 448 | char *p = lua_check_string(2, "gsub"); | 459 | char *p = lua_check_string(2, "gsub"); |
| 449 | lua_Object newp = lua_getparam(3); | 460 | lua_Object newp = lua_getparam(3); |
| 450 | int max_s = lua_opt_number(4, strlen(src), "gsub"); | 461 | int max_s = lua_opt_number(4, strlen(src)+1, "gsub"); |
| 451 | int anchor = (*p == '^') ? (p++, 1) : 0; | 462 | int anchor = (*p == '^') ? (p++, 1) : 0; |
| 452 | int n = 0; | 463 | int n = 0; |
| 453 | luaI_addchar(0); | 464 | luaI_addchar(0); |
| 454 | while (*src && n < max_s) { | 465 | while (n < max_s) { |
| 455 | char *e; | 466 | char *e = match(src, p, 0); |
| 456 | if ((e=match(src, p, 0)) == NULL) | 467 | if (e) { |
| 457 | luaI_addchar(*src++); | ||
| 458 | else { | ||
| 459 | if (e == src) lua_error("empty pattern in substitution"); | ||
| 460 | add_s(newp); | 468 | add_s(newp); |
| 461 | src = e; | ||
| 462 | n++; | 469 | n++; |
| 463 | } | 470 | } |
| 471 | if (e && e>src) /* non empty match? */ | ||
| 472 | src = e; /* skip it */ | ||
| 473 | else if (*src) | ||
| 474 | luaI_addchar(*src++); | ||
| 475 | else break; | ||
| 464 | if (anchor) break; | 476 | if (anchor) break; |
| 465 | } | 477 | } |
| 466 | addstr(src); | 478 | addstr(src); |
