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 Sep 13, 2021
1 parent 6ecdea3 commit 902f68c
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions cygwin/cygwin.c
Expand Up @@ -177,23 +177,18 @@ wide_to_utf8(const wchar_t *wbuf)
wchar_t*
utf8_to_wide(const char *buf)
{
wchar_t *wbuf;
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* wbuf = (wchar_t *) safemalloc(wlen);

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

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

SETLOCALE_UNLOCK;

return wbuf;
}
Expand Down

0 comments on commit 902f68c

Please sign in to comment.