hush: speed up ${x//\*/|} too
function old new delta expand_one_var 2502 2544 +42 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
5aaeb550b7
commit
49bcf9f40c
13
shell/hush.c
13
shell/hush.c
@ -6472,16 +6472,21 @@ static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p)
|
|||||||
/* ${var/[/]pattern[/repl]} helpers */
|
/* ${var/[/]pattern[/repl]} helpers */
|
||||||
static char *strstr_pattern(char *val, const char *pattern, int *size)
|
static char *strstr_pattern(char *val, const char *pattern, int *size)
|
||||||
{
|
{
|
||||||
int sz = strcspn(pattern, "*?[\\");
|
int first_escaped = (pattern[0] == '\\' && pattern[1]);
|
||||||
if (pattern[sz] == '\0') {
|
/* "first_escaped" trick allows to treat e.g. "\*no_glob_chars"
|
||||||
|
* as literal too (as it is semi-common, and easy to accomodate
|
||||||
|
* by just using str + 1).
|
||||||
|
*/
|
||||||
|
int sz = strcspn(pattern + first_escaped * 2, "*?[\\");
|
||||||
|
if ((pattern + first_escaped * 2)[sz] == '\0') {
|
||||||
/* Optimization for trivial patterns.
|
/* Optimization for trivial patterns.
|
||||||
* Testcase for very slow replace (performs about 22k replaces):
|
* 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$x;echo ${#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//:/|}"
|
* echo "${x//:/|}"
|
||||||
*/
|
*/
|
||||||
*size = sz;
|
*size = sz + first_escaped;
|
||||||
return strstr(val, pattern);
|
return strstr(val, pattern + first_escaped);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
Loading…
Reference in New Issue
Block a user