summaryrefslogtreecommitdiff
path: root/locale.c
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2022-10-04 05:04:32 -0600
committerKarl Williamson <khw@cpan.org>2022-10-10 13:12:22 -0600
commit3d3bea996b3b73e474a80a4fde19459ea89dbeff (patch)
tree3b20cd0aa2f2ebd1a3a01350001049d8f5cd17b1 /locale.c
parente88d6362ca2333867768f8bd4b668b12545a87bf (diff)
downloadperl-3d3bea996b3b73e474a80a4fde19459ea89dbeff.tar.gz
locale.c: Generalize static functions
This changes these functions to take the code page as input, instead of being just UTF-8. Macros are created to call them with UTF-8. I'm doing this because there is no loss of efficiency, and it is somewhat jarring, given Perl terminology, to call a function with 'Byte' in the name with a parameter with 'utf8' in the name.
Diffstat (limited to 'locale.c')
-rw-r--r--locale.c24
1 files changed, 14 insertions, 10 deletions
diff --git a/locale.c b/locale.c
index 9fefcc183f..b2f3077063 100644
--- a/locale.c
+++ b/locale.c
@@ -2495,11 +2495,11 @@ S_new_collate(pTHX_ const char *newcoll)
#ifdef WIN32
wchar_t *
-S_Win_utf8_string_to_wstring(const char * utf8_string)
+S_Win_byte_string_to_wstring(const UINT code_page, const char * byte_string)
{
wchar_t *wstring;
- int req_size = MultiByteToWideChar(CP_UTF8, 0, utf8_string, -1, NULL, 0);
+ int req_size = MultiByteToWideChar(code_page, 0, byte_string, -1, NULL, 0);
if (! req_size) {
errno = EINVAL;
return NULL;
@@ -2507,7 +2507,7 @@ S_Win_utf8_string_to_wstring(const char * utf8_string)
Newx(wstring, req_size, wchar_t);
- if (! MultiByteToWideChar(CP_UTF8, 0, utf8_string, -1, wstring, req_size))
+ if (! MultiByteToWideChar(code_page, 0, byte_string, -1, wstring, req_size))
{
Safefree(wstring);
errno = EINVAL;
@@ -2517,27 +2517,31 @@ S_Win_utf8_string_to_wstring(const char * utf8_string)
return wstring;
}
+#define Win_utf8_string_to_wstring(s) Win_byte_string_to_wstring(CP_UTF8, (s))
+
char *
-S_Win_wstring_to_utf8_string(const wchar_t * wstring)
+S_Win_wstring_to_byte_string(const UINT code_page, const wchar_t * wstring)
{
- char *utf8_string;
int req_size =
- WideCharToMultiByte(CP_UTF8, 0, wstring, -1, NULL, 0, NULL, NULL);
+ WideCharToMultiByte(code_page, 0, wstring, -1, NULL, 0, NULL, NULL);
- Newx(utf8_string, req_size, char);
+ char *byte_string;
+ Newx(byte_string, req_size, char);
- if (! WideCharToMultiByte(CP_UTF8, 0, wstring, -1, utf8_string,
+ if (! WideCharToMultiByte(code_page, 0, wstring, -1, byte_string,
req_size, NULL, NULL))
{
- Safefree(utf8_string);
+ Safefree(byte_string);
errno = EINVAL;
return NULL;
}
- return utf8_string;
+ return byte_string;
}
+#define Win_wstring_to_utf8_string(ws) Win_wstring_to_byte_string(CP_UTF8, (ws))
+
STATIC char *
S_wrap_wsetlocale(pTHX_ int category, const char *locale) {
PERL_ARGS_ASSERT_WRAP_WSETLOCALE;