summary refs log tree commit diff
diff options
context:
space:
mode:
authorJun-ichi Takimoto <takimoto-j@kba.biglobe.ne.jp>2022-03-29 11:09:36 +0900
committerJun-ichi Takimoto <takimoto-j@kba.biglobe.ne.jp>2022-03-29 11:09:36 +0900
commit8154ddc62d404589ef6059ab17901fb5101f301f (patch)
treec6104b16aded952bc78193efc29782fbc585290a
parent1e814ba48bd92fcf573b4c58eca109ed93508651 (diff)
downloadzsh-8154ddc62d404589ef6059ab17901fb5101f301f.tar.gz
zsh-8154ddc62d404589ef6059ab17901fb5101f301f.tar.xz
zsh-8154ddc62d404589ef6059ab17901fb5101f301f.zip
49853 + 49882/49883: make "${arr[*]:off}" compatible with ksh/bash
-rw-r--r--ChangeLog5
-rw-r--r--Src/subst.c21
-rw-r--r--Test/D04parameter.ztst32
3 files changed, 55 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 7603b7cf6..abcae8067 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2022-03-29  Jun-ichi Takimoto  <takimoto-j@kba.biglobe.ne.jp>
+
+	* 49853 + 49882/49883: Src/subst.c, Test/D04parameter.ztst: make
+	"${array[*]:offset:length}" compatible with ksh/bash
+
 2022-03-28  Peter Stephenson  <p.stephenson@samsung.com>
 
 	* 49890: Src/Zle/zle_utils.c: best not to look at unallocated
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);
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index b6b1f2e33..6bf55b4db 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1693,6 +1693,38 @@
 >b
 >c
 
+   () {
+     emulate -L sh
+     local a=( one two three )
+     printf '<%s><%s>\n' ${a[*]:0:2}
+     printf '<%s><%s>\n' "${a[*]:0:2}"
+     printf '<%s><%s>\n' ${a[@]:0:2}
+     printf '<%s><%s>\n' "${a[@]:0:2}"
+     printf '<%s><%s>\n' "${a:0:2}"
+     printf '<%s><%s>\n' ${*:1:2}
+     printf '<%s><%s>\n' "${*:1:2}"
+     printf '<%s><%s>\n' ${@:1:2}
+     printf '<%s><%s>\n' "${@:1:2}"
+     printf '<%s><%s>\n' ${*:0:2}
+     printf '<%s><%s>\n' "${*:0:2}"
+     printf '<%s><%s>\n' ${@:0:2}
+     printf '<%s><%s>\n' "${@:0:2}"
+   } one two three
+0:Bash-style offsets, quoted array
+><one><two>
+><one two><>
+><one><two>
+><one><two>
+><on><>
+><one><two>
+><one two><>
+><one><two>
+><one><two>
+><(anon)><one>
+><(anon) one><>
+><(anon)><one>
+><(anon)><one>
+
    printf "%n" '[0]'
 1:Regression test for identifier test
 ?(eval):1: not an identifier: [0]