This commit is contained in:
2022-04-26 20:04:10 +03:00

View File

@@ -27,7 +27,9 @@
#ifdef WINDOWS
# include <stringapiset.h>
#endif
#include <wchar.h>
#include <string>
#include <locale>
#include <codecvt>
//! \addtogroup Core
//! \{
@@ -201,55 +203,29 @@ 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);
enlarge(s);
// UChar * ucs = new UChar[s];
// memset(ucs, 0, s * sizeof(UChar));
e = (UErrorCode)0;
int sz = ucnv_toUChars(cc, (UChar*)(PIDeque<PIChar>::data(old_sz)), s, c, s, &e);
//printf("appendFromChars %d -> %d\n", s, sz);
//printf("PIString %d -> %d\n", c[0], ucs[0]);
resize(old_sz+sz);
// for (int i = 0; i < sz; ++i) {
// push_back(PIChar((ushort)ucs[i]));
// }
// delete[] ucs;
ucnv_close(cc);
return;
}
#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;
enlarge(sz);
MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, (LPWSTR)PIDeque<PIChar>::data(old_sz), sz);
return;
//printf("request %d\n", sz);
# 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_);
enlarge(len);
for (size_t i=0; i<len; ++i) (*this)[old_sz+i] = wstr[i];
// //qDebug() << "FromChars done" << size();
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
std::u16string ucs2 = ucs2conv.from_bytes(c, c+s);
enlarge(ucs2.size());
ucs2.copy((char16_t *)PIDeque<PIChar>::data(old_sz), ucs2.size());
# endif
#endif
}
@@ -348,21 +324,8 @@ void PIString::buildData(const char * cp) const {
const size_t len = MB_CUR_MAX*size()+1;
data_ = (char *)malloc(len);
sz = ucnv_fromUChars(cc, data_, len, (const UChar*)(PIDeque<PIChar>::data()), size_s(), &e);
// char uc[8];
// data_.reserve(size_s());
// for (int i = 0; i < size_s(); ++i) {
// if (at(i).isAscii()) {
// data_.push_back(uchar(at(i).unicode16Code()));
// } else {
// e = (UErrorCode)0;
// for (int j = 0; j < sz; ++j) {
// data_.push_back(uc[j]);
// }
// }
// }
ucnv_close(cc);
data_[sz] = '\0';
// data_.push_back('\0');
return;
}
#else
@@ -381,23 +344,10 @@ void PIString::buildData(const char * cp) const {
data_[sz] = '\0';
return;
# else
wchar_t wc;
//char tc[MB_CUR_MAX];
mbstate_t state;
memset(&state, 0, sizeof(state));
data_ = (char *)malloc(MB_CUR_MAX*size()+1);
char *p = data_;
for (int i = 0; i < size_s(); ++i) {
// if (at(i).isAscii()) {
// data_.push_back(uchar(at(i).toAscii()));
// continue;
// }
wc = at(i).toWChar();
sz = wcrtomb(p, wc, &state);
if (sz < 0) break;
p += sz;
}
p[0] = '\0';
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
std::string u8str = ucs2conv.to_bytes((char16_t*)PIDeque<PIChar>::data(), (char16_t*)PIDeque<PIChar>::data()+size());
data_ = (char *)malloc(u8str.size()+1);
strcpy(data_, u8str.c_str());
# endif
#endif
}