linux mb test

This commit is contained in:
Andrey
2022-04-26 16:01:14 +03:00
parent a7ffc85404
commit 5c767c5e3e

View File

@@ -27,7 +27,7 @@
#ifdef WINDOWS
# include <stringapiset.h>
#endif
#include <wchar.h>
#include <cuchar>
//! \addtogroup Core
//! \{
@@ -201,11 +201,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);
enlarge(s);
// UChar * ucs = new UChar[s];
// memset(ucs, 0, s * sizeof(UChar));
@@ -223,7 +223,6 @@ 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;
enlarge(sz);
@@ -231,25 +230,37 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
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;
std::mbstate_t state{};
PIChar c16;
int sz = 0;
while(std::size_t rc = std::mbrtoc16(&c16, c+sz, s - sz, &state)) {
if(rc == (std::size_t)-3)
d.append(c16);
else if(rc == (std::size_t)-2)
break;
else if(rc == (std::size_t)-1)
break;
else {
sz += rc;
d.append(c16);
}
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();
}
// 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<len; ++i) d[old_sz+i] = PIChar(wstr[i]);
# endif
#endif
}