Skip to content

Commit

Permalink
35343: Avoid $#name length handling for POSIX_IDENTIFIERS
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Stephenson committed May 31, 2015
1 parent 911edc5 commit 75cafcc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
@@ -1,3 +1,9 @@
2015-05-31 Peter Stephenson <p.w.stephenson@ntlworld.com>

* 35343: Doc/Zsh/expn.yo, Doc/Zsh/options.yo, Src/subst.c,
Test/D04parameter.ztst: $#foo for any valid identifier
is only the length of foo if POSIX_IDENTIFIERS is not set.

2015-05-30 Barton E. Schaefer <schaefer@zsh.org>

* users/20238: Doc/Zsh/builtins.yo, Src/builtin.c: add "fc -L" to
Expand Down
7 changes: 7 additions & 0 deletions Doc/Zsh/expn.yo
Expand Up @@ -777,6 +777,13 @@ This has the side-effect that joining is skipped even in quoted
forms, which may affect other sub-expressions in var(spec).
Note that `tt(^)', `tt(=)', and `tt(~)', below, must appear
to the left of `tt(#)' when these forms are combined.

If the option tt(POSIX_IDENTIFIERS) is not set, and var(spec) is a
simple name, then the braces are optional; this is true even
for special parameters so e.g. tt($#-) and tt($#*) take the length
of the string tt($-) and the array tt($*) respectively. If
tt(POSIX_IDENTIFIERS) is set, then braces are required for
the tt(#) to be treated in this fashion.
)
item(tt(${^)var(spec)tt(}))(
pindex(RC_EXPAND_PARAM, toggle)
Expand Down
7 changes: 7 additions & 0 deletions Doc/Zsh/options.yo
Expand Up @@ -2054,6 +2054,13 @@ When this option is set, only the ASCII characters tt(a) to tt(z), tt(A) to
tt(Z), tt(0) to tt(9) and tt(_) may be used in identifiers (names
of shell parameters and modules).

In addition, setting this option limits the effect of parameter
substitution with no braces, so that the expression tt($#) is treated as
the parameter tt($#) even if followed by a valid parameter name.
When it is unset, zsh allows expresions of the form tt($#)var(name)
to refer to the length of tt($)var(name), even for special variables,
for example in expressions such as tt($#-) and tt($#*).

When the option is unset and multibyte character support is enabled (i.e. it
is compiled in and the option tt(MULTIBYTE) is set), then additionally any
alphanumeric characters in the local character set may be used in
Expand Down
3 changes: 2 additions & 1 deletion Src/subst.c
Expand Up @@ -2156,6 +2156,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags)
nojoin = !(ifs && *ifs);
}
} else if ((c == '#' || c == Pound) &&
(inbrace || !isset(POSIXIDENTIFIERS)) &&
(itype_end(s+1, IIDENT, 0) != s + 1
|| (cc = s[1]) == '*' || cc == Star || cc == '@'
|| cc == '?' || cc == Quest
Expand All @@ -2170,7 +2171,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags)
*/
|| ((cc == '#' || cc == Pound) &&
s[2] == Outbrace)
|| (inbrace && (cc == '-' || (cc == ':' && s[2] == '-')))
|| cc == '-' || (cc == ':' && s[2] == '-')
|| (isstring(cc) && (s[2] == Inbrace || s[2] == Inpar)))) {
getlen = 1 + whichlen, s++;
/*
Expand Down
9 changes: 6 additions & 3 deletions Test/D04parameter.ztst
Expand Up @@ -1704,7 +1704,10 @@
[[ $funnychars = ${~${(b)funnychars}} ]]
0:${(b)...} quoting protects from GLOB_SUBST

set --
print $#-1
0:Avoid confusion after overloaded characters in braceless substitution
set -- foo
echo $(( $#*3 ))
emulate sh -c 'nolenwithoutbrace() { echo $#-1; }'
nolenwithoutbrace
0:Avoid confusion after overloaded characters in braceless substitution in sh
>13
>0-1

0 comments on commit 75cafcc

Please sign in to comment.