From: Jun T <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: Re: "${*:offset:length}" and ksh93/bash compatibility
Date: Wed, 16 Mar 2022 14:57:16 +0900 [thread overview]
Message-ID: <68098B63-701D-4DDF-9A44-2B1DC80FA0D2@kba.biglobe.ne.jp> (raw)
In-Reply-To: <CAHYJk3TTtr_4ib5rebTb+XicG1s0oEKcz8wAKdOcD2ztdOy8Xg@mail.gmail.com>
> 2022/03/14 0:22, Mikael Magnusson <mikachu@gmail.com> wrote:
>
> I realized that this is not limited to $*,
>
> zsh% a=(one two three); echo "${a[@]:0:2}"
> one two
> zsh% a=(one two three); echo "${a[*]:0:2}"
> on
The following patch seems to work for
"${*:1:2}" "${@:1:2}" "$a[@]:0:2}" "${a[*]:0:2}"
For example (with or without KSH_ARRAYS):
zsh% printf '<%s>\n' "${a[@]:0:2}"
<one>
<two>
If KSH_ARRAYS is not set, "$a:0:2" behaves like "${a[*]:0:2}".
I don't know if there are better/simpler solutions.
'make check' passes, but please do some tests to see if there
are any bad side effects.
diff --git a/Src/subst.c b/Src/subst.c
index 465fe970f..0f98e6ea3 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1854,6 +1854,12 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
* nested (P) flags.
*/
int fetch_needed;
+ /*
+ * If an array parameter is quoted but has :offset:length (as in
+ * "${array:off:len}"), we apply :off:len as array index before
+ * joining the array into a string (for compatibility with ksh/bash).
+ */
+ int quoted_array_with_offset = 0;
*s++ = '\0';
/*
@@ -3377,7 +3383,16 @@ colonsubscript:
return NULL;
}
}
- if (isarr) {
+ /*
+ * We've got :OFFSET (and :LENGTH).
+ * If aval is non-NULL but isarr is 0, PARAM is (probably)
+ * an array but quoted like "${PARAM:OFFSET}". We apply
+ * :OFFSET as array index (as if it is not quoted). We will
+ * join them later (search for quoted_array_with_offset).
+ */
+ if (aval && !isarr)
+ quoted_array_with_offset = 1;
+ if (isarr || quoted_array_with_offset) {
int alen, count;
char **srcptr, **dstptr, **newarr;
@@ -3622,9 +3637,9 @@ colonsubscript:
* exception is that ${name:-word} and ${name:+word} will have already
* done any requested splitting of the word value with quoting preserved.
*/
- if (ssub || spbreak || spsep || sep) {
+ if (ssub || spbreak || spsep || sep || quoted_array_with_offset) {
int force_split = !ssub && (spbreak || spsep);
- if (isarr) {
+ if (isarr || quoted_array_with_offset) {
/* sep non-null here means F or j flag, force join */
if (nojoin == 0 || sep) {
val = sepjoin(aval, sep, 1);
next prev parent reply other threads:[~2022-03-16 5:57 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-10 7:50 Stephane Chazelas
2022-03-10 12:48 ` Mikael Magnusson
2022-03-10 14:25 ` Peter Stephenson
2022-03-13 15:22 ` Mikael Magnusson
2022-03-13 18:21 ` Bart Schaefer
2022-03-13 19:23 ` Mikael Magnusson
2022-03-13 20:57 ` Bart Schaefer
2022-03-16 5:57 ` Jun T [this message]
2022-03-16 15:57 ` Mikael Magnusson
2022-03-23 8:08 ` Jun T
2022-03-23 10:23 ` Mikael Magnusson
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=68098B63-701D-4DDF-9A44-2B1DC80FA0D2@kba.biglobe.ne.jp \
--to=takimoto-j@kba.biglobe.ne.jp \
--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).