diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-10-09 03:52:04 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-10-09 03:52:04 +0200 |
commit | 49bcf9f40cff1320a761d674cf89a0c0ab97ef49 (patch) | |
tree | c687441c2bf142649e8010b44e3192aadae4e47a | |
parent | 5aaeb550b76f063ffddef8587d9d91f7fddc4406 (diff) | |
download | busybox-w32-49bcf9f40cff1320a761d674cf89a0c0ab97ef49.tar.gz busybox-w32-49bcf9f40cff1320a761d674cf89a0c0ab97ef49.tar.bz2 busybox-w32-49bcf9f40cff1320a761d674cf89a0c0ab97ef49.zip |
hush: speed up ${x//\*/|} too
function old new delta
expand_one_var 2502 2544 +42
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/hush.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/shell/hush.c b/shell/hush.c index 6d472337f..87fc2f445 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -6472,16 +6472,21 @@ static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p) | |||
6472 | /* ${var/[/]pattern[/repl]} helpers */ | 6472 | /* ${var/[/]pattern[/repl]} helpers */ |
6473 | static char *strstr_pattern(char *val, const char *pattern, int *size) | 6473 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
6474 | { | 6474 | { |
6475 | int sz = strcspn(pattern, "*?[\\"); | 6475 | int first_escaped = (pattern[0] == '\\' && pattern[1]); |
6476 | if (pattern[sz] == '\0') { | 6476 | /* "first_escaped" trick allows to treat e.g. "\*no_glob_chars" |
6477 | * as literal too (as it is semi-common, and easy to accomodate | ||
6478 | * by just using str + 1). | ||
6479 | */ | ||
6480 | int sz = strcspn(pattern + first_escaped * 2, "*?[\\"); | ||
6481 | if ((pattern + first_escaped * 2)[sz] == '\0') { | ||
6477 | /* Optimization for trivial patterns. | 6482 | /* Optimization for trivial patterns. |
6478 | * Testcase for very slow replace (performs about 22k replaces): | 6483 | * Testcase for very slow replace (performs about 22k replaces): |
6479 | * x=:::::::::::::::::::::: | 6484 | * x=:::::::::::::::::::::: |
6480 | * 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} | 6485 | * 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} |
6481 | * echo "${x//:/|}" | 6486 | * echo "${x//:/|}" |
6482 | */ | 6487 | */ |
6483 | *size = sz; | 6488 | *size = sz + first_escaped; |
6484 | return strstr(val, pattern); | 6489 | return strstr(val, pattern + first_escaped); |
6485 | } | 6490 | } |
6486 | 6491 | ||
6487 | while (1) { | 6492 | while (1) { |