Merge branch 'master' of https://git.shs.tools/SHS/pip into pistring_unparent

This commit is contained in:
Andrey
2022-04-26 16:59:36 +03:00

View File

@@ -27,7 +27,9 @@
#ifdef WINDOWS #ifdef WINDOWS
# include <stringapiset.h> # include <stringapiset.h>
#endif #endif
#include <wchar.h> #include <string>
#include <locale>
#include <codecvt>
//! \addtogroup Core //! \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) { 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 old_sz = size_s();
if (s == -1) s = strlen(c);
#ifdef PIP_ICU #ifdef PIP_ICU
UErrorCode e((UErrorCode)0); UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(codepage, &e); UConverter * cc = ucnv_open(codepage, &e);
if (cc) { if (cc) {
if (s == -1) s = strlen(c);
d.enlarge(s); d.enlarge(s);
e = (UErrorCode)0; e = (UErrorCode)0;
int sz = ucnv_toUChars(cc, (UChar*)(d.data(old_sz)), s, c, s, &e); 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 #else
# ifdef WINDOWS # ifdef WINDOWS
if (s == -1) s = strlen(c);
int sz = MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, 0, 0); int sz = MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, 0, 0);
if (sz <= 0) return; if (sz <= 0) return;
d.enlarge(sz); d.enlarge(sz);
MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, (LPWSTR)d.data(old_sz), sz); MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, (LPWSTR)d.data(old_sz), sz);
return;
# else # else
mbstate_t state; std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
memset(&state, 0, sizeof(state)); std::u16string ucs2 = ucs2conv.from_bytes(c, c+s);
const char ** pc; enlarge(ucs2.size());
char * c_ = nullptr; ucs2.copy((char16_t *)PIDeque<PIChar>::data(old_sz), ucs2.size());
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<len; ++i) d[old_sz+i] = PIChar(wstr[i]);
# endif # endif
#endif #endif
} }
@@ -350,18 +336,10 @@ void PIString::buildData(const char * cp) const {
data_[sz] = '\0'; data_[sz] = '\0';
return; return;
# else # else
wchar_t wc; std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
mbstate_t state; std::string u8str = ucs2conv.to_bytes((char16_t*)PIDeque<PIChar>::data(), (char16_t*)PIDeque<PIChar>::data()+size());
memset(&state, 0, sizeof(state)); data_ = (char *)malloc(u8str.size()+1);
data_ = (char *)malloc(MB_CUR_MAX*size()+1); strcpy(data_, u8str.c_str());
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';
# endif # endif
#endif #endif
} }