From: Mikael Magnusson <mikachu@gmail.com>
To: zsh-workers@zsh.org
Subject: PATCH: Support ${PARAM:OFFSET:LENGTH} with an empty offset
Date: Sat, 9 May 2020 08:31:18 +0200 [thread overview]
Message-ID: <20200509063118.25075-1-mikachu@gmail.com> (raw)
Bash allows using this syntax with an empty offset and it doesn't conflict
with any existing zsh syntax. The error message you received before this
also didn't make much sense:
% echo ${a::1}
zsh: closing brace expected
as no matter where you put a closing brace, the syntax is invalid (until
you've removed all colons).
---
Src/subst.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/Src/subst.c b/Src/subst.c
index ecd3c7c50a..40edf3d72a 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1565,6 +1565,11 @@ check_colon_subscript(char *str, char **endp)
if (!*str || ialpha(*str) || *str == '&')
return NULL;
+ if (*str == ':') {
+ *endp = str;
+ return dupstring("0");
+ }
+
*endp = parse_subscript(str, 0, ':');
if (!*endp) {
/* No trailing colon? */
@@ -1575,8 +1580,10 @@ check_colon_subscript(char *str, char **endp)
sav = **endp;
**endp = '\0';
str = dupstring(str);
- if (parsestr(&str))
+ if (parsestr(&str)) {
+ **endp = sav;
return NULL;
+ }
singsub(&str);
remnulargs(str);
untokenize(str);
@@ -2782,7 +2789,6 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
}
if (bct) {
- noclosebrace:
zerr("closing brace expected");
return NULL;
}
@@ -2938,9 +2944,12 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
}
break;
case ':':
- /* this must be `::=', unconditional assignment */
- if (*s != '=' && *s != Equals)
- goto noclosebrace;
+ /* this could be either `::=', unconditional assignment
+ * or a ${PARAM:OFFSET:LENGTH} with an empty offset */
+ if (*s != '=' && *s != Equals) {
+ s -= 1;
+ goto colonsubscript;
+ }
vunset = 1;
s++;
/* Fall through */
@@ -3283,6 +3292,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
* if there isn't a trailing modifier? Why don't we do this
* e.g. when we handle the ${(t)...} flag?
*/
+colonsubscript:
if (chkset) {
val = dupstring(vunset ? "0" : "1");
isarr = 0;
--
2.15.1
reply other threads:[~2020-05-09 6:32 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200509063118.25075-1-mikachu@gmail.com \
--to=mikachu@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).