Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make anchored empty pattern match in ${parameter/pattern/string}
@ormaaj wrote: > This is useful for compatibility and I think it's supposed to > work anyway. > > Working in ksh93 v- and u+m: > > ${x/~(E)^/y} > > Non-working: > > ${x/#/y} > ${x/#~(K)/y} The current ksh93 behaviour, though unique, /is/ logical. Unlike an empty regular expression, an empty shell pattern matches nothing. The non-working reproducers tell ksh to match an empty glob pattern at #, the beginning of the string (the ~(K) signifying a shell/glob pattern, which is also the default). Since empty shell patterns never match, no substitution is done. Having said that, mksh, bash, yash, and zsh have all special-cased ${var/#/prefix} and ${var/%/suffix}. I think we should do the same, because the strictly logical behaviour is not meeting user expectations and the special-casing is useful, particularly in vector expansions such as ${@/#/prefix} (which will now prefix each positional parameter). This change is a potential corner-case incompatibility, so it is applied only to the dev branch for the future ksh 93u+m/1.1. (The ${x/#~(K)/y} case still doesn't work after this commit, but I'm okay with that; it explicitly circumvents the special-casing and basically says "yes, I absolutely want to match the string against an empty glob pattern". So that case is not going to break user expectations.) src/cmd/ksh93/sh/macro.c: - When calling strngrpmatch(3) (in the '%' case, via substring()), if pattern is empty, pass an ERE anchor (^ or $ prefixed by ~(E)) to match the beginning or end of the string, as appropriate. Resolves: #558
- Loading branch information