Skip to content

Commit

Permalink
cygwin: Convert to use utf8_to_utf16()
Browse files Browse the repository at this point in the history
This now-core function simplifies things, and removes the need for
changing the locale.
  • Loading branch information
khwilliamson committed Jan 20, 2023
1 parent c2e4572 commit 0845591
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions cygwin/cygwin.c
Expand Up @@ -179,23 +179,18 @@ wchar_t*
utf8_to_wide(const char *buf)
{
dTHX;
wchar_t *wsrc;
mbstate_t mbs;
char *oldlocale;
int wlen = sizeof(wchar_t)*strlen(buf);
Size_t len = strlen(buf) + 1;

SETLOCALE_LOCK;
/* Max expansion factor is sizeof(wchar_t) */
Size_t wlen = sizeof(wchar_t) * len;

oldlocale = setlocale(LC_CTYPE, NULL);
wchar_t* wsrc = (wchar_t *) safemalloc(wlen);

setlocale(LC_CTYPE, "utf-8");
wsrc = (wchar_t *) safemalloc(wlen);
wlen = mbsrtowcs(wsrc, (const char**)&buf, wlen, &mbs);
utf8_to_utf16(buf, (U8 *) wsrc, len, &wlen);

if (oldlocale) setlocale(LC_CTYPE, oldlocale);
else setlocale(LC_CTYPE, "C");
return wsrc;
}

SETLOCALE_UNLOCK;

return wsrc;
}
Expand Down

0 comments on commit 0845591

Please sign in to comment.