Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: "${*:offset:length}" and ksh93/bash compatibility



On 3/10/22, Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
> Hello,
>
> From workers/28418 I understand the ${param:offset:length} was
> introduced only for compatibility with bash (though the feature
> was initially from ksh93) as zsh already had $param[first,last]
> (long before bash had arrays or ksh93 even existed).
>
> Still, there's a difference with ksh/bash when
> ${*:offset:length} (same for ${array[*]}) is quoted:
>
> $ zsh --emulate ksh -c 'printf "<%s>\n" ${*:1:2}' zsh foo bar baz
> <foo>
> <bar>
> $ zsh --emulate ksh -c 'printf "<%s>\n" "${*:1:2}"' zsh foo bar baz
> <oo>
>
> $ ksh -c 'printf "<%s>\n" ${*:1:2}' ksh foo bar baz
> <foo>
> <bar>
> $ ksh -c 'printf "<%s>\n" "${*:1:2}"' ksh foo bar baz
> <foo bar>
>
> $ bash -c 'printf "<%s>\n" ${*:1:2}' ksh foo bar baz
> <foo>
> <bar>
> $ bash -c 'printf "<%s>\n" "${*:1:2}"' ksh foo bar baz
> <foo bar>
>
> Should zsh align with bash/ksh there.

If we do, it would probably look something like this (does not handle
all cases), unless someone has a better idea...

diff --git i/Src/subst.c w/Src/subst.c
index 465fe970ff..a34ad5f571 100644
--- i/Src/subst.c
+++ w/Src/subst.c
@@ -2718,7 +2718,7 @@ paramsubst(LinkList l, LinkNode n, char **str,
int qt, int pf_flags,
            }
        }
        /* See if this is a reference to the positional parameters. */
-       if (v && v->pm && v->pm->gsu.a == &vararray_gsu &&
+       if (v && v->pm &&
            (char ***)v->pm->u.data == &pparams)
            horrible_offset_hack = 1;
        else
@@ -3441,6 +3441,17 @@ colonsubscript:
                        }
                        if (offset < 0)
                            offset = 0;
+                   } else if (offset > 0 && horrible_offset_hack) {
+                       offset--;
+                       if (length_set && length > 0) {
+                           char **elem;
+                           int newlen = 0;
+                           for (elem = aval; *elem && length; elem++) {
+                               newlen += strlen(*elem) + 1;
+                               length--;
+                           }
+                           length = newlen - 1;
+                       }
                    }
                    given_offset = offset;
                    MB_METACHARINIT();


-- 
Mikael Magnusson




Messages sorted by: Reverse Date, Date, Thread, Author