code format
This commit is contained in:
@@ -1,26 +1,27 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
String
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
String
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pistring.h"
|
||||
#include "pistringlist.h"
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pimathbase.h"
|
||||
#include "pistringlist.h"
|
||||
#ifdef PIP_ICU
|
||||
# define U_NOEXCEPT
|
||||
# include "unicode/ucnv.h"
|
||||
@@ -28,9 +29,9 @@
|
||||
#ifdef WINDOWS
|
||||
# include <stringapiset.h>
|
||||
#endif
|
||||
#include <string>
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#include <string>
|
||||
|
||||
//! \class PIString pistring.h
|
||||
//! \~\details
|
||||
@@ -51,35 +52,36 @@
|
||||
//!
|
||||
|
||||
|
||||
|
||||
|
||||
const char PIString::toBaseN[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
||||
'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^'};
|
||||
const char PIString::fromBaseN[] = {(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char) 0, (char) 1, (char) 2, (char) 3, (char) 4, (char) 5, (char) 6, (char) 7, (char) 8, (char) 9, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)10, (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, (char)21, (char)22, (char)23, (char)24,
|
||||
(char)25, (char)26, (char)27, (char)28, (char)29, (char)30, (char)31, (char)32, (char)33, (char)34, (char)35, (char)36, (char)37, (char)38, (char)39, (char)-1,
|
||||
(char)-1, (char)10, (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, (char)21, (char)22, (char)23, (char)24,
|
||||
(char)25, (char)26, (char)27, (char)28, (char)29, (char)30, (char)31, (char)32, (char)33, (char)34, (char)35, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1};
|
||||
const char PIString::toBaseN[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^'};
|
||||
const char PIString::fromBaseN[] = {
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)0, (char)1, (char)2, (char)3,
|
||||
(char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)10, (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, (char)21, (char)22,
|
||||
(char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, (char)31, (char)32, (char)33, (char)34, (char)35,
|
||||
(char)36, (char)37, (char)38, (char)39, (char)-1, (char)-1, (char)10, (char)11, (char)12, (char)13, (char)14, (char)15, (char)16,
|
||||
(char)17, (char)18, (char)19, (char)20, (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29,
|
||||
(char)30, (char)31, (char)32, (char)33, (char)34, (char)35, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1,
|
||||
(char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1, (char)-1};
|
||||
|
||||
const float PIString::ElideLeft = 0.f;
|
||||
const float PIString::ElideCenter = .5f;
|
||||
const float PIString::ElideRight = 1.f;
|
||||
|
||||
|
||||
template <typename T>
|
||||
template<typename T>
|
||||
T toDecimal(const PIString & s) {
|
||||
int part = 0, exp = 0;
|
||||
bool negative = false, negative_exp = false, err = false, has_digit = false;
|
||||
@@ -91,8 +93,8 @@ T toDecimal(const PIString & s) {
|
||||
if (pc.isSpace()) continue;
|
||||
if (c >= '0' && c <= '9') {
|
||||
has_digit = true;
|
||||
ret = c - '0';
|
||||
part = 1;
|
||||
ret = c - '0';
|
||||
part = 1;
|
||||
continue;
|
||||
}
|
||||
if (c == '+') {
|
||||
@@ -101,7 +103,7 @@ T toDecimal(const PIString & s) {
|
||||
}
|
||||
if (c == '-') {
|
||||
negative = true;
|
||||
part = 1;
|
||||
part = 1;
|
||||
continue;
|
||||
}
|
||||
if (c == '.' || c == ',') {
|
||||
@@ -113,7 +115,7 @@ T toDecimal(const PIString & s) {
|
||||
case 1: // integer
|
||||
if (c >= '0' && c <= '9') {
|
||||
has_digit = true;
|
||||
ret = ret * 10 + (c - '0');
|
||||
ret = ret * 10 + (c - '0');
|
||||
continue;
|
||||
}
|
||||
if (c == '.' || c == ',') {
|
||||
@@ -129,7 +131,7 @@ T toDecimal(const PIString & s) {
|
||||
case 2: // fractional
|
||||
if (c >= '0' && c <= '9') {
|
||||
has_digit = true;
|
||||
frac = frac * 10 + (c - '0');
|
||||
frac = frac * 10 + (c - '0');
|
||||
frac_delim *= 10.;
|
||||
continue;
|
||||
}
|
||||
@@ -146,7 +148,7 @@ T toDecimal(const PIString & s) {
|
||||
}
|
||||
if (c == '-') {
|
||||
negative_exp = true;
|
||||
part = 4;
|
||||
part = 4;
|
||||
continue;
|
||||
}
|
||||
case 4: // exponent
|
||||
@@ -163,24 +165,42 @@ T toDecimal(const PIString & s) {
|
||||
ret += frac;
|
||||
if (negative && has_digit) ret = -ret;
|
||||
if (exp > 0) {
|
||||
if (negative_exp) ret /= pow10((T)exp);
|
||||
else ret *= pow10((T)exp);
|
||||
if (negative_exp)
|
||||
ret /= pow10((T)exp);
|
||||
else
|
||||
ret *= pow10((T)exp);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#define pisprintf(f, v) char ch[256]; memset(ch, 0, 256); snprintf(ch, 256, f, v); return PIStringAscii(ch);
|
||||
#define pisprintf(f, v) \
|
||||
char ch[256]; \
|
||||
memset(ch, 0, 256); \
|
||||
snprintf(ch, 256, f, v); \
|
||||
return PIStringAscii(ch);
|
||||
|
||||
PIString PIString::itos(const int num) {pisprintf("%d", num);}
|
||||
PIString PIString::ltos(const long num) {pisprintf("%ld", num);}
|
||||
PIString PIString::lltos(const llong num) {pisprintf("%lld", num);}
|
||||
PIString PIString::uitos(const uint num) {pisprintf("%u", num);}
|
||||
PIString PIString::ultos(const ulong num) {pisprintf("%lu", num);}
|
||||
PIString PIString::ulltos(const ullong num) {pisprintf("%llu", num);}
|
||||
PIString PIString::itos(const int num) {
|
||||
pisprintf("%d", num);
|
||||
}
|
||||
PIString PIString::ltos(const long num) {
|
||||
pisprintf("%ld", num);
|
||||
}
|
||||
PIString PIString::lltos(const llong num) {
|
||||
pisprintf("%lld", num);
|
||||
}
|
||||
PIString PIString::uitos(const uint num) {
|
||||
pisprintf("%u", num);
|
||||
}
|
||||
PIString PIString::ultos(const ulong num) {
|
||||
pisprintf("%lu", num);
|
||||
}
|
||||
PIString PIString::ulltos(const ullong num) {
|
||||
pisprintf("%llu", num);
|
||||
}
|
||||
PIString PIString::dtos(const double num, char format, int precision) {
|
||||
char f[8] = "%.";
|
||||
int wr = snprintf(&(f[2]), 4, "%d", precision);
|
||||
int wr = snprintf(&(f[2]), 4, "%d", precision);
|
||||
if (wr > 4) wr = 4;
|
||||
f[2 + wr] = format;
|
||||
f[3 + wr] = 0;
|
||||
@@ -189,7 +209,6 @@ PIString PIString::dtos(const double num, char format, int precision) {
|
||||
#undef pisprintf
|
||||
|
||||
|
||||
|
||||
PIString PIString::fromNumberBaseS(const llong value, int base, bool * ok) {
|
||||
if (value == 0LL) return PIString('0');
|
||||
if ((base < 2) || (base > 40)) {
|
||||
@@ -200,11 +219,11 @@ PIString PIString::fromNumberBaseS(const llong value, int base, bool * ok) {
|
||||
if (base == 10) return lltos(value);
|
||||
PIString ret;
|
||||
llong v = value < 0 ? -value : value, cn;
|
||||
int b = base;
|
||||
int b = base;
|
||||
while (v >= llong(base)) {
|
||||
cn = v % b;
|
||||
v /= b;
|
||||
//cout << int(cn) << ", " << int(v) << endl;
|
||||
// cout << int(cn) << ", " << int(v) << endl;
|
||||
ret.push_front(PIChar(toBaseN[cn]));
|
||||
}
|
||||
if (v > 0) ret.push_front(PIChar(toBaseN[v]));
|
||||
@@ -222,11 +241,11 @@ PIString PIString::fromNumberBaseU(const ullong value, int base, bool * ok) {
|
||||
if (base == 10) return ulltos(value);
|
||||
PIString ret;
|
||||
ullong v = value, cn;
|
||||
int b = base;
|
||||
int b = base;
|
||||
while (v >= ullong(base)) {
|
||||
cn = v % b;
|
||||
v /= b;
|
||||
//cout << int(cn) << ", " << int(v) << endl;
|
||||
// cout << int(cn) << ", " << int(v) << endl;
|
||||
ret.push_front(PIChar(toBaseN[cn]));
|
||||
}
|
||||
if (v > 0) ret.push_front(PIChar(toBaseN[v]));
|
||||
@@ -236,7 +255,7 @@ PIString PIString::fromNumberBaseU(const ullong value, int base, bool * ok) {
|
||||
|
||||
llong PIString::toNumberBase(const PIString & value, int base, bool * ok) {
|
||||
static const PIString s_0x = PIStringAscii("0x");
|
||||
PIString v = value.trimmed();
|
||||
PIString v = value.trimmed();
|
||||
if (base < 0) {
|
||||
int ind = v.find(s_0x);
|
||||
if (ind == 0 || ind == 1) {
|
||||
@@ -276,7 +295,7 @@ llong PIString::toNumberBase(const PIString & value, int base, bool * ok) {
|
||||
|
||||
|
||||
void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
// piCout << "appendFromChars";
|
||||
// piCout << "appendFromChars";
|
||||
if (s == 0) return;
|
||||
int old_sz = size_s();
|
||||
if (s == -1) s = strlen(c);
|
||||
@@ -285,9 +304,9 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
UConverter * cc = ucnv_open(codepage, &e);
|
||||
if (cc) {
|
||||
d.enlarge(s);
|
||||
e = (UErrorCode)0;
|
||||
int sz = ucnv_toUChars(cc, (UChar*)(d.data(old_sz)), s, c, s, &e);
|
||||
d.resize(old_sz+sz);
|
||||
e = (UErrorCode)0;
|
||||
int sz = ucnv_toUChars(cc, (UChar *)(d.data(old_sz)), s, c, s, &e);
|
||||
d.resize(old_sz + sz);
|
||||
ucnv_close(cc);
|
||||
return;
|
||||
}
|
||||
@@ -299,7 +318,7 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, s, (LPWSTR)d.data(old_sz), sz);
|
||||
# else
|
||||
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
|
||||
std::u16string ucs2 = ucs2conv.from_bytes(c, c+s);
|
||||
std::u16string ucs2 = ucs2conv.from_bytes(c, c + s);
|
||||
d.enlarge(ucs2.size());
|
||||
ucs2.copy((char16_t *)d.data(old_sz), ucs2.size());
|
||||
# endif
|
||||
@@ -309,7 +328,7 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
|
||||
PIString PIString::fromConsole(const PIByteArray & ba) {
|
||||
PIString ret;
|
||||
if (ba.isNotEmpty()) ret.appendFromChars((const char*)ba.data(), ba.size(), __sysoemname__);
|
||||
if (ba.isNotEmpty()) ret.appendFromChars((const char *)ba.data(), ba.size(), __sysoemname__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -319,13 +338,12 @@ PIString PIString::fromConsole(const char * s) {
|
||||
if (!s) return ret;
|
||||
if (s[0] != '\0') ret.appendFromChars(s, -1, __sysoemname__);
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromSystem(const PIByteArray & ba) {
|
||||
PIString ret;
|
||||
if (ba.isNotEmpty()) ret.appendFromChars((const char*)ba.data(), ba.size(), __syslocname__);
|
||||
if (ba.isNotEmpty()) ret.appendFromChars((const char *)ba.data(), ba.size(), __syslocname__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -355,8 +373,8 @@ PIString PIString::fromUTF8(const char * s) {
|
||||
PIString PIString::fromUTF8(const PIByteArray & ba) {
|
||||
PIString ret;
|
||||
if (ba.isNotEmpty()) {
|
||||
const char * data = (const char*)ba.data();
|
||||
int size = ba.size();
|
||||
const char * data = (const char *)ba.data();
|
||||
int size = ba.size();
|
||||
if (ba.size() >= 3) {
|
||||
if (ba[0] == 0xEF && ba[1] == 0xBB && ba[2] == 0xBF) {
|
||||
data += 3;
|
||||
@@ -387,15 +405,19 @@ PIString PIString::fromAscii(const char * s, int len) {
|
||||
|
||||
PIString PIString::fromCodepage(const char * s, const char * c) {
|
||||
PIString ret;
|
||||
if (s[0] > '\0') ret.appendFromChars(s, -1
|
||||
if (s[0] > '\0')
|
||||
ret.appendFromChars(s,
|
||||
-1
|
||||
#ifdef PIP_ICU
|
||||
, c
|
||||
,
|
||||
c
|
||||
#else
|
||||
# ifdef WINDOWS
|
||||
, __utf8name__
|
||||
,
|
||||
__utf8name__
|
||||
# endif
|
||||
#endif
|
||||
);
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -426,9 +448,9 @@ void PIString::buildData(const char * cp) const {
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(cp, &e);
|
||||
if (cc) {
|
||||
const size_t len = MB_CUR_MAX*size()+1;
|
||||
data_ = (char *)malloc(len);
|
||||
int sz = ucnv_fromUChars(cc, data_, len, (const UChar*)(d.data()), d.size_s(), &e);
|
||||
const size_t len = MB_CUR_MAX * size() + 1;
|
||||
data_ = (char *)malloc(len);
|
||||
int sz = ucnv_fromUChars(cc, data_, len, (const UChar *)(d.data()), d.size_s(), &e);
|
||||
ucnv_close(cc);
|
||||
data_[sz] = '\0';
|
||||
return;
|
||||
@@ -437,18 +459,18 @@ void PIString::buildData(const char * cp) const {
|
||||
# ifdef WINDOWS
|
||||
int sz = WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)d.data(), d.size_s(), 0, 0, NULL, NULL);
|
||||
if (sz <= 0) {
|
||||
data_ = (char *)malloc(1);
|
||||
data_ = (char *)malloc(1);
|
||||
data_[0] = '\0';
|
||||
return;
|
||||
}
|
||||
data_ = (char *)malloc(sz+1);
|
||||
data_ = (char *)malloc(sz + 1);
|
||||
WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)d.data(), d.size_s(), (LPSTR)data_, sz, NULL, NULL);
|
||||
data_[sz] = '\0';
|
||||
return;
|
||||
# else
|
||||
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
|
||||
std::string u8str = ucs2conv.to_bytes((char16_t*)d.data(), (char16_t*)d.data() + d.size());
|
||||
data_ = (char *)malloc(u8str.size()+1);
|
||||
std::string u8str = ucs2conv.to_bytes((char16_t *)d.data(), (char16_t *)d.data() + d.size());
|
||||
data_ = (char *)malloc(u8str.size() + 1);
|
||||
strcpy(data_, u8str.c_str());
|
||||
# endif
|
||||
#endif
|
||||
@@ -462,7 +484,7 @@ void PIString::deleteData() const {
|
||||
}
|
||||
|
||||
|
||||
void PIString::trimsubstr(int &st, int &fn) const {
|
||||
void PIString::trimsubstr(int & st, int & fn) const {
|
||||
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;
|
||||
@@ -480,7 +502,7 @@ void PIString::trimsubstr(int &st, int &fn) const {
|
||||
|
||||
|
||||
uint PIString::hash() const {
|
||||
return piHashData((const uchar*)d.data(), d.size() * sizeof(PIChar));
|
||||
return piHashData((const uchar *)d.data(), d.size() * sizeof(PIChar));
|
||||
}
|
||||
|
||||
|
||||
@@ -502,13 +524,13 @@ PIByteArray PIString::toCharset(const char * c) const {
|
||||
if (isEmpty()) return PIByteArray();
|
||||
buildData(
|
||||
#ifdef PIP_ICU
|
||||
c
|
||||
c
|
||||
#else
|
||||
# ifdef WINDOWS
|
||||
__utf8name__
|
||||
__utf8name__
|
||||
# endif
|
||||
#endif
|
||||
);
|
||||
);
|
||||
return PIByteArray(data_, strlen(data_));
|
||||
}
|
||||
|
||||
@@ -516,8 +538,7 @@ PIByteArray PIString::toCharset(const char * c) const {
|
||||
PIString PIString::simplified() const {
|
||||
PIString ret(*this);
|
||||
for (int i = 0; i < ret.size_s(); ++i)
|
||||
if (!ret[i].isAscii())
|
||||
ret[i] = '?';
|
||||
if (!ret[i].isAscii()) ret[i] = '?';
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -543,15 +564,15 @@ PIString & PIString::unmask(const PIString & symbols, const PIChar mc) {
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const char * str) {
|
||||
PIString & PIString::operator+=(const char * str) {
|
||||
if (!str) return *this;
|
||||
appendFromChars(str, -1, __syslocname__);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const PIByteArray & ba) {
|
||||
appendFromChars((const char * )ba.data(), ba.size_s(), __utf8name__);
|
||||
PIString & PIString::operator+=(const PIByteArray & ba) {
|
||||
appendFromChars((const char *)ba.data(), ba.size_s(), __utf8name__);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -561,7 +582,7 @@ PIString::~PIString() {
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const wchar_t * str) {
|
||||
PIString & PIString::operator+=(const wchar_t * str) {
|
||||
if (!str) return *this;
|
||||
int i = -1;
|
||||
while (str[++i]) {
|
||||
@@ -571,13 +592,13 @@ PIString & PIString::operator +=(const wchar_t * str) {
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const PIString & str) {
|
||||
PIString & PIString::operator+=(const PIString & str) {
|
||||
d.append(str.d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const PIConstChars & str) {
|
||||
PIString & PIString::operator+=(const PIConstChars & str) {
|
||||
if (!str.isEmpty()) {
|
||||
size_t os = d.size();
|
||||
d.enlarge(str.size());
|
||||
@@ -589,37 +610,41 @@ PIString & PIString::operator +=(const PIConstChars & str) {
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator ==(const PIString & str) const {
|
||||
bool PIString::operator==(const PIString & str) const {
|
||||
return d == str.d;
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator !=(const PIString & str) const {
|
||||
bool PIString::operator!=(const PIString & str) const {
|
||||
return d != str.d;
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator <(const PIString & str) const {
|
||||
bool PIString::operator<(const PIString & str) const {
|
||||
size_t l = str.size();
|
||||
if (size() < l) return true;
|
||||
if (size() > l) return false;
|
||||
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;
|
||||
if (at(i) < str.at(i))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator >(const PIString & str) const {
|
||||
bool PIString::operator>(const PIString & str) const {
|
||||
size_t l = str.size();
|
||||
if (size() < l) return false;
|
||||
if (size() > l) return true;
|
||||
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;
|
||||
if (at(i) < str.at(i))
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -874,7 +899,7 @@ PIString & PIString::elide(int size, float pos) {
|
||||
fill('.');
|
||||
return *this;
|
||||
}
|
||||
pos = piClampf(pos, 0.f, 1.f);
|
||||
pos = piClampf(pos, 0.f, 1.f);
|
||||
int ns = size - 2;
|
||||
int ls = piRoundf(ns * pos);
|
||||
d.remove(ls, length() - ns);
|
||||
@@ -964,8 +989,7 @@ int PIString::findLast(const char c, const int start) const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int PIString::findLast(PIChar c, const int start) const
|
||||
{
|
||||
int PIString::findLast(PIChar c, const int start) const {
|
||||
for (int i = length() - 1; i >= start; --i) {
|
||||
if (at(i) == c) return i;
|
||||
}
|
||||
@@ -1094,14 +1118,18 @@ int PIString::findRange(const PIChar start, const PIChar end, const PIChar shiel
|
||||
continue;
|
||||
}
|
||||
if (trim_) {
|
||||
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
|
||||
continue;
|
||||
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
|
||||
trim_ = false;
|
||||
}
|
||||
if (eq) {
|
||||
if (c == start) {
|
||||
if (cnt == 0) ls = i;
|
||||
else {le = i; cnt = 0; break;}
|
||||
if (cnt == 0)
|
||||
ls = i;
|
||||
else {
|
||||
le = i;
|
||||
cnt = 0;
|
||||
break;
|
||||
}
|
||||
cnt++;
|
||||
}
|
||||
} else {
|
||||
@@ -1116,7 +1144,7 @@ int PIString::findRange(const PIChar start, const PIChar end, const PIChar shiel
|
||||
}
|
||||
if (cnt <= 0) break;
|
||||
}
|
||||
//piCout << ls << le << cnt;
|
||||
// piCout << ls << le << cnt;
|
||||
if (le < ls || ls < 0 || le < 0 || cnt != 0) return -1;
|
||||
if (len) *len = le - ls - 1;
|
||||
return ls + 1;
|
||||
@@ -1157,9 +1185,9 @@ bool PIString::endsWith(const PIString & str) const {
|
||||
//! \endcode
|
||||
bool PIString::toBool() const {
|
||||
static const PIString s_true = PIStringAscii("true");
|
||||
static const PIString s_yes = PIStringAscii("yes" );
|
||||
static const PIString s_on = PIStringAscii("on" );
|
||||
static const PIString s_ok = PIStringAscii("ok" );
|
||||
static const PIString s_yes = PIStringAscii("yes");
|
||||
static const PIString s_on = PIStringAscii("on");
|
||||
static const PIString s_ok = PIStringAscii("ok");
|
||||
PIString s(*this);
|
||||
s = s.trimmed().toLowerCase();
|
||||
if (s == s_true || s == s_yes || s == s_on || s == s_ok) return true;
|
||||
@@ -1303,7 +1331,7 @@ PIString PIString::takeNumber() {
|
||||
for (int i = 0; i < sz; ++i) {
|
||||
if (phase > 7) break;
|
||||
PIChar c = at(i);
|
||||
//piCout << "char " << c << "phase" << phase;
|
||||
// piCout << "char " << c << "phase" << phase;
|
||||
switch (phase) {
|
||||
case 0: // trim
|
||||
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
|
||||
@@ -1312,7 +1340,7 @@ PIString PIString::takeNumber() {
|
||||
phase = 7;
|
||||
case 7: // sign
|
||||
if (c == '-' || c == '+') {
|
||||
ls = i;
|
||||
ls = i;
|
||||
phase = 1;
|
||||
break;
|
||||
}
|
||||
@@ -1320,20 +1348,22 @@ PIString PIString::takeNumber() {
|
||||
if ((c >= '0' && c <= '9') || c == '.') {
|
||||
le = i;
|
||||
if (ls < 0) ls = i;
|
||||
if (c == '.') phase = 3;
|
||||
else phase = 2;
|
||||
if (c == '.')
|
||||
phase = 3;
|
||||
else
|
||||
phase = 2;
|
||||
break;
|
||||
}
|
||||
phase = 9;
|
||||
break;
|
||||
case 2: // integer
|
||||
if (c == '.') {
|
||||
le = i;
|
||||
le = i;
|
||||
phase = 3;
|
||||
break;
|
||||
}
|
||||
if (c == 'e' || c == 'E') {
|
||||
le = i;
|
||||
le = i;
|
||||
phase = 4;
|
||||
break;
|
||||
}
|
||||
@@ -1345,7 +1375,7 @@ PIString PIString::takeNumber() {
|
||||
break;
|
||||
case 3: // point
|
||||
if (c == 'e' || c == 'E') {
|
||||
le = i;
|
||||
le = i;
|
||||
phase = 4;
|
||||
break;
|
||||
}
|
||||
@@ -1357,7 +1387,7 @@ PIString PIString::takeNumber() {
|
||||
break;
|
||||
case 4: // exp
|
||||
if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
|
||||
le = i;
|
||||
le = i;
|
||||
phase = 5;
|
||||
break;
|
||||
}
|
||||
@@ -1379,11 +1409,13 @@ PIString PIString::takeNumber() {
|
||||
break;
|
||||
}
|
||||
if (phase == 6) {
|
||||
if (c == 'f' || c == 's' || c == 'u' || c == 'l' || c == 'L') le = i;
|
||||
else phase = 9;
|
||||
if (c == 'f' || c == 's' || c == 'u' || c == 'l' || c == 'L')
|
||||
le = i;
|
||||
else
|
||||
phase = 9;
|
||||
}
|
||||
}
|
||||
//piCout << ls << le;
|
||||
// piCout << ls << le;
|
||||
if (le < ls) return ret;
|
||||
ret = mid(ls, le - ls + 1);
|
||||
cutLeft(le + 1);
|
||||
@@ -1449,7 +1481,7 @@ PIString PIString::takeRange(const PIChar start, const PIChar end, const PIChar
|
||||
if (cnt == 0) {
|
||||
ls = i;
|
||||
} else {
|
||||
le = i;
|
||||
le = i;
|
||||
cnt = 0;
|
||||
break;
|
||||
}
|
||||
@@ -1467,7 +1499,7 @@ PIString PIString::takeRange(const PIChar start, const PIChar end, const PIChar
|
||||
}
|
||||
if (cnt <= 0) break;
|
||||
}
|
||||
//piCout << ls << le << cnt;
|
||||
// piCout << ls << le << cnt;
|
||||
if (le < ls || ls < 0 || le < 0 || cnt != 0) return ret;
|
||||
ret = mid(ls + 1, le - ls - 1);
|
||||
cutLeft(le + 1);
|
||||
@@ -1493,7 +1525,7 @@ PIString PIString::inBrackets(const PIChar start, const PIChar end) const {
|
||||
}
|
||||
if (cc == end && st >= 0) {
|
||||
bcnt--;
|
||||
if (bcnt == 0) return mid(st+1, i-st-1);
|
||||
if (bcnt == 0) return mid(st + 1, i - st - 1);
|
||||
}
|
||||
}
|
||||
return PIString();
|
||||
@@ -1533,7 +1565,7 @@ const char * PIString::data() const {
|
||||
//! \~\sa \a data(), \a dataUTF8()
|
||||
const char * PIString::dataConsole() const {
|
||||
if (isEmpty()) return "";
|
||||
buildData(__sysoemname__ );
|
||||
buildData(__sysoemname__);
|
||||
return data_;
|
||||
}
|
||||
|
||||
@@ -1568,7 +1600,7 @@ const char * PIString::dataUTF8() const {
|
||||
const char * PIString::dataAscii() const {
|
||||
if (isEmpty()) return "";
|
||||
deleteData();
|
||||
data_ = (char*)malloc(size()+1);
|
||||
data_ = (char *)malloc(size() + 1);
|
||||
for (int i = 0; i < size_s(); ++i) {
|
||||
data_[i] = uchar(at(i).ch);
|
||||
}
|
||||
@@ -1646,7 +1678,7 @@ PIString & PIString::setReadableSize(llong bytes) {
|
||||
return *this;
|
||||
}
|
||||
double fres = bytes / 1024.;
|
||||
llong res = bytes / 1024;
|
||||
llong res = bytes / 1024;
|
||||
fres -= res;
|
||||
if (res < 1024) {
|
||||
*this += (PIString::fromNumber(res) + PIStringAscii(".") + PIString::fromNumber(llong(fres * 10)).left(1) + PIStringAscii(" kB"));
|
||||
@@ -1722,7 +1754,7 @@ void parseVersion(PIString s, PIVector<int> & codes, PIStringList & strs) {
|
||||
strs << comps[i];
|
||||
}
|
||||
}
|
||||
//piCout << codes << strs;
|
||||
// piCout << codes << strs;
|
||||
}
|
||||
|
||||
|
||||
@@ -1742,7 +1774,7 @@ int versionLabelValue(PIString s) {
|
||||
ret += 10000 + s.toInt();
|
||||
}
|
||||
if (s == PIStringAscii("alpha")) ret -= 4;
|
||||
if (s == PIStringAscii("beta" )) ret -= 2;
|
||||
if (s == PIStringAscii("beta")) ret -= 2;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1780,10 +1812,11 @@ int versionLabelValue(PIString s) {
|
||||
//! * 1 - v0 > v1
|
||||
//! * -1 - v0 < v1
|
||||
int versionCompare(const PIString & v0, const PIString & v1, int components) {
|
||||
PIStringList strs[2]; PIVector<int> codes[2];
|
||||
PIStringList strs[2];
|
||||
PIVector<int> codes[2];
|
||||
parseVersion(v0.toLowerCase(), codes[0], strs[0]);
|
||||
parseVersion(v1.toLowerCase(), codes[1], strs[1]);
|
||||
//piCout << codes[0] << strs[0];
|
||||
// piCout << codes[0] << strs[0];
|
||||
int mc = piMaxi(codes[0].size_s(), codes[1].size_s());
|
||||
if (codes[0].size_s() < mc) codes[0].resize(mc, 0);
|
||||
if (codes[1].size_s() < mc) codes[1].resize(mc, 0);
|
||||
@@ -1793,14 +1826,14 @@ int versionCompare(const PIString & v0, const PIString & v1, int components) {
|
||||
int comps = piMini(components, codes[0].size_s(), codes[1].size_s());
|
||||
if (comps < 1) return (v0 == v1 ? 0 : (v0 > v1 ? 1 : -1));
|
||||
for (int c = 0; c < comps; ++c) {
|
||||
if (codes[0][c] > codes[1][c]) return 1;
|
||||
if (codes[0][c] > codes[1][c]) return 1;
|
||||
if (codes[0][c] < codes[1][c]) return -1;
|
||||
}
|
||||
mc = piClampi(mc, 0, components - comps);
|
||||
for (int c = 0; c < mc; ++c) {
|
||||
int lv0 = versionLabelValue(strs[0][c]);
|
||||
int lv1 = versionLabelValue(strs[1][c]);
|
||||
if (lv0 > lv1) return 1;
|
||||
if (lv0 > lv1) return 1;
|
||||
if (lv0 < lv1) return -1;
|
||||
}
|
||||
return 0;
|
||||
@@ -1827,13 +1860,16 @@ int versionCompare(const PIString & v0, const PIString & v1, int components) {
|
||||
//! piCout << versionNormalize("1..4_rc2-999"); // 1.0.4-999_rc2
|
||||
//! \endcode
|
||||
PIString versionNormalize(const PIString & v) {
|
||||
PIStringList strs; PIVector<int> codes;
|
||||
PIStringList strs;
|
||||
PIVector<int> codes;
|
||||
parseVersion(v.toLowerCase(), codes, strs);
|
||||
PIString ret;
|
||||
for (int i = 0; i < codes.size_s(); ++i) {
|
||||
if (i > 0) {
|
||||
if (i < 3) ret += '.';
|
||||
else ret += '-';
|
||||
if (i < 3)
|
||||
ret += '.';
|
||||
else
|
||||
ret += '-';
|
||||
}
|
||||
ret += PIString::fromNumber(codes[i]);
|
||||
}
|
||||
@@ -1843,4 +1879,3 @@ PIString versionNormalize(const PIString & v) {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user