Skip to content

Commit 1b767c1

Browse files
committed
utf8_to_bytes_: Add ability to return a mortalized pv
This is a non-destructive conversion of the input into native bytes, and with any new memory required set for destruction via SAVEFREEPV. This allows the caller to not have to be concerned at all if memory was created or not. A new macro is created that calls this internal function with the correct parameter to force this behavior.
1 parent 8c15ff3 commit 1b767c1

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

embed.fnc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3709,6 +3709,9 @@ Admp |bool |utf8_to_bytes_new_pv \
37093709
Admp |bool |utf8_to_bytes_overwrite \
37103710
|NN U8 **s_ptr \
37113711
|NN STRLEN *lenp
3712+
Admp |bool |utf8_to_bytes_temp_pv \
3713+
|NN U8 const **s_ptr \
3714+
|NN STRLEN *lenp
37123715
EMXp |U8 * |utf16_to_utf8 |NN U8 *p \
37133716
|NN U8 *d \
37143717
|Size_t bytelen \

embed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@
862862
# define utf8_to_bytes_(a,b,c,d) Perl_utf8_to_bytes_(aTHX_ a,b,c,d)
863863
# define utf8_to_bytes_new_pv(a,b,c) Perl_utf8_to_bytes_new_pv(aTHX,a,b,c)
864864
# define utf8_to_bytes_overwrite(a,b) Perl_utf8_to_bytes_overwrite(aTHX,a,b)
865+
# define utf8_to_bytes_temp_pv(a,b) Perl_utf8_to_bytes_temp_pv(aTHX,a,b)
865866
# define utf8_to_uvchr_buf_helper(a,b,c) Perl_utf8_to_uvchr_buf_helper(aTHX_ a,b,c)
866867
# define utf8n_to_uvchr_msgs Perl_utf8n_to_uvchr_msgs
867868
# define uvchr_to_utf8(a,b) Perl_uvchr_to_utf8(aTHX,a,b)

proto.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utf8.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,6 @@ Perl_utf8_to_bytes_(pTHX_ U8 **s_ptr, STRLEN *lenp, U8 ** free_me,
23822382
Perl_utf8_to_bytes_arg result_as)
23832383
{
23842384
PERL_ARGS_ASSERT_UTF8_TO_BYTES_;
2385-
PERL_UNUSED_CONTEXT;
23862385

23872386
if (result_as == PL_utf8_to_bytes_new_memory) {
23882387
*free_me = NULL;
@@ -2574,7 +2573,13 @@ Perl_utf8_to_bytes_(pTHX_ U8 **s_ptr, STRLEN *lenp, U8 ** free_me,
25742573
*lenp = d - d0;
25752574

25762575
if (result_as != PL_utf8_to_bytes_overwrite) {
2577-
*s_ptr = *free_me = d0;
2576+
*s_ptr = d0;
2577+
if (result_as == PL_utf8_to_bytes_use_temporary) {
2578+
SAVEFREEPV(*s_ptr);
2579+
}
2580+
else {
2581+
*free_me = *s_ptr;
2582+
}
25782583
}
25792584

25802585
return true;

utf8.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,9 @@ typedef enum {
13211321
#define Perl_utf8_to_bytes_new_pv(mTHX, s, l, f) \
13221322
Perl_utf8_to_bytes_(aTHX_ (U8 **) s, l, f, \
13231323
PL_utf8_to_bytes_new_memory)
1324+
#define Perl_utf8_to_bytes_temp_pv(mTHX, s, l) \
1325+
Perl_utf8_to_bytes_(aTHX_ (U8 **) s, l, INT2PTR(U8 **, 1), \
1326+
PL_utf8_to_bytes_use_temporary)
13241327

13251328
/* Do not use; should be deprecated. Use isUTF8_CHAR() instead; this is
13261329
* retained solely for backwards compatibility */

0 commit comments

Comments
 (0)