diff --git a/libs/main/core/pistring.cpp b/libs/main/core/pistring.cpp index 36a4ea5e..2448e0ed 100644 --- a/libs/main/core/pistring.cpp +++ b/libs/main/core/pistring.cpp @@ -27,7 +27,9 @@ #ifdef WINDOWS # include #endif -#include +#include +#include +#include //! \addtogroup Core //! \{ @@ -197,11 +199,11 @@ llong PIString::toNumberBase(const PIString & value, int base, bool * ok) { void PIString::appendFromChars(const char * c, int s, const char * codepage) { if (s == 0) return; int old_sz = size_s(); + if (s == -1) s = strlen(c); #ifdef PIP_ICU UErrorCode e((UErrorCode)0); UConverter * cc = ucnv_open(codepage, &e); if (cc) { - if (s == -1) s = strlen(c); d.enlarge(s); e = (UErrorCode)0; int sz = ucnv_toUChars(cc, (UChar*)(d.data(old_sz)), s, c, s, &e); @@ -211,31 +213,15 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) { } #else # ifdef WINDOWS - if (s == -1) s = strlen(c); int sz = MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, 0, 0); if (sz <= 0) return; d.enlarge(sz); MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, (LPWSTR)d.data(old_sz), sz); - return; # else - mbstate_t state; - memset(&state, 0, sizeof(state)); - const char ** pc; - char * c_ = nullptr; - if (s > 0) { - c_ = (char*)malloc(s+1); - memcpy(c_, c, s); - c_[s] = '\0'; - pc = (const char **)&c_; - } else { - pc = &c; - } - size_t len = mbsrtowcs(NULL, pc, 0, &state); - wchar_t wstr[len+1]; - mbsrtowcs(&wstr[0], pc, len+1, &state); - if (c_) free(c_); - d.enlarge(len); - for (size_t i=0; i, char16_t> ucs2conv; + std::u16string ucs2 = ucs2conv.from_bytes(c, c+s); + enlarge(ucs2.size()); + ucs2.copy((char16_t *)PIDeque::data(old_sz), ucs2.size()); # endif #endif } @@ -350,18 +336,10 @@ void PIString::buildData(const char * cp) const { data_[sz] = '\0'; return; # else - wchar_t wc; - mbstate_t state; - memset(&state, 0, sizeof(state)); - data_ = (char *)malloc(MB_CUR_MAX*size()+1); - char *p = data_; - for (int i = 0; i < d.size_s(); ++i) { - wc = at(i).toWChar(); - sz = wcrtomb(p, wc, &state); - if (sz < 0) break; - p += sz; - } - p[0] = '\0'; + std::wstring_convert, char16_t> ucs2conv; + std::string u8str = ucs2conv.to_bytes((char16_t*)PIDeque::data(), (char16_t*)PIDeque::data()+size()); + data_ = (char *)malloc(u8str.size()+1); + strcpy(data_, u8str.c_str()); # endif #endif }