zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: "${*:offset:length}" and ksh93/bash compatibility
Date: Thu, 10 Mar 2022 13:48:57 +0100	[thread overview]
Message-ID: <CAHYJk3Q-Oe6Fp-JGENpLkLLXTr8JE_cjMWXyq8N6ELOTP7yysg@mail.gmail.com> (raw)
In-Reply-To: <20220310075026.ovwlhxq24cmda5s3@chazelas.org>

On 3/10/22, Stephane Chazelas <stephane@chazelas.org> 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


  reply	other threads:[~2022-03-10 12:49 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 [this message]
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
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=CAHYJk3Q-Oe6Fp-JGENpLkLLXTr8JE_cjMWXyq8N6ELOTP7yysg@mail.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).