zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: clarification on (#U) in pattern matching.
Date: Sun, 6 Feb 2022 08:42:55 +0000	[thread overview]
Message-ID: <20220206084255.tn3dgitvpr7qdjig@chazelas.org> (raw)

$ set -o extendedglob
$ a='Stéphane€'
$ print -rn -- ${a//(#U)?} | hd
00000000  a9 82 ac                                          |...|
00000003

It seems that with (#U) (and here in a locale using UTF-8 as
charmap), ? with (#U) matches only on the first byte of
multibyte characters. Is that how it's meant to be?

$ print -r -- ${a//(#m)?/[$MATCH]}
[S][t][é][p][h][a][n][e][€]
$ print -r -- ${a//(#Um)?/[$MATCH]}
[S][t][�]�[p][h][a][n][e][�]��

Also

[[ $'\ue9' = (#U)*$'\xa9'* ]] returns true (and doesn't without
(#U), but:

print -r -- ${a//(#U)$'\xa9'}

fails to remove it. But:

$ echo ${a//(#U)?$'\xa9'}
Stphane€

With set +o multibyte:

$ set +o multibyte
$ print -r -- ${a//(#m)?/[$MATCH]}
[S][t][�][�][p][h][a][n][e][�][�][�]

Where ? matches on each byte of those multi-byte characters.

The doc has:

> U
>     All characters are considered to be a single byte long.  The
>     opposite of u.  This overrides the MULTIBYTE option.

which is a bit ambiguous and may be interpreted as justifying
the current behaviour.

But I suspect that's because when ${var//pattern/replace}
resumes searching for the next pattern after the first one, it
starts at the next character instead of next byte, and the (#U)
applies to pattern matching but not to the
${var//pattern/replace} operator itself.

Thanks
Stephane


             reply	other threads:[~2022-02-06  8:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-06  8:42 Stephane Chazelas [this message]
     [not found] ` <1071890479.577225.1644233454174@mail2.virginmedia.com>
2022-02-07 12:15   ` Peter Stephenson
2022-02-07 12:24     ` Stephane Chazelas
2022-02-07 12:59     ` Peter Stephenson

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=20220206084255.tn3dgitvpr7qdjig@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).