diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-07-05 11:30:38 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-07-05 11:30:38 -0300 |
| commit | 8efb44b5e046fd1ba671f489b3b996ff97386165 (patch) | |
| tree | 6eb6c3a88b3388f7b560e5d94a752295af94928d | |
| parent | f7720bebe31f3523ef91a68fde5a1e3ff1768be9 (diff) | |
| download | lua-8efb44b5e046fd1ba671f489b3b996ff97386165.tar.gz lua-8efb44b5e046fd1ba671f489b3b996ff97386165.tar.bz2 lua-8efb44b5e046fd1ba671f489b3b996ff97386165.zip | |
changes in 'string.find' and 'string.gfind' (new 'string.match' and
'string.gmatch')
| -rw-r--r-- | lstrlib.c | 55 |
1 files changed, 43 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstrlib.c,v 1.116 2005/05/20 15:53:42 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.117 2005/05/31 14:25:18 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 | */ |
| @@ -485,15 +485,15 @@ static int push_captures (MatchState *ms, const char *s, const char *e) { | |||
| 485 | } | 485 | } |
| 486 | 486 | ||
| 487 | 487 | ||
| 488 | static int str_find (lua_State *L) { | 488 | static int str_find_aux (lua_State *L, int find) { |
| 489 | size_t l1, l2; | 489 | size_t l1, l2; |
| 490 | const char *s = luaL_checklstring(L, 1, &l1); | 490 | const char *s = luaL_checklstring(L, 1, &l1); |
| 491 | const char *p = luaL_checklstring(L, 2, &l2); | 491 | const char *p = luaL_checklstring(L, 2, &l2); |
| 492 | ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; | 492 | ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; |
| 493 | if (init < 0) init = 0; | 493 | if (init < 0) init = 0; |
| 494 | else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; | 494 | else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; |
| 495 | if (lua_toboolean(L, 4) || /* explicit request? */ | 495 | if (find && (lua_toboolean(L, 4) || /* explicit request? */ |
| 496 | strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ | 496 | strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ |
| 497 | /* do a plain search */ | 497 | /* do a plain search */ |
| 498 | const char *s2 = lmemfind(s+init, l1-init, p, l2); | 498 | const char *s2 = lmemfind(s+init, l1-init, p, l2); |
| 499 | if (s2) { | 499 | if (s2) { |
| @@ -513,18 +513,37 @@ static int str_find (lua_State *L) { | |||
| 513 | const char *res; | 513 | const char *res; |
| 514 | ms.level = 0; | 514 | ms.level = 0; |
| 515 | if ((res=match(&ms, s1, p)) != NULL) { | 515 | if ((res=match(&ms, s1, p)) != NULL) { |
| 516 | lua_pushinteger(L, s1-s+1); /* start */ | 516 | if (find) { |
| 517 | lua_pushinteger(L, res-s); /* end */ | 517 | lua_pushinteger(L, s1-s+1); /* start */ |
| 518 | return push_captures(&ms, NULL, 0) + 2; | 518 | lua_pushinteger(L, res-s); /* end */ |
| 519 | #if defined(LUA_COMPAT_FIND) | ||
| 520 | return push_captures(&ms, NULL, 0) + 2; | ||
| 521 | #else | ||
| 522 | return 2; | ||
| 523 | #endif | ||
| 524 | |||
| 525 | } | ||
| 526 | else | ||
| 527 | return push_captures(&ms, s1, res); | ||
| 519 | } | 528 | } |
| 520 | } while (s1++<ms.src_end && !anchor); | 529 | } while (s1++ < ms.src_end && !anchor); |
| 521 | } | 530 | } |
| 522 | lua_pushnil(L); /* not found */ | 531 | lua_pushnil(L); /* not found */ |
| 523 | return 1; | 532 | return 1; |
| 524 | } | 533 | } |
| 525 | 534 | ||
| 526 | 535 | ||
| 527 | static int gfind_aux (lua_State *L) { | 536 | static int str_find (lua_State *L) { |
| 537 | return str_find_aux(L, 1); | ||
| 538 | } | ||
| 539 | |||
| 540 | |||
| 541 | static int str_match (lua_State *L) { | ||
| 542 | return str_find_aux(L, 0); | ||
| 543 | } | ||
| 544 | |||
| 545 | |||
| 546 | static int gmatch_aux (lua_State *L) { | ||
| 528 | MatchState ms; | 547 | MatchState ms; |
| 529 | size_t ls; | 548 | size_t ls; |
| 530 | const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); | 549 | const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); |
| @@ -550,16 +569,22 @@ static int gfind_aux (lua_State *L) { | |||
| 550 | } | 569 | } |
| 551 | 570 | ||
| 552 | 571 | ||
| 553 | static int gfind (lua_State *L) { | 572 | static int gmatch (lua_State *L) { |
| 554 | luaL_checkstring(L, 1); | 573 | luaL_checkstring(L, 1); |
| 555 | luaL_checkstring(L, 2); | 574 | luaL_checkstring(L, 2); |
| 556 | lua_settop(L, 2); | 575 | lua_settop(L, 2); |
| 557 | lua_pushinteger(L, 0); | 576 | lua_pushinteger(L, 0); |
| 558 | lua_pushcclosure(L, gfind_aux, 3); | 577 | lua_pushcclosure(L, gmatch_aux, 3); |
| 559 | return 1; | 578 | return 1; |
| 560 | } | 579 | } |
| 561 | 580 | ||
| 562 | 581 | ||
| 582 | static int gfind_nodef (lua_State *L) { | ||
| 583 | return luaL_error(L, LUA_QL("string.gfind") " was renamed to " | ||
| 584 | LUA_QL("string.gmatch")); | ||
| 585 | } | ||
| 586 | |||
| 587 | |||
| 563 | static void add_s (MatchState *ms, luaL_Buffer *b, | 588 | static void add_s (MatchState *ms, luaL_Buffer *b, |
| 564 | const char *s, const char *e) { | 589 | const char *s, const char *e) { |
| 565 | lua_State *L = ms->L; | 590 | lua_State *L = ms->L; |
| @@ -765,8 +790,10 @@ static const luaL_reg strlib[] = { | |||
| 765 | {"format", str_format}, | 790 | {"format", str_format}, |
| 766 | {"dump", str_dump}, | 791 | {"dump", str_dump}, |
| 767 | {"find", str_find}, | 792 | {"find", str_find}, |
| 768 | {"gfind", gfind}, | 793 | {"gfind", gfind_nodef}, |
| 794 | {"gmatch", gmatch}, | ||
| 769 | {"gsub", str_gsub}, | 795 | {"gsub", str_gsub}, |
| 796 | {"match", str_match}, | ||
| 770 | {NULL, NULL} | 797 | {NULL, NULL} |
| 771 | }; | 798 | }; |
| 772 | 799 | ||
| @@ -790,6 +817,10 @@ static void createmetatable (lua_State *L) { | |||
| 790 | */ | 817 | */ |
| 791 | LUALIB_API int luaopen_string (lua_State *L) { | 818 | LUALIB_API int luaopen_string (lua_State *L) { |
| 792 | luaL_openlib(L, LUA_STRLIBNAME, strlib, 0); | 819 | luaL_openlib(L, LUA_STRLIBNAME, strlib, 0); |
| 820 | #if defined(LUA_COMPAT_GFIND) | ||
| 821 | lua_getfield(L, -1, "gmatch"); | ||
| 822 | lua_setfield(L, -2, "gfind"); | ||
| 823 | #endif | ||
| 793 | createmetatable(L); | 824 | createmetatable(L); |
| 794 | return 1; | 825 | return 1; |
| 795 | } | 826 | } |
