From: Stephane Chazelas <stephane@chazelas.org>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [bug] $scalar[(pws[\0])1] or $scalar[(pws[á])1] doesn't work
Date: Sun, 13 Nov 2022 08:14:52 +0000 [thread overview]
Message-ID: <20221113081452.bk7zdnbc6qvqjxef@chazelas.org> (raw)
From: https://unix.stackexchange.com/questions/724638/subscripted-variable-expansion-on-nul-delimited-words-in-zsh
Note that we're talking of the s[separator] *array subscript*
flag combined with w and p, not the s[separator] parameter
expansion flag here.
$ a=$'foo\0bar'
$ echo $a[(pws[\0])1]
f
Expected "foo".
It seems that's some missing metafication as confirmed by:
$ echo á | hd
00000000 c3 a1 0a |...|
00000003
(0xa1 needing metafication as well)
$ a=fooábar
$ echo $a[(pws[á])1]
fooábar
$ a=fooébar
$ echo $a[(pws[é])1]
foo
$ n=$'\x83\x20' # a metafied NUL
$ a=$'foo\0bar'
$ eval "echo \$a[(pws[$n])1]"
foo
Also note the doc says about the "w" subscript flag:
> w
> If the parameter subscripted is a scalar then this flag makes
> subscripting work on words instead of characters. The default word
> separator is whitespace.[...]
I find that's not true. By default it seems to split on $IFS
characters (which by default does include space, tab and
newline but also NUL). Also I find empty elements are always
discarded even if $IFS-splitting with non-whitespace characters:
$ a=::foo:bar
$ echo $a[(ws[:])1]
foo
$ IFS=:
$ echo $a[(w)1]
foo
Also, it would be nice if one could do $a[(pws[$var])1] to align
with ${(ps[$var])var}
--
Stephane
next reply other threads:[~2022-11-13 8:15 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-13 8:14 Stephane Chazelas [this message]
2022-11-18 12:25 ` Stephane Chazelas
2022-11-28 15:50 ` Jun. T
2022-11-28 23:57 ` Jun T
2022-11-28 15:47 ` Jun. T
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=20221113081452.bk7zdnbc6qvqjxef@chazelas.org \
--to=stephane@chazelas.org \
--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).