Skip to content

Commit 4c305ff

Browse files
committed
Move code from POSIX.xs to locale.c
This avoids duplicated logic.
1 parent 047ab9f commit 4c305ff

File tree

5 files changed

+75
-61
lines changed

5 files changed

+75
-61
lines changed

embed.fnc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,7 @@ ATdo |const char*|Perl_langinfo|const nl_item item
15981598
#else
15991599
ATdo |const char*|Perl_langinfo|const int item
16001600
#endif
1601+
pEX |int |mbtowc_|NULLOK const wchar_t * pwc|NULLOK const char * s|const Size_t len
16011602
CpO |int |init_i18nl10n |int printwarn
16021603
CbpOD |int |init_i18nl14n |int printwarn
16031604
p |char* |my_strerror |const int errnum

embed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@
952952
#define get_prop_values Perl_get_prop_values
953953
#define grok_atoUV Perl_grok_atoUV
954954
#define load_charnames(a,b,c,d) Perl_load_charnames(aTHX_ a,b,c,d)
955+
#define mbtowc_(a,b,c) Perl_mbtowc_(aTHX_ a,b,c)
955956
#define mg_find_mglob(a) Perl_mg_find_mglob(aTHX_ a)
956957
#define multiconcat_stringify(a) Perl_multiconcat_stringify(aTHX_ a)
957958
#define multideref_stringify(a,b) Perl_multideref_stringify(aTHX_ a,b)

ext/POSIX/POSIX.xs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,30 +3397,18 @@ mblen(s, n = ~0)
33973397
OUTPUT:
33983398
RETVAL
33993399

3400-
#if defined(HAS_MBRTOWC) && (defined(USE_ITHREADS) || ! defined(HAS_MBTOWC))
3401-
# define USE_MBRTOWC
3402-
#else
3403-
# undef USE_MBRTOWC
3404-
#endif
3405-
34063400
int
34073401
mbtowc(pwc, s, n = ~0)
34083402
SV * pwc
34093403
SV * s
34103404
size_t n
34113405
CODE:
3406+
RETVAL = -1;
3407+
#if defined(HAS_MBTOWC) || defined(HAS_MBRTOWC)
34123408
errno = 0;
34133409
SvGETMAGIC(s);
34143410
if (! SvOK(s)) { /* Initialize state */
3415-
#ifdef USE_MBRTOWC
3416-
/* Initialize the shift state to all zeros in PL_mbrtowc_ps. */
3417-
memzero(&PL_mbrtowc_ps, sizeof(PL_mbrtowc_ps));
3418-
RETVAL = 0;
3419-
#else
3420-
MBTOWC_LOCK;
3421-
RETVAL = mbtowc(NULL, NULL, 0);
3422-
MBTOWC_UNLOCK;
3423-
#endif
3411+
mbtowc_(NULL, NULL, 0);
34243412
}
34253413
else { /* Not resetting state */
34263414
wchar_t wc;
@@ -3433,15 +3421,7 @@ mbtowc(pwc, s, n = ~0)
34333421
size_t len;
34343422
char * string = SvPV(byte_s, len);
34353423
if (n < len) len = n;
3436-
#ifdef USE_MBRTOWC
3437-
RETVAL = (SSize_t) mbrtowc(&wc, string, len, &PL_mbrtowc_ps);
3438-
#else
3439-
/* Locking prevents races, but locales can be switched out
3440-
* without locking, so this isn't a cure all */
3441-
MBTOWC_LOCK;
3442-
RETVAL = mbtowc(&wc, string, len);
3443-
MBTOWC_UNLOCK;
3444-
#endif
3424+
RETVAL = mbtowc_(&wc, string, len);
34453425
if (RETVAL >= 0) {
34463426
sv_setiv_mg(pwc, wc);
34473427
}
@@ -3450,6 +3430,7 @@ mbtowc(pwc, s, n = ~0)
34503430
}
34513431
}
34523432
}
3433+
#endif
34533434
OUTPUT:
34543435
RETVAL
34553436

locale.c

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,69 @@ S_save_to_buffer(const char * string, const char **buf, Size_t *buf_size,
26222622
return *buf;
26232623
}
26242624

2625+
int
2626+
Perl_mbtowc_(pTHX_ const wchar_t * pwc, const char * s, const Size_t len)
2627+
{
2628+
2629+
#if ! defined(HAS_MBRTOWC) && ! defined(HAS_MBTOWC)
2630+
2631+
PERL_UNUSED_ARG(pwc);
2632+
PERL_UNUSED_ARG(s);
2633+
PERL_UNUSED_ARG(len);
2634+
return -1;
2635+
2636+
#else /* Below we have some form of mbtowc() */
2637+
# if defined(HAS_MBRTOWC) \
2638+
&& (defined(USE_LOCALE_THREADS) || ! defined(HAS_MBTOWC))
2639+
# define USE_MBRTOWC
2640+
# else
2641+
# undef USE_MBRTOWC
2642+
# endif
2643+
2644+
int retval = -1;
2645+
2646+
if (s == NULL) { /* Initialize the shift state to all zeros in
2647+
PL_mbrtowc_ps. */
2648+
2649+
# if defined(USE_MBRTOWC)
2650+
2651+
memzero(&PL_mbrtowc_ps, sizeof(PL_mbrtowc_ps));
2652+
return 0;
2653+
2654+
# else
2655+
2656+
MBTOWC_LOCK;
2657+
SETERRNO(0, 0);
2658+
retval = mbtowc(NULL, NULL, 0);
2659+
MBTOWC_UNLOCK;
2660+
return retval;
2661+
2662+
# endif
2663+
2664+
}
2665+
2666+
# if defined(USE_MBRTOWC)
2667+
2668+
SETERRNO(0, 0);
2669+
retval = (SSize_t) mbrtowc((wchar_t *) pwc, s, len, &PL_mbrtowc_ps);
2670+
2671+
# else
2672+
2673+
/* Locking prevents races, but locales can be switched out without locking,
2674+
* so this isn't a cure all */
2675+
MBTOWC_LOCK;
2676+
SETERRNO(0, 0);
2677+
retval = mbtowc((wchar_t *) pwc, s, len);
2678+
MBTOWC_UNLOCK;
2679+
2680+
# endif
2681+
2682+
return retval;
2683+
2684+
#endif
2685+
2686+
}
2687+
26252688
/*
26262689
26272690
=for apidoc Perl_langinfo
@@ -4943,47 +5006,13 @@ Perl__is_cur_LC_category_utf8(pTHX_ int category)
49435006
* late adder to C89, so very likely to have it. However, testing has
49445007
* shown that, like nl_langinfo() above, there are locales that are not
49455008
* strictly UTF-8 that this will return that they are */
4946-
49475009
{
49485010
wchar_t wc;
49495011
int len;
4950-
dSAVEDERRNO;
4951-
4952-
# if defined(HAS_MBRTOWC) && defined(USE_LOCALE_THREADS)
4953-
4954-
mbstate_t ps;
49555012

4956-
# endif
4957-
4958-
/* mbrtowc() and mbtowc() convert a byte string to a wide
4959-
* character. Feed a byte string to one of them and check that the
4960-
* result is the expected Unicode code point */
4961-
4962-
# if defined(HAS_MBRTOWC) && defined(USE_LOCALE_THREADS)
4963-
/* Prefer this function if available, as it's reentrant */
4964-
4965-
memzero(&ps, sizeof(ps));;
4966-
PERL_UNUSED_RESULT(mbrtowc(&wc, NULL, 0, &ps)); /* Reset any shift
4967-
state */
4968-
SETERRNO(0, 0);
4969-
len = mbrtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8), &ps);
4970-
SAVE_ERRNO;
4971-
4972-
# else
4973-
4974-
MBTOWC_LOCK;
4975-
PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */
4976-
SETERRNO(0, 0);
4977-
len = mbtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8));
4978-
SAVE_ERRNO;
4979-
MBTOWC_UNLOCK;
4980-
4981-
# endif
4982-
4983-
RESTORE_ERRNO;
4984-
DEBUG_Lv(PerlIO_printf(Perl_debug_log,
4985-
"\treturn from mbtowc; len=%d; code_point=%x; errno=%d\n",
4986-
len, (unsigned int) wc, GET_ERRNO));
5013+
PERL_UNUSED_RESULT(mbtowc_(NULL, NULL, 0));
5014+
len = mbtowc_(&wc, REPLACEMENT_CHARACTER_UTF8,
5015+
STRLENs(REPLACEMENT_CHARACTER_UTF8));
49875016

49885017
is_utf8 = cBOOL( len == STRLENs(REPLACEMENT_CHARACTER_UTF8)
49895018
&& wc == (wchar_t) UNICODE_REPLACEMENT);

proto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,8 @@ PERL_CALLCONV Malloc_t Perl_malloc(MEM_SIZE nbytes)
20222022

20232023
PERL_CALLCONV I32 * Perl_markstack_grow(pTHX);
20242024
#define PERL_ARGS_ASSERT_MARKSTACK_GROW
2025+
PERL_CALLCONV int Perl_mbtowc_(pTHX_ const wchar_t * pwc, const char * s, const Size_t len);
2026+
#define PERL_ARGS_ASSERT_MBTOWC_
20252027
PERL_CALLCONV SV* Perl_mess(pTHX_ const char* pat, ...)
20262028
__attribute__format__(__printf__,pTHX_1,pTHX_2);
20272029
#define PERL_ARGS_ASSERT_MESS \

0 commit comments

Comments
 (0)