Skip to content

Commit

Permalink
do_print: Use new utfj8_to_bytes_temp_pv()
Browse files Browse the repository at this point in the history
  • Loading branch information
khwilliamson committed Dec 3, 2024
1 parent 9c2eed8 commit 329ae31
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions doio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2215,35 +2215,23 @@ Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
STRLEN len;
/* Do this first to trigger any overloading. */
const char *tmps = SvPV_const(sv, len);
U8 *tmpbuf = NULL;
bool happy = TRUE;

if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
/* We don't modify the original scalar. */
tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
tmps = (char *) tmpbuf;
tmps = (char *) bytes_to_utf8((const U8*) tmps, &len);
SAVEFREEPV(tmps);
}
else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
(void) check_utf8_print((const U8*) tmps, len);
}
} /* else stream isn't utf8 */
else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
convert to bytes */
STRLEN tmplen = len;
bool utf8 = TRUE;
U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
if (!utf8) {

/* Here, succeeded in downgrading from utf8. Set up to below
* output the converted value */
tmpbuf = result;
tmps = (char *) tmpbuf;
len = tmplen;
}
else { /* Non-utf8 output stream, but string only representable in
utf8 */
assert((char *)result == tmps);
if (! utf8_to_bytes_temp_pv((const U8**) &tmps, &len)) {
/* Non-utf8 output stream, but string only representable in
utf8 */
Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
"Wide character in %s",
PL_op ? OP_DESC(PL_op) : "print"
Expand All @@ -2261,7 +2249,6 @@ Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
* io the write failure can be delayed until the flush/close. --jhi */
if (len && (PerlIO_write(fp,tmps,len) == 0))
happy = FALSE;
Safefree(tmpbuf);
return happy ? !PerlIO_error(fp) : FALSE;
}
}
Expand Down

0 comments on commit 329ae31

Please sign in to comment.