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

clarification on (#U) in pattern matching.



$ 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