git-svn-id: svn://db.shs.com.ru/pip@402 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -78,6 +78,7 @@ const int PIString::fromBaseN[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
|
||||
|
||||
|
||||
#ifndef CC_VC
|
||||
# define pisprintf(f, v) char ch[256]; memset(ch, 0, 256); sprintf(ch, f, v);
|
||||
#else
|
||||
@@ -86,7 +87,6 @@ const int PIString::fromBaseN[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -
|
||||
#ifdef ANDROID
|
||||
int wctomb(char * c, wchar_t w) {*c = ((char * )&w)[0]; return 1;}
|
||||
#endif
|
||||
|
||||
PIString PIString::itos(const int num) {pisprintf("%d", num); return PIString(ch);}
|
||||
PIString PIString::ltos(const long num) {pisprintf("%ld", num); return PIString(ch);}
|
||||
PIString PIString::lltos(const llong num) {pisprintf("%lld", num); return PIString(ch);}
|
||||
@@ -95,10 +95,88 @@ PIString PIString::ultos(const ulong num) {pisprintf("%lu", num); return PIStrin
|
||||
PIString PIString::ulltos(const ullong num) {pisprintf("%llu", num); return PIString(ch);}
|
||||
PIString PIString::ftos(const float num) {pisprintf("%.8f", num); return PIString(ch);}
|
||||
PIString PIString::dtos(const double num) {pisprintf("%.8f", num); return PIString(ch);}
|
||||
|
||||
#undef pisprintf
|
||||
|
||||
|
||||
|
||||
PIString PIString::fromNumberBaseS(const llong value, int base, bool * ok) {
|
||||
if (value == 0LL) return PIString("0");
|
||||
if (base < 2 || base > 40) {if (ok != 0) *ok = false; return PIString();}
|
||||
if (ok != 0) *ok = true;
|
||||
if (base == 10) return lltos(value);
|
||||
PIString ret;
|
||||
llong v = value < 0 ? -value : value, cn;
|
||||
int b = base;
|
||||
while (v >= llong(base)) {
|
||||
cn = v % b;
|
||||
v /= b;
|
||||
//cout << int(cn) << ", " << int(v) << endl;
|
||||
ret.push_front(PIChar(toBaseN[cn]));
|
||||
}
|
||||
if (v > 0) ret.push_front(PIChar(toBaseN[v]));
|
||||
if (value < 0) ret.push_front('-');
|
||||
return ret;
|
||||
}
|
||||
|
||||
PIString PIString::fromNumberBaseU(const ullong value, int base, bool * ok) {
|
||||
if (value == 0ULL) return PIString("0");
|
||||
if (base < 2 || base > 40) {if (ok != 0) *ok = false; return PIString();}
|
||||
if (ok != 0) *ok = true;
|
||||
if (base == 10) return ulltos(value);
|
||||
PIString ret;
|
||||
ullong v = value, cn;
|
||||
int b = base;
|
||||
while (v >= ullong(base)) {
|
||||
cn = v % b;
|
||||
v /= b;
|
||||
//cout << int(cn) << ", " << int(v) << endl;
|
||||
ret.push_front(PIChar(toBaseN[cn]));
|
||||
}
|
||||
if (v > 0) ret.push_front(PIChar(toBaseN[v]));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
llong PIString::toNumberBase(const PIString & value, int base, bool * ok) {
|
||||
PIString v = value.trimmed();
|
||||
if (base < 0) {
|
||||
int ind = v.find("0x");
|
||||
if (ind == 0 || ind == 1) {v.remove(ind, 2); base = 16;}
|
||||
else base = 10;
|
||||
} else
|
||||
if (base < 2 || base > 40) {if (ok != 0) *ok = false; return 0;}
|
||||
//v.reverse();
|
||||
if (ok) *ok = true;
|
||||
PIVector<int> digits;
|
||||
llong ret = 0, m = 1;
|
||||
bool neg = false;
|
||||
int cs;
|
||||
for (int i = 0; i < v.size_s(); ++i) {
|
||||
if (v[i] == PIChar('-')) {neg = !neg; continue;}
|
||||
cs = fromBaseN[int(v[i].toAscii())];
|
||||
if (cs < 0 || cs >= base) {
|
||||
if (ok) *ok = false;
|
||||
break;
|
||||
}
|
||||
digits << cs;
|
||||
}
|
||||
for (int i = digits.size_s() - 1; i >= 0; --i) {
|
||||
ret += digits[i] * m;
|
||||
m *= base;
|
||||
}
|
||||
if (neg) ret = -ret;
|
||||
/*piForeachC (PIChar & i, v) {
|
||||
if (i == PIChar('-')) {ret = -ret; continue;}
|
||||
cs = fromBaseN[int(i.toAscii())];
|
||||
cout << i << " = " << cs << endl;
|
||||
if (cs < 0 || cs >= base) return ret;
|
||||
ret += cs * m;
|
||||
m *= base;
|
||||
}*/
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void PIString::appendFromChars(const char * c, int s, const char * cp) {
|
||||
if (s <= 0) return;
|
||||
#ifdef PIP_ICU
|
||||
@@ -127,21 +205,21 @@ void PIString::appendFromChars(const char * c, int s, const char * cp) {
|
||||
sz = mbtowc(&wc, &(c[i]), 4);
|
||||
//cout << sz << endl;
|
||||
switch (sz) {
|
||||
case 4:
|
||||
push_back(PIChar(*(int*)&(c[i])));
|
||||
i += 3;
|
||||
case 4:
|
||||
push_back(PIChar(*(int*)&(c[i])));
|
||||
i += 3;
|
||||
continue;
|
||||
case 3:
|
||||
push_back(PIChar(*(int*)&(c[i])));
|
||||
back().ch &= 0xFFFFFF;
|
||||
i += 2;
|
||||
case 3:
|
||||
push_back(PIChar(*(int*)&(c[i])));
|
||||
back().ch &= 0xFFFFFF;
|
||||
i += 2;
|
||||
continue;
|
||||
case 2:
|
||||
push_back(PIChar(*(short * )&(c[i])));
|
||||
++i;
|
||||
case 2:
|
||||
push_back(PIChar(*(short * )&(c[i])));
|
||||
++i;
|
||||
continue;
|
||||
default:
|
||||
push_back(PIChar(c[i]));
|
||||
default:
|
||||
push_back(PIChar(c[i]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -208,6 +286,13 @@ PIString PIString::fromCodepage(const char * s, const char * c) {
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::readableSize(llong bytes) {
|
||||
PIString s;
|
||||
s.setReadableSize(bytes);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
void PIString::buildData(const char * cp) const {
|
||||
data_.clear();
|
||||
#ifdef PIP_ICU
|
||||
|
||||
Reference in New Issue
Block a user