diff options
author | Rob Landley <rob@landley.net> | 2019-05-04 19:32:52 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-05-04 19:32:52 -0500 |
commit | 6a40e12124b2d441a1085a4e486d685ed12665d0 (patch) | |
tree | 97d517cc0e66f77e41679992ed2f5b74d89b33fe | |
parent | 9f781cd5c3f7b071a3d9a62bf0a4aeb8c3cda8ec (diff) | |
download | toybox-6a40e12124b2d441a1085a4e486d685ed12665d0.tar.gz |
Optimize regexec0() for long lines.
Don't strlen() to find NUL to skip to until after we've confirmed first
section hasn't got a match (by calling regexec() on it).
-rw-r--r-- | lib/lib.c | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -1327,16 +1327,9 @@ int regexec0(regex_t *preg, char *string, long len, int nmatch, char *s = string; for (;;) { - long ll = 0; - int rc; + int rc = regexec(preg, s, nmatch, pmatch, eflags); - while (len && !*s) { - s++; - len--; - } - while (s[ll] && ll<len) ll++; - - rc = regexec(preg, s, nmatch, pmatch, eflags); + // check for match if (!rc) { for (rc = 0; rc<nmatch && pmatch[rc].rm_so!=-1; rc++) { pmatch[rc].rm_so += s-string; @@ -1345,10 +1338,17 @@ int regexec0(regex_t *preg, char *string, long len, int nmatch, return 0; } - if (ll==len) return rc; - s += ll; - len -= ll; + // advance past NUL bytes and try again + while (len && *s) { + s++; + len--; + } + while (len && !*s) { + s++; + len--; + } + if (!len) return REG_NOMATCH; } } |