Skip to content

Commit 6ceb408

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 3d2e1f3 commit 6ceb408

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
@@ -4761,8 +4761,18 @@ See also L</sv_uni_display>.
47614761
=for apidoc Amnh||UNI_DISPLAY_ISPRINT
47624762
=for apidoc Amnh||UNI_DISPLAY_QQ
47634763
=for apidoc Amnh||UNI_DISPLAY_REGEX
4764+
4765+
=for apidoc Cmn||UNI_DISPLAY_TR
4766+
4767+
This is an extra flag for L<perlapi/sv_uni_display> which is for internal use
4768+
only. It displays an operand of the tr/// operation. These operands have a
4769+
peculiar, deliberate UTF-8 malformation which this flag enables the proper
4770+
handling of. It turns on ISPRINT and BACKSLASH as well.
4771+
47644772
=cut
4773+
47654774
*/
4775+
47664776
char *
47674777
Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim,
47684778
UV flags)
@@ -4784,6 +4794,14 @@ Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim,
47844794
break;
47854795
}
47864796

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

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)