From 24baa919c1a5e63a32565cbba0a331de9a1fc63e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Nov 2010 18:39:41 -0200 Subject: small bug: 'find' did not detect magic chars after a \0 in a pattern and did a plain search in those cases --- lstrlib.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 493756c3..090ea711 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.156 2010/10/29 17:52:46 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.158 2010/11/16 19:14:21 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -505,6 +505,18 @@ static int push_captures (MatchState *ms, const char *s, const char *e) { } +/* check whether pattern has no special characters */ +static int nospecials (const char *p, size_t l) { + size_t upto = 0; + do { + if (strpbrk(p + upto, SPECIALS)) + return 0; /* pattern has a special character */ + upto += strlen(p + upto) + 1; /* may have more after \0 */ + } while (upto <= l); + return 1; /* no special chars found */ +} + + static int str_find_aux (lua_State *L, int find) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); @@ -515,8 +527,8 @@ static int str_find_aux (lua_State *L, int find) { lua_pushnil(L); /* cannot find anything */ return 1; } - if (find && (lua_toboolean(L, 4) || /* explicit request? */ - strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ + /* explicit request or no special characters? */ + if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); if (s2) { -- cgit v1.2.3-55-g6feb