Skip to content

Commit 8d6b69c

Browse files
committed
Add option to pv_uni_display for better tr/// output
tr// has a special malformed UTF-8 character as a sentinel; Teach pv_uni_display about that.
1 parent 07457a3 commit 8d6b69c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

utf8.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4759,8 +4759,18 @@ See also L</sv_uni_display>.
47594759
=for apidoc Amnh||UNI_DISPLAY_ISPRINT
47604760
=for apidoc Amnh||UNI_DISPLAY_QQ
47614761
=for apidoc Amnh||UNI_DISPLAY_REGEX
4762+
4763+
=for apidoc Cmn||UNI_DISPLAY_TR
4764+
4765+
This is an extra flag for L<perlapi/sv_uni_display> which is for internal use
4766+
only. It displays an operand of the tr/// operation. These operands have a
4767+
peculiar, deliberate UTF-8 malformation which this flag enables the proper
4768+
handling of. It turns on ISPRINT and BACKSLASH as well.
4769+
47624770
=cut
4771+
47634772
*/
4773+
47644774
char *
47654775
Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim,
47664776
UV flags)
@@ -4782,6 +4792,14 @@ Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim,
47824792
break;
47834793
}
47844794

4795+
/* The minus is unambiguously the range indicator within a UTF-8 tr///
4796+
* operand */
4797+
if (UNLIKELY(flags & UNI_DISPLAY_TR_ && *s == ILLEGAL_UTF8_BYTE)) {
4798+
sv_catpvs(dsv, "-");
4799+
next_len = 1;
4800+
continue;
4801+
}
4802+
47854803
(void) utf8_to_uv(s, e, &u, &next_len);
47864804
assert(next_len > 0);
47874805

utf8.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,10 @@ point's representation.
13391339
#define UNI_DISPLAY_BACKSLASH 0x0002
13401340
#define UNI_DISPLAY_BACKSPACE 0x0004 /* Allow \b when also
13411341
UNI_DISPLAY_BACKSLASH */
1342-
#define UNI_DISPLAY_QQ (UNI_DISPLAY_ISPRINT \
1342+
#define UNI_DISPLAY_TR_ ( 0x0008 \
1343+
|UNI_DISPLAY_ISPRINT \
1344+
|UNI_DISPLAY_BACKSLASH)
1345+
#define UNI_DISPLAY_QQ (UNI_DISPLAY_ISPRINT \
13431346
|UNI_DISPLAY_BACKSLASH \
13441347
|UNI_DISPLAY_BACKSPACE)
13451348

0 commit comments

Comments
 (0)