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 #ifdef WINDOWS
# include <stringapiset.h> # include <stringapiset.h>
#endif #endif
#include <wchar.h> #include <cuchar>
//! \addtogroup Core //! \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) { 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);
enlarge(s); enlarge(s);
// UChar * ucs = new UChar[s]; // UChar * ucs = new UChar[s];
// memset(ucs, 0, s * sizeof(UChar)); // memset(ucs, 0, s * sizeof(UChar));
@@ -223,7 +223,6 @@ 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;
enlarge(sz); enlarge(sz);
@@ -231,25 +230,37 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
return; return;
//printf("request %d\n", sz); //printf("request %d\n", sz);
# else # else
mbstate_t state; std::mbstate_t state{};
memset(&state, 0, sizeof(state)); PIChar c16;
const char ** pc; int sz = 0;
char * c_ = nullptr; while(std::size_t rc = std::mbrtoc16(&c16, c+sz, s - sz, &state)) {
if (s > 0) { if(rc == (std::size_t)-3)
c_ = (char*)malloc(s+1); d.append(c16);
memcpy(c_, c, s); else if(rc == (std::size_t)-2)
c_[s] = '\0'; break;
pc = (const char **)&c_; else if(rc == (std::size_t)-1)
} else { break;
pc = &c; else {
sz += rc;
d.append(c16);
} }
size_t len = mbsrtowcs(NULL, pc, 0, &state); }
wchar_t wstr[len+1]; // const char ** pc;
mbsrtowcs(&wstr[0], pc, len+1, &state); // char * c_ = nullptr;
if (c_) free(c_); // if (s > 0) {
enlarge(len); // c_ = (char*)malloc(s+1);
for (size_t i=0; i<len; ++i) (*this)[old_sz+i] = wstr[i]; // memcpy(c_, c, s);
// //qDebug() << "FromChars done" << size(); // 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
} }