PIString replace pibytearray by char *
This commit is contained in:
@@ -28,12 +28,6 @@
|
||||
# include <stringapiset.h>
|
||||
#endif
|
||||
#include <wchar.h>
|
||||
#ifdef ANDROID
|
||||
# if __ANDROID_API__ < 21
|
||||
# define wctomb(s, wc) wcrtomb(s, wc, NULL)
|
||||
# define mbtowc(pwc, s, n) mbrtowc(pwc, s, n, NULL)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
//! \addtogroup Core
|
||||
//! \{
|
||||
@@ -88,6 +82,8 @@ const float PIString::ElideLeft = 0.f;
|
||||
const float PIString::ElideCenter = .5f;
|
||||
const float PIString::ElideRight = 1.f;
|
||||
|
||||
static const char * _PIString_empty_cc = "";
|
||||
|
||||
|
||||
#ifndef CC_VC
|
||||
# define pisprintf(f, v) char ch[256]; memset(ch, 0, 256); sprintf(ch, f, v); return PIString(ch);
|
||||
@@ -235,14 +231,15 @@ 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));
|
||||
wchar_t wc;
|
||||
mbtowc(0,0,0); // reset mbtowc
|
||||
//qDebug() << "FromChars ...";
|
||||
while (s>0) {
|
||||
while (sz = mbrtowc(&wc, c, s, &state) > 0) {
|
||||
//qDebug() << "0" << s;
|
||||
sz = mbtowc(&wc, c, s);
|
||||
// sz = mbrtowc(&wc, c, s, &state);
|
||||
//qDebug() << "1" << sz;
|
||||
if (sz < 1) break;
|
||||
// if (sz < 1) break;
|
||||
push_back(PIChar(wc));
|
||||
c += sz; s -= sz;
|
||||
//qDebug() << "2" << c;
|
||||
@@ -254,37 +251,30 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
|
||||
|
||||
PIString PIString::fromConsole(const char * s) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l, __sysoemname__);
|
||||
if (s[0] != '\0') ret.appendFromChars(s, strlen(s), __sysoemname__);
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromSystem(const char * s) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l, __syslocname__);
|
||||
if (s[0] != '\0') ret.appendFromChars(s, strlen(s), __syslocname__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromUTF8(const char * s) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l, __utf8name__);
|
||||
if (s[0] != '\0') ret.appendFromChars(s, strlen(s), __utf8name__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromUTF8(const PIByteArray & ba) {
|
||||
PIString ret;
|
||||
if (ba.isEmpty()) return ret;
|
||||
ret.appendFromChars((const char*)ba.data(), ba.size(), __utf8name__);
|
||||
if (ba.isNotEmpty()) ret.appendFromChars((const char*)ba.data(), ba.size(), __utf8name__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -296,19 +286,17 @@ PIString PIString::fromAscii(const char * s) {
|
||||
|
||||
PIString PIString::fromAscii(const char * s, int len) {
|
||||
PIString ret;
|
||||
ret.reserve(len);
|
||||
ret.resize(len);
|
||||
for (int l = 0; l < len; ++l) {
|
||||
ret.push_back(PIChar(s[l]));
|
||||
ret[l] = s[l];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromCodepage(const char * s, const char * c) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l
|
||||
if (s[0] > '\0') ret.appendFromChars(s, strlen(s)
|
||||
#ifdef PIP_ICU
|
||||
, c
|
||||
#else
|
||||
@@ -342,7 +330,11 @@ PIString PIString::readableSize(llong bytes) {
|
||||
|
||||
|
||||
void PIString::buildData(const char * cp) const {
|
||||
data_.clear();
|
||||
//data_.clear();
|
||||
if (data_) {
|
||||
free(data_);
|
||||
data_ = nullptr;
|
||||
}
|
||||
int sz = 0;
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
@@ -371,29 +363,33 @@ void PIString::buildData(const char * cp) const {
|
||||
//printf("WideCharToMultiByte %d %d\n", (uint)(uintptr_t)cp, sz);
|
||||
if (sz <= 0) {
|
||||
//printf("WideCharToMultiByte erro %d\n", GetLastError());
|
||||
data_.push_back(uchar('\0'));
|
||||
data_ = (char *)malloc(1);
|
||||
data_[0] = '\0';
|
||||
return;
|
||||
}
|
||||
data_.resize(sz);
|
||||
WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque<PIChar>::data(), PIDeque<PIChar>::size_s(), (LPSTR)data_.data(), data_.size_s(), NULL, NULL);
|
||||
data_.push_back(uchar('\0'));
|
||||
data_ = (char *)malloc(sz+1);
|
||||
//data_.resize(sz);
|
||||
WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque<PIChar>::data(), PIDeque<PIChar>::size_s(), (LPSTR)data_, sz, NULL, NULL);
|
||||
data_[sz] = '\0';
|
||||
return;
|
||||
# else
|
||||
wchar_t wc;
|
||||
char tc[8];
|
||||
wctomb(0, 0);
|
||||
//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;
|
||||
}
|
||||
// if (at(i).isAscii()) {
|
||||
// data_.push_back(uchar(at(i).toAscii()));
|
||||
// continue;
|
||||
// }
|
||||
wc = at(i).toWChar();
|
||||
sz = wctomb(tc, wc);
|
||||
for (int b = 0; b < sz; ++b) {
|
||||
data_.push_back(uchar(tc[b]));
|
||||
}
|
||||
sz = wcrtomb(p, wc, &state);
|
||||
if (sz < 0) break;
|
||||
p += sz;
|
||||
}
|
||||
data_.push_back(uchar('\0'));
|
||||
p[0] = '\0';
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
@@ -422,14 +418,14 @@ uint PIString::hash() const {
|
||||
|
||||
|
||||
PIByteArray PIString::toUTF8() const {
|
||||
if (isEmpty()) return data_.resized(0);
|
||||
if (isEmpty()) return PIByteArray(1,'\0');
|
||||
buildData(__utf8name__);
|
||||
return data_.resized(data_.size_s() - 1);
|
||||
return PIByteArray(data_, strlen(data_));
|
||||
}
|
||||
|
||||
|
||||
PIByteArray PIString::toCharset(const char * c) const {
|
||||
if (isEmpty()) return data_.resized(0);
|
||||
if (isEmpty()) return PIByteArray(1,'\0');
|
||||
buildData(
|
||||
#ifdef PIP_ICU
|
||||
c
|
||||
@@ -439,19 +435,20 @@ PIByteArray PIString::toCharset(const char * c) const {
|
||||
# endif
|
||||
#endif
|
||||
);
|
||||
return data_.resized(data_.size_s() - 1);
|
||||
|
||||
return PIByteArray(data_, strlen(data_));
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const char * str) {
|
||||
if (!str) return *this;
|
||||
int l = 0;
|
||||
while (str[l] != '\0') ++l;
|
||||
appendFromChars(str, l, __syslocname__);
|
||||
appendFromChars(str, strlen(str), __syslocname__);
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIString::~PIString() {
|
||||
if(data_) free(data_);
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const wchar_t * str) {
|
||||
if (!str) return *this;
|
||||
@@ -1350,20 +1347,6 @@ PIString PIString::inBrackets(const PIChar start, const PIChar end) const {
|
||||
}
|
||||
|
||||
|
||||
//! \~\details
|
||||
//! \~english It`s equivalent length of char sequence returned by function \a data() - 1, without terminating null-char
|
||||
//! \~russian Эквивалентно длине данных, возвращаемых \a data() - 1, без завершающего нулевого байта
|
||||
//! \~\code
|
||||
//! piCout << PIString("0123456789").lengthAscii(); // 10
|
||||
//! piCout << PIString("№1").lengthAscii(); // 3
|
||||
//! \endcode
|
||||
//! \~\sa \a data()
|
||||
int PIString::lengthAscii() const {
|
||||
buildData(__syslocname__);
|
||||
return data_.size_s() - 1;
|
||||
}
|
||||
|
||||
|
||||
//! \~\details
|
||||
//! \~english
|
||||
//! This function fill internal buffer by sequence
|
||||
@@ -1380,8 +1363,9 @@ int PIString::lengthAscii() const {
|
||||
//! \endcode
|
||||
//! \~\sa \a dataConsole(), \a dataUTF8()
|
||||
const char * PIString::data() const {
|
||||
if (isEmpty()) return _PIString_empty_cc;
|
||||
buildData(__syslocname__);
|
||||
return (const char *)(data_.data());
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
@@ -1397,8 +1381,9 @@ const char * PIString::data() const {
|
||||
//! действителен до следующего вызова этого метода
|
||||
//! \~\sa \a data(), \a dataUTF8()
|
||||
const char * PIString::dataConsole() const {
|
||||
if (isEmpty()) return _PIString_empty_cc;
|
||||
buildData(__sysoemname__ );
|
||||
return (const char *)(data_.data());
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
@@ -1414,8 +1399,9 @@ const char * PIString::dataConsole() const {
|
||||
//! действителен до следующего вызова этого метода
|
||||
//! \~\sa \a data(), \a dataConsole()
|
||||
const char * PIString::dataUTF8() const {
|
||||
if (isEmpty()) return _PIString_empty_cc;
|
||||
buildData(__utf8name__);
|
||||
return (const char *)(data_.data());
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
@@ -1431,12 +1417,17 @@ const char * PIString::dataUTF8() const {
|
||||
//! действителен до следующего вызова этого метода
|
||||
//! \~\sa \a dataConsole(), \a dataUTF8()
|
||||
const char * PIString::dataAscii() const {
|
||||
data_.clear();
|
||||
for (int i = 0; i < size_s(); ++i) {
|
||||
data_.push_back(uchar(at(i).ch));
|
||||
if (isEmpty()) return _PIString_empty_cc;
|
||||
if (data_) {
|
||||
free(data_);
|
||||
data_ = nullptr;
|
||||
}
|
||||
data_.push_back(uchar('\0'));
|
||||
return (const char *)data_.data();
|
||||
data_ = (char*)malloc(size()+1);
|
||||
for (int i = 0; i < size_s(); ++i) {
|
||||
data_[i] = uchar(at(i).ch);
|
||||
}
|
||||
data_[size()] = '\0';
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user