Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to pv_uni_display for better tr/// output #22930

Open
wants to merge 1 commit into
base: blead
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -4748,7 +4748,13 @@ See also L</sv_uni_display>.
=for apidoc Amnh||UNI_DISPLAY_QQ
=for apidoc Amnh||UNI_DISPLAY_REGEX
=cut

Undocumented is UNI_DISPLAY_TR_ which is used internally to display an operand
of the tr/// operation. These operands have a peculiar, deliberate UTF-8
malformation which this flag enables the proper handling of. It turns on
ISPRINT and BACKSLASH as well.
*/

char *
Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim,
UV flags)
Expand All @@ -4770,6 +4776,14 @@ Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim,
break;
}

/* The minus is unambiguously the range indicator within a UTF-8 tr///
* operand */
if (UNLIKELY(flags & UNI_DISPLAY_TR_ && *s == ILLEGAL_UTF8_BYTE)) {
sv_catpvs(dsv, "-");
next_len = 1;
continue;
}

u = utf8_to_uvchr_buf(s, e, &next_len);
assert(next_len > 0);

Expand Down
3 changes: 3 additions & 0 deletions utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,9 @@ point's representation.
#define UNI_DISPLAY_BACKSLASH 0x0002
#define UNI_DISPLAY_BACKSPACE 0x0004 /* Allow \b when also
UNI_DISPLAY_BACKSLASH */
#define UNI_DISPLAY_TR_ ( 0x0008 \
|UNI_DISPLAY_ISPRINT \
|UNI_DISPLAY_BACKSLASH)
#define UNI_DISPLAY_QQ (UNI_DISPLAY_ISPRINT \
|UNI_DISPLAY_BACKSLASH \
|UNI_DISPLAY_BACKSPACE)
Expand Down
Loading