diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-16 18:39:41 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-16 18:39:41 -0200 |
commit | 24baa919c1a5e63a32565cbba0a331de9a1fc63e (patch) | |
tree | 4cb299a2a50a5a0141d66361499ceb13af5cf2b6 | |
parent | d1c0efdb7da3bab589290ce196abbc44e92d38f8 (diff) | |
download | lua-24baa919c1a5e63a32565cbba0a331de9a1fc63e.tar.gz lua-24baa919c1a5e63a32565cbba0a331de9a1fc63e.tar.bz2 lua-24baa919c1a5e63a32565cbba0a331de9a1fc63e.zip |
small bug: 'find' did not detect magic chars after a \0 in a pattern
and did a plain search in those cases
-rw-r--r-- | lstrlib.c | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.156 2010/10/29 17:52:46 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.158 2010/11/16 19:14: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 | */ |
@@ -505,6 +505,18 @@ static int push_captures (MatchState *ms, const char *s, const char *e) { | |||
505 | } | 505 | } |
506 | 506 | ||
507 | 507 | ||
508 | /* check whether pattern has no special characters */ | ||
509 | static int nospecials (const char *p, size_t l) { | ||
510 | size_t upto = 0; | ||
511 | do { | ||
512 | if (strpbrk(p + upto, SPECIALS)) | ||
513 | return 0; /* pattern has a special character */ | ||
514 | upto += strlen(p + upto) + 1; /* may have more after \0 */ | ||
515 | } while (upto <= l); | ||
516 | return 1; /* no special chars found */ | ||
517 | } | ||
518 | |||
519 | |||
508 | static int str_find_aux (lua_State *L, int find) { | 520 | static int str_find_aux (lua_State *L, int find) { |
509 | size_t ls, lp; | 521 | size_t ls, lp; |
510 | const char *s = luaL_checklstring(L, 1, &ls); | 522 | const char *s = luaL_checklstring(L, 1, &ls); |
@@ -515,8 +527,8 @@ static int str_find_aux (lua_State *L, int find) { | |||
515 | lua_pushnil(L); /* cannot find anything */ | 527 | lua_pushnil(L); /* cannot find anything */ |
516 | return 1; | 528 | return 1; |
517 | } | 529 | } |
518 | if (find && (lua_toboolean(L, 4) || /* explicit request? */ | 530 | /* explicit request or no special characters? */ |
519 | strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ | 531 | if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { |
520 | /* do a plain search */ | 532 | /* do a plain search */ |
521 | const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); | 533 | const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); |
522 | if (s2) { | 534 | if (s2) { |