PIString wo pideue parent
This commit is contained in:
@@ -83,11 +83,7 @@ const float PIString::ElideCenter = .5f;
|
||||
const float PIString::ElideRight = 1.f;
|
||||
|
||||
|
||||
#ifndef CC_VC
|
||||
# define pisprintf(f, v) char ch[256]; memset(ch, 0, 256); sprintf(ch, f, v); return PIString(ch);
|
||||
#else
|
||||
# define pisprintf(f, v) char ch[256]; memset(ch, 0, 256); sprintf_s(ch, 256, f, v); return PIString(ch);
|
||||
#endif
|
||||
#define pisprintf(f, v) char ch[256]; memset(ch, 0, 256); snprintf(ch, 256, f, v); return PIString(ch);
|
||||
|
||||
PIString PIString::itos(const int num) {pisprintf("%d", num);}
|
||||
PIString PIString::ltos(const long num) {pisprintf("%ld", num);}
|
||||
@@ -206,18 +202,10 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
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));
|
||||
d.enlarge(s);
|
||||
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;
|
||||
int sz = ucnv_toUChars(cc, (UChar*)(d.data(old_sz)), s, c, s, &e);
|
||||
d.resize(old_sz+sz);
|
||||
ucnv_close(cc);
|
||||
return;
|
||||
}
|
||||
@@ -226,10 +214,9 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
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);
|
||||
d.enlarge(sz);
|
||||
MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, (LPWSTR)d.data(old_sz), sz);
|
||||
return;
|
||||
//printf("request %d\n", sz);
|
||||
# else
|
||||
mbstate_t state;
|
||||
memset(&state, 0, sizeof(state));
|
||||
@@ -247,9 +234,8 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
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();
|
||||
d.enlarge(len);
|
||||
for (size_t i=0; i<len; ++i) d[old_sz+i] = PIChar(wstr[i]);
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
@@ -338,7 +324,6 @@ PIString PIString::readableSize(llong bytes) {
|
||||
|
||||
|
||||
void PIString::buildData(const char * cp) const {
|
||||
//data_.clear();
|
||||
deleteData();
|
||||
int sz = 0;
|
||||
#ifdef PIP_ICU
|
||||
@@ -347,51 +332,30 @@ void PIString::buildData(const char * cp) const {
|
||||
if (cc) {
|
||||
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]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
sz = ucnv_fromUChars(cc, data_, len, (const UChar*)(d.data()), d.size_s(), &e);
|
||||
ucnv_close(cc);
|
||||
data_[sz] = '\0';
|
||||
// data_.push_back('\0');
|
||||
return;
|
||||
}
|
||||
#else
|
||||
# ifdef WINDOWS
|
||||
sz = WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque<PIChar>::data(), PIDeque<PIChar>::size_s(), 0, 0, NULL, NULL);
|
||||
//printf("WideCharToMultiByte %d %d\n", (uint)(uintptr_t)cp, sz);
|
||||
sz = WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)d.data(), d.size_s(), 0, 0, NULL, NULL);
|
||||
if (sz <= 0) {
|
||||
//printf("WideCharToMultiByte erro %d\n", GetLastError());
|
||||
data_ = (char *)malloc(1);
|
||||
data_[0] = '\0';
|
||||
return;
|
||||
}
|
||||
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);
|
||||
WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)d.data(), d.size_s(), (LPSTR)data_, sz, NULL, NULL);
|
||||
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;
|
||||
// }
|
||||
for (int i = 0; i < d.size_s(); ++i) {
|
||||
wc = at(i).toWChar();
|
||||
sz = wcrtomb(p, wc, &state);
|
||||
if (sz < 0) break;
|
||||
@@ -411,14 +375,14 @@ void PIString::deleteData() const {
|
||||
|
||||
|
||||
void PIString::trimsubstr(int &st, int &fn) const {
|
||||
for (int i = 0; i < length(); ++i) {
|
||||
for (int i = 0; i < d.size_s(); ++i) {
|
||||
if (at(i) != ' ' && at(i) != '\t' && at(i) != '\n' && at(i) != '\r' && at(i) != char(12) && at(i) != uchar(0)) {
|
||||
st = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (st < 0) return;
|
||||
for (int i = length() - 1; i >= 0; --i) {
|
||||
for (int i = d.size_s() - 1; i >= 0; --i) {
|
||||
if (at(i) != ' ' && at(i) != '\t' && at(i) != '\n' && at(i) != '\r' && at(i) != char(12) && at(i) != uchar(0)) {
|
||||
fn = i;
|
||||
break;
|
||||
@@ -428,7 +392,7 @@ void PIString::trimsubstr(int &st, int &fn) const {
|
||||
|
||||
|
||||
uint PIString::hash() const {
|
||||
return piHashData((const uchar*)PIDeque<PIChar>::data(), size() * sizeof(PIChar));
|
||||
return piHashData((const uchar*)d.data(), d.size() * sizeof(PIChar));
|
||||
}
|
||||
|
||||
|
||||
@@ -470,45 +434,35 @@ PIString & PIString::operator +=(const wchar_t * str) {
|
||||
if (!str) return *this;
|
||||
int i = -1;
|
||||
while (str[++i]) {
|
||||
push_back(PIChar(str[i]));
|
||||
d.push_back(PIChar(str[i]));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const PIString & str) {
|
||||
PIDeque<PIChar>::append(*(const PIDeque<PIChar>*)&str);
|
||||
d.append(str.d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator ==(const PIString & str) const {
|
||||
uint l = str.size();
|
||||
if (size() != l) return false;
|
||||
for (uint i = 0; i < l; ++i) {
|
||||
if (str[i] != at(i)) return false;
|
||||
}
|
||||
return true;
|
||||
return d == str.d;
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator !=(const PIString & str) const {
|
||||
uint l = str.size();
|
||||
if (size() != l) return true;
|
||||
for (uint i = 0; i < l; ++i) {
|
||||
if (str[i] != at(i)) return true;
|
||||
}
|
||||
return false;
|
||||
return d != str.d;
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator <(const PIString & str) const {
|
||||
uint l = str.size();
|
||||
size_t l = str.size();
|
||||
if (size() < l) return true;
|
||||
if (size() > l) return false;
|
||||
for (uint i = 0; i < l; ++i) {
|
||||
if (at(i) == str[i]) continue;
|
||||
if (at(i) < str[i]) return true;
|
||||
for (size_t i = 0; i < l; ++i) {
|
||||
if (at(i) == str.at(i)) continue;
|
||||
if (at(i) < str.at(i)) return true;
|
||||
else return false;
|
||||
}
|
||||
return false;
|
||||
@@ -516,12 +470,12 @@ bool PIString::operator <(const PIString & str) const {
|
||||
|
||||
|
||||
bool PIString::operator >(const PIString & str) const {
|
||||
uint l = str.size();
|
||||
size_t l = str.size();
|
||||
if (size() < l) return false;
|
||||
if (size() > l) return true;
|
||||
for (uint i = 0; i < l; ++i) {
|
||||
if (at(i) == str[i]) continue;
|
||||
if (at(i) < str[i]) return false;
|
||||
for (size_t i = 0; i < l; ++i) {
|
||||
if (at(i) == str.at(i)) continue;
|
||||
if (at(i) < str.at(i)) return false;
|
||||
else return true;
|
||||
}
|
||||
return false;
|
||||
@@ -552,10 +506,10 @@ PIString PIString::mid(const int start, const int len) const {
|
||||
s = 0;
|
||||
}
|
||||
if (l < 0) {
|
||||
return PIString(&(at(s)), size_s() - s);
|
||||
return PIString(d.data(s), size_s() - s);
|
||||
} else {
|
||||
if (l > length() - s) l = length() - s;
|
||||
return PIString(&(at(s)), l);
|
||||
return PIString(d.data(s), l);
|
||||
}
|
||||
return PIString();
|
||||
}
|
||||
@@ -580,10 +534,10 @@ PIString & PIString::cutMid(const int start, const int len) {
|
||||
s = 0;
|
||||
}
|
||||
if (l < 0) {
|
||||
remove(s, size() - s);
|
||||
d.remove(s, size() - s);
|
||||
} else {
|
||||
if (l > length() - s) l = length() - s;
|
||||
remove(s, l);
|
||||
d.remove(s, l);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -632,10 +586,10 @@ PIString PIString::trimmed() const {
|
||||
PIString & PIString::replace(int from, int count, const PIString & with) {
|
||||
count = piMini(count, length() - from);
|
||||
if (count == with.size_s()) {
|
||||
memcpy(PIDeque<PIChar>::data(from), static_cast<PIDeque<PIChar>>(with).data(), count * sizeof(PIChar));
|
||||
memcpy(d.data(from), with.d.data(), count * sizeof(PIChar));
|
||||
} else {
|
||||
remove(from, count);
|
||||
PIDeque<PIChar>::insert(from, with);
|
||||
d.remove(from, count);
|
||||
d.insert(from, with.d);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -687,9 +641,9 @@ PIString & PIString::replaceAll(const PIString & what, const PIString & with) {
|
||||
}
|
||||
}
|
||||
if (!match) continue;
|
||||
if (dl > 0) PIDeque<PIChar>::insert(i, PIDeque<PIChar>((size_t)dl));
|
||||
if (dl < 0) PIDeque<PIChar>::remove(i, -dl);
|
||||
memcpy(PIDeque<PIChar>::data(i), &(with.at(0)), with.length() * sizeof(PIChar));
|
||||
if (dl > 0) d.insert(i, PIDeque<PIChar>((size_t)dl));
|
||||
if (dl < 0) d.remove(i, -dl);
|
||||
memcpy(d.data(i), with.d.data(), with.size() * sizeof(PIChar));
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
@@ -715,8 +669,8 @@ PIString & PIString::replaceAll(const PIString & what, const char with) {
|
||||
}
|
||||
}
|
||||
if (!match) continue;
|
||||
if (dl > 0) PIDeque<PIChar>::remove(i, dl);
|
||||
(*this)[i] = PIChar(with);
|
||||
if (dl > 0) d.remove(i, dl);
|
||||
d[i] = PIChar(with);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -732,7 +686,7 @@ PIString & PIString::replaceAll(const PIString & what, const char with) {
|
||||
PIString & PIString::replaceAll(const char what, const char with) {
|
||||
int l = length();
|
||||
for (int i = 0; i < l; ++i) {
|
||||
if (at(i) == what) (*this)[i] = with;
|
||||
if (at(i) == what) d[i] = with;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -744,13 +698,13 @@ PIString & PIString::removeAll(const PIString & str) {
|
||||
for (int i = 0; i < length() - l + 1; ++i) {
|
||||
bool match = true;
|
||||
for (int j = 0; j < l; ++j) {
|
||||
if (at(j + i) != str[j]) {
|
||||
if (d.at(j + i) != str.at(j)) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!match) continue;
|
||||
PIDeque<PIChar>::remove(i, l);
|
||||
d.remove(i, l);
|
||||
i -= l;
|
||||
}
|
||||
return *this;
|
||||
@@ -758,7 +712,7 @@ PIString & PIString::removeAll(const PIString & str) {
|
||||
|
||||
|
||||
PIString & PIString::insert(int index, const PIString & str) {
|
||||
PIDeque<PIChar>::insert(index, *((const PIDeque<PIChar>*)&str));
|
||||
d.insert(index, str.d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -773,8 +727,8 @@ PIString & PIString::elide(int size, float pos) {
|
||||
pos = piClampf(pos, 0.f, 1.f);
|
||||
int ns = size - 2;
|
||||
int ls = piRoundf(ns * pos);
|
||||
remove(ls, length() - ns);
|
||||
insert(ls, s_dotdot);
|
||||
d.remove(ls, length() - ns);
|
||||
d.insert(ls, s_dotdot.d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -860,6 +814,14 @@ int PIString::findLast(const char c, const int start) const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int PIString::findLast(PIChar c, const int start) const
|
||||
{
|
||||
for (int i = length() - 1; i >= start; --i) {
|
||||
if (at(i) == c) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
//! \~\details
|
||||
//! \~\code
|
||||
@@ -909,14 +871,14 @@ int PIString::findWord(const PIString & word, const int start) const {
|
||||
bool ok = true;
|
||||
PIChar c;
|
||||
if (f > 0) {
|
||||
c = (*this)[f - 1];
|
||||
c = at(f - 1);
|
||||
if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r')) {
|
||||
ok = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (f + wl < tl) {
|
||||
c = (*this)[f + wl];
|
||||
c = at(f + wl);
|
||||
if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r')) {
|
||||
ok = false;
|
||||
continue;
|
||||
@@ -943,14 +905,14 @@ int PIString::findCWord(const PIString & word, const int start) const {
|
||||
bool ok = true;
|
||||
PIChar c;
|
||||
if (f > 0) {
|
||||
c = (*this)[f - 1];
|
||||
c = at(f - 1);
|
||||
if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || (c != '_' && !c.isAlpha() && !c.isDigit()))) {
|
||||
ok = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (f + wl < tl) {
|
||||
c = (*this)[f + wl];
|
||||
c = at(f + wl);
|
||||
if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || (c != '_' && !c.isAlpha() && !c.isDigit()))) {
|
||||
ok = false;
|
||||
continue;
|
||||
@@ -1448,7 +1410,7 @@ PIString PIString::toUpperCase() const {
|
||||
PIString str(*this);
|
||||
int l = str.size();
|
||||
for (int i = 0; i < l; ++i) {
|
||||
str[i] = str[i].toUpper();
|
||||
str.d[i] = str.d[i].toUpper();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
@@ -1458,7 +1420,7 @@ PIString PIString::toLowerCase() const {
|
||||
PIString str(*this);
|
||||
int l = str.size();
|
||||
for (int i = 0; i < l; ++i) {
|
||||
str[i] = str[i].toLower();
|
||||
str.d[i] = str.d[i].toLower();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user