linux mb test

This commit is contained in:
Andrey
2022-04-26 16:30:26 +03:00
parent 69b9589e84
commit 06c8e6af10

View File

@@ -27,7 +27,9 @@
#ifdef WINDOWS
# include <stringapiset.h>
#endif
#include <cuchar>
#include <string>
#include <locale>
#include <codecvt>
//! \addtogroup Core
//! \{
@@ -207,17 +209,9 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
UConverter * cc = ucnv_open(codepage, &e);
if (cc) {
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;
}
@@ -227,40 +221,11 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
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
std::mbstate_t state{};
PIChar c16;
int sz = 0;
while(std::size_t rc = std::mbrtoc16((char16_t*)&c16, c+sz, s - sz, &state)) {
if(rc == (std::size_t)-3)
append(c16);
else if(rc == (std::size_t)-2)
break;
else if(rc == (std::size_t)-1)
break;
else {
sz += rc;
append(c16);
}
}
// 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]);
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
}