Rewrote utf16.cpp to use my utf lib. Maybe I should be doing attributed strings safter all, but I might as well optimize too I guess??

This commit is contained in:
Pietro Gagliardi 2016-12-03 11:31:11 -05:00
parent c0f91058c4
commit 0d5ff432b3
3 changed files with 26 additions and 26 deletions

View File

@ -7,6 +7,7 @@ list(APPEND _LIBUI_SOURCES
common/matrix.c common/matrix.c
common/shouldquit.c common/shouldquit.c
common/userbugs.c common/userbugs.c
common/utf.c
) )
set(_LIBUI_SOURCES ${_LIBUI_SOURCES} PARENT_SCOPE) set(_LIBUI_SOURCES ${_LIBUI_SOURCES} PARENT_SCOPE)

View File

@ -1,4 +1,7 @@
// 6 april 2015 // 6 april 2015
// TODO can extern "C"s nest?
#include "utf.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -3,48 +3,42 @@
// see http://stackoverflow.com/a/29556509/3408572 // see http://stackoverflow.com/a/29556509/3408572
#define MBTWC(str, wstr, bufsiz) MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, bufsiz)
WCHAR *toUTF16(const char *str) WCHAR *toUTF16(const char *str)
{ {
WCHAR *wstr; WCHAR *wstr;
int n; WCHAR *wp;
size_t n;
uint32_t rune;
if (*str == '\0') // empty string if (*str == '\0') // empty string
return emptyUTF16(); return emptyUTF16();
n = MBTWC(str, NULL, 0); n = utf8UTF16Count(str, 0);
if (n == 0) { wstr = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]");
logLastError(L"error figuring out number of characters to convert to"); wp = wstr;
return emptyUTF16(); while (*str) {
} str = utf8DecodeRune(str, 0, &rune);
wstr = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]"); n = utf16EncodeRune(rune, wp);
if (MBTWC(str, wstr, n) != n) { wp += n;
logLastError(L"error converting from UTF-8 to UTF-16");
// and return an empty string
*wstr = L'\0';
} }
return wstr; return wstr;
} }
#define WCTMB(wstr, str, bufsiz) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, bufsiz, NULL, NULL)
char *toUTF8(const WCHAR *wstr) char *toUTF8(const WCHAR *wstr)
{ {
char *str; char *str;
int n; char *sp;
size_t n;
uint32_t rune;
if (*wstr == L'\0') // empty string if (*wstr == L'\0') // empty string
return emptyUTF8(); return emptyUTF8();
n = WCTMB(wstr, NULL, 0); n = utf16RuneCount(wstr, 0);
if (n == 0) { str = (char *) uiAlloc((n + 1) * sizeof (char), "char[]");
logLastError(L"error figuring out number of characters to convert to"); sp = str;
return emptyUTF8(); while (*wstr) {
} wstr = utf16DecodeRune(wstr, &rune);
str = (char *) uiAlloc(n * sizeof (char), "char[]"); n = utf8EncodeRune(rune, sp);
if (WCTMB(wstr, str, n) != n) { sp += n;
logLastError(L"error converting from UTF-16 to UTF-8");
// and return an empty string
*str = '\0';
} }
return str; return str;
} }
@ -92,6 +86,8 @@ WCHAR *vstrf(const WCHAR *format, va_list ap)
return buf; return buf;
} }
// TODO merge the following two with the toUTF*()s?
// Let's shove these utility routines here too. // Let's shove these utility routines here too.
// Prerequisite: lfonly is UTF-8. // Prerequisite: lfonly is UTF-8.
char *LFtoCRLF(const char *lfonly) char *LFtoCRLF(const char *lfonly)