From 37460f5daff9b9ed751ce37b912cc61de94adf09 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 27 Jul 2021 18:13:11 +0200 Subject: hush: tweak ${var/pattern/repl} optimization function old new delta expand_one_var 2507 2502 -5 Signed-off-by: Denys Vlasenko --- shell/hush.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'shell/hush.c') diff --git a/shell/hush.c b/shell/hush.c index 179155f..c970d90 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -6466,17 +6466,16 @@ static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p) /* ${var/[/]pattern[/repl]} helpers */ static char *strstr_pattern(char *val, const char *pattern, int *size) { - if (!strpbrk(pattern, "*?[\\")) { + int sz = strcspn(pattern, "*?[\\"); + if (pattern[sz] == '\0') { /* Optimization for trivial patterns. * Testcase for very slow replace (performs about 22k replaces): * x=:::::::::::::::::::::: * x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;echo ${#x} * echo "${x//:/|}" */ - char *found = strstr(val, pattern); - if (found) - *size = strlen(pattern); - return found; + *size = sz; + return strstr(val, pattern); } while (1) { -- cgit v1.1