From bd9ad160749bdfd3ecd8f6c439ec0bb5afd9fb94 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 25 Apr 2022 23:31:46 +0300 Subject: [PATCH] linux PIString fix --- libs/main/core/pistring.cpp | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/libs/main/core/pistring.cpp b/libs/main/core/pistring.cpp index c1cc5212..a1147d71 100644 --- a/libs/main/core/pistring.cpp +++ b/libs/main/core/pistring.cpp @@ -199,21 +199,21 @@ 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; + if (s == 0) return; int old_sz = size_s(); - int sz; #ifdef PIP_ICU UErrorCode e((UErrorCode)0); UConverter * cc = ucnv_open(codepage, &e); if (cc) { + if (s == -1) s = strlen(s); enlarge(s); // UChar * ucs = new UChar[s]; // memset(ucs, 0, s * sizeof(UChar)); e = (UErrorCode)0; - sz = ucnv_toUChars(cc, (UChar*)(PIDeque::data(old_sz)), s, c, s, &e); + int sz = ucnv_toUChars(cc, (UChar*)(PIDeque::data(old_sz)), s, c, s, &e); //printf("appendFromChars %d -> %d\n", s, sz); //printf("PIString %d -> %d\n", c[0], ucs[0]); - resize(sz+sz); + resize(old_sz+sz); // for (int i = 0; i < sz; ++i) { // push_back(PIChar((ushort)ucs[i])); // } @@ -223,7 +223,8 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) { } #else # ifdef WINDOWS - sz = MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, 0, 0); + if (s == -1) s = strlen(s); + int sz = MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, 0, 0); if (sz <= 0) return; enlarge(sz); MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, (LPWSTR)PIDeque::data(old_sz), sz); @@ -232,9 +233,18 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) { # else mbstate_t state; memset(&state, 0, sizeof(state)); - size_t len = mbsrtowcs(NULL, &c, 0, &state); + char * pc; + if (s > 0) { + char c_ [s+1]; + memcpy(c_, c, s); + c_[s] = '\0'; + pc = c_; + } else { + pc = c; + } + size_t len = mbsrtowcs(NULL, &pc, 0, &state); wchar_t wstr[len+1]; - mbsrtowcs(&wstr[0], &c, len+1, &state); + mbsrtowcs(&wstr[0], &pc, len+1, &state); enlarge(len); for (size_t i=0; i '\0') ret.appendFromChars(s, strlen(s) + if (s[0] > '\0') ret.appendFromChars(s, -1 #ifdef PIP_ICU , c #else @@ -444,7 +454,7 @@ PIByteArray PIString::toCharset(const char * c) const { PIString & PIString::operator +=(const char * str) { if (!str) return *this; - appendFromChars(str, strlen(str), __syslocname__); + appendFromChars(str, -1, __syslocname__); return *this; }