Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
clarification on (#U) in pattern matching.
- X-seq: zsh-workers 49743
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: clarification on (#U) in pattern matching.
- Date: Sun, 6 Feb 2022 08:42:55 +0000
- Archived-at: <https://zsh.org/workers/49743>
- List-id: <zsh-workers.zsh.org>
- Mail-followup-to: Zsh hackers list <zsh-workers@xxxxxxx>
$ 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author