From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22492 invoked by alias); 10 Mar 2011 11:10:20 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 28877 Received: (qmail 24413 invoked from network); 10 Mar 2011 11:10:07 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.212.43 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=X369OjSt+kevvkSiiEOasBW9OBuNy2v8ijl4StjfVIQ=; b=GuZNohdzn+tf1XCsuBwvLR0rJKcyycAfZlcGh+aTZkC0Zjp2eW68dXgcnKbmCpEem7 YTij3jyCwCc/oNCrxThw3RsLOE/Lemgb3HvZPgQNp60pkxWffsTpOFiPkzKQ/LXIzmf9 MmDbIiZrKDyDYrLEe1W9TsXmiwGFJmPNp/A9w= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=xDc9HdqeGvVJWOSyHj4E8ZjW8Kx56e8856DAgtV8+FvWpC3rp79MEr1cW8qAI71EN9 VKt0ZfqyojegC5GC0hjt0GKQRxRXhlsj8M5lpfiHYnfcSfKtDXAMXdJM8LifGGEG6Wsa HtmaX1wHAIH5IJC0i/xLp8E4sM+iMVedQailI= MIME-Version: 1.0 Date: Thu, 10 Mar 2011 12:10:00 +0100 Message-ID: Subject: support negative LEN in ${VAR:OFFSET:LEN} From: Mikael Magnusson To: zsh workers Content-Type: text/plain; charset=UTF-8 bash 4.2 adds support for this. http://cgit.mika.l3ib.org/cgit/zsh-cvs/patch/?id=3950051089ce1c87679abf0335edc16fd9289878 From: Mikael Magnusson Date: Thu, 10 Mar 2011 10:56:19 +0100 Subject: [PATCH] support LEN < 0 in ${VAR:OFFSET:LEN}, new in bash 4.2 --- Doc/Zsh/expn.yo | 12 ++++++++---- Src/subst.c | 36 +++++++++++++++++++++++++++--------- Test/D04parameter.ztst | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo index 9c44913..b48ddd9 100644 --- a/Doc/Zsh/expn.yo +++ b/Doc/Zsh/expn.yo @@ -588,7 +588,7 @@ remove the non-matched elements). xitem(tt(${)var(name)tt(:)var(offset)tt(})) item(tt(${)var(name)tt(:)var(offset)tt(:)var(length)tt(}))( This syntax gives effects similar to parameter subscripting -in the form tt($)var(name)tt({)var(start)tt(,)var(end)tt(}), but is +in the form tt($)var(name)tt([)var(start)tt(,)var(end)tt(]), but is compatible with other shells; note that both var(offset) and var(length) are interpreted differently from the components of a subscript. @@ -608,8 +608,12 @@ the option tt(KSH_ARRAYS). A negative offset counts backwards from the end of the scalar or array, so that -1 corresponds to the last character or element, and so on. -var(length) is always treated directly as a length and hence may not be -negative. The option tt(MULTIBYTE) is obeyed, i.e. the offset and length +var(length) is treated directly as a length when it is positive. +When it is negative, it works as an offset just like var(offset). If +this results in a negative length, a diagnostic will be printed and +nothing will be substituted. + +The option tt(MULTIBYTE) is obeyed, i.e. the offset and length count multibyte characters where appropriate. var(offset) and var(length) undergo the same set of shell substitutions @@ -635,7 +639,7 @@ tt(${)var(name)tt(:-)var(word)tt(}) form of substitution. Instead, a space may be inserted before the tt(-). Furthermore, neither var(offset) nor var(length) may begin with an alphabetic character or tt(&) as these are used to indicate history-style modifiers. To substitute a value from a -variable, the recommended approach is to proceed it with a tt($) as this +variable, the recommended approach is to precede it with a tt($) as this signifies the intention (parameter substitution can easily be rendered unreadable); however, as arithmetic substitution is performed, the expression tt(${var: offs}) does work, retrieving the offset from diff --git a/Src/subst.c b/Src/subst.c index 377aba8..141a353 100644 --- a/Src/subst.c +++ b/Src/subst.c @@ -2799,7 +2799,8 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) char *check_offset = check_colon_subscript(s, &check_offset2); if (check_offset) { zlong offset = mathevali(check_offset); - zlong length = (zlong)-1; + zlong length; + int length_set = 0; int offset_hack_argzero = 0; if (errflag) return NULL; @@ -2814,14 +2815,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) zerr("invalid length: %s", check_offset); return NULL; } - if (check_offset) { + if (check_offset) { length = mathevali(check_offset); + length_set = 1; if (errflag) return NULL; - if (length < (zlong)0) { - zerr("invalid length: %s", check_offset); - return NULL; - } } } if (horrible_offset_hack) { @@ -2849,8 +2847,15 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) } if (offset_hack_argzero) alen++; - if (length < 0) - length = alen; + if (length_set) { + if (length < 0) + length += alen - offset; + if (length < 0) { + zerr("substring expression < 0: %d", length); + return NULL; + } + } else + length = alen; if (offset > alen) offset = alen; if (offset + length > alen) @@ -2879,11 +2884,24 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) offset = 0; } MB_METACHARINIT(); + if (length_set && length < 0) + length -= offset; for (sptr = val; *sptr && offset; ) { sptr += MB_METACHARLEN(sptr); offset--; } - if (length >= 0) { + if (length_set) { + if (length < 0) { + MB_METACHARINIT(); + for (eptr = val; *eptr; ) { + eptr += MB_METACHARLEN(eptr); + length++; + } + if (length < 0) { + zerr("substring expression < 0: %d", length); + return NULL; + } + } for (eptr = sptr; *eptr && length; ) { eptr += MB_METACHARLEN(eptr); length--; diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst index c0ad1d2..272fb87 100644 --- a/Test/D04parameter.ztst +++ b/Test/D04parameter.ztst @@ -1338,6 +1338,7 @@ print ${foo:$(echo 3 + 3):`echo 4 - 3`} print ${foo: -1} print ${foo: -10} + print ${foo:5:-2} 0:Bash-style offsets, scalar >456789 >56789 @@ -1349,6 +1350,7 @@ >7 >9 >123456789 +>67 foo=(1 2 3 4 5 6 7 8 9) print ${foo:3} @@ -1361,6 +1363,7 @@ print ${foo:$(echo 3 + 3):`echo 4 - 3`} print ${foo: -1} print ${foo: -10} + print ${foo:5:-2} 0:Bash-style offsets, array >4 5 6 7 8 9 >5 6 7 8 9 @@ -1372,6 +1375,7 @@ >7 >9 >1 2 3 4 5 6 7 8 9 +>6 7 testfn() { emulate -L sh @@ -1410,3 +1414,13 @@ print ${str:0:} 1:Regression test for missing length after offset ?(eval):2: unrecognized modifier + + foo="123456789" + print ${foo:5:-6} +1:Regression test for total length < 0 in string +?(eval):2: substring expression < 0: -2 + + foo=(1 2 3 4 5 6 7 8 9) + print ${foo:5:-6} +1:Regression test for total length < 0 in array +?(eval):2: substring expression < 0: -2 -- 1.7.4-rc1 -- Mikael Magnusson