PIString optimization

This commit is contained in:
Andrey
2022-04-26 17:19:05 +03:00
parent 4bc12989ca
commit 7ad520a1c3
2 changed files with 17 additions and 11 deletions

View File

@@ -85,7 +85,7 @@ const float PIString::ElideCenter = .5f;
const float PIString::ElideRight = 1.f; const float PIString::ElideRight = 1.f;
#define pisprintf(f, v) char ch[256]; memset(ch, 0, 256); snprintf(ch, 256, f, v); return PIString(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::itos(const int num) {pisprintf("%d", num);}
PIString PIString::ltos(const long num) {pisprintf("%ld", num);} PIString PIString::ltos(const long num) {pisprintf("%ld", num);}
@@ -95,14 +95,14 @@ PIString PIString::ultos(const ulong num) {pisprintf("%lu", num);}
PIString PIString::ulltos(const ullong num) {pisprintf("%llu", num);} PIString PIString::ulltos(const ullong num) {pisprintf("%llu", num);}
PIString PIString::ftos(const float num, char format, int precision) { PIString PIString::ftos(const float num, char format, int precision) {
char f[8] = "%."; char f[8] = "%.";
int wr = sprintf(&(f[2]), "%d", precision); int wr = snprintf(&(f[2]), 8, "%d", precision);
f[2 + wr] = format; f[2 + wr] = format;
f[3 + wr] = 0; f[3 + wr] = 0;
pisprintf(f, num); pisprintf(f, num);
} }
PIString PIString::dtos(const double num, char format, int precision) { PIString PIString::dtos(const double num, char format, int precision) {
char f[8] = "%."; char f[8] = "%.";
int wr = sprintf(&(f[2]), "%d", precision); int wr = snprintf(&(f[2]), 8, "%d", precision);
f[2 + wr] = format; f[2 + wr] = format;
f[3 + wr] = 0; f[3 + wr] = 0;
pisprintf(f, num); pisprintf(f, num);
@@ -113,7 +113,7 @@ PIString PIString::dtos(const double num, char format, int precision) {
PIString PIString::fromNumberBaseS(const llong value, int base, bool * ok) { PIString PIString::fromNumberBaseS(const llong value, int base, bool * ok) {
if (value == 0LL) return PIString('0'); if (value == 0LL) return PIString('0');
if (base < 2 || base > 40) { if ((base < 2) || (base > 40)) {
if (ok != 0) *ok = false; if (ok != 0) *ok = false;
return PIString(); return PIString();
} }
@@ -135,7 +135,7 @@ PIString PIString::fromNumberBaseS(const llong value, int base, bool * ok) {
PIString PIString::fromNumberBaseU(const ullong value, int base, bool * ok) { PIString PIString::fromNumberBaseU(const ullong value, int base, bool * ok) {
if (value == 0ULL) return PIString('0'); if (value == 0ULL) return PIString('0');
if (base < 2 || base > 40) { if ((base < 2) || (base > 40)) {
if (ok != 0) *ok = false; if (ok != 0) *ok = false;
return PIString(); return PIString();
} }
@@ -166,7 +166,7 @@ llong PIString::toNumberBase(const PIString & value, int base, bool * ok) {
} else { } else {
base = 10; base = 10;
} }
} else if (base < 2 || base > 40) { } else if ((base < 2) || (base > 40)) {
if (ok != 0) *ok = false; if (ok != 0) *ok = false;
return 0; return 0;
} }
@@ -197,6 +197,7 @@ llong PIString::toNumberBase(const PIString & value, int base, bool * ok) {
void PIString::appendFromChars(const char * c, int s, const char * codepage) { void PIString::appendFromChars(const char * c, int s, const char * codepage) {
// piCout << "appendFromChars";
if (s == 0) return; if (s == 0) return;
int old_sz = size_s(); int old_sz = size_s();
if (s == -1) s = strlen(c); if (s == -1) s = strlen(c);
@@ -221,7 +222,7 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv; 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);
enlarge(ucs2.size()); enlarge(ucs2.size());
ucs2.copy((char16_t *)PIDeque<PIChar>::data(old_sz), ucs2.size()); ucs2.copy((char16_t *)d.data(old_sz), ucs2.size());
# endif # endif
#endif #endif
} }
@@ -311,21 +312,20 @@ PIString PIString::readableSize(llong bytes) {
void PIString::buildData(const char * cp) const { void PIString::buildData(const char * cp) const {
deleteData(); deleteData();
int sz = 0;
#ifdef PIP_ICU #ifdef PIP_ICU
UErrorCode e((UErrorCode)0); UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(cp, &e); UConverter * cc = ucnv_open(cp, &e);
if (cc) { if (cc) {
const size_t len = MB_CUR_MAX*size()+1; const size_t len = MB_CUR_MAX*size()+1;
data_ = (char *)malloc(len); data_ = (char *)malloc(len);
sz = ucnv_fromUChars(cc, data_, len, (const UChar*)(d.data()), d.size_s(), &e); int sz = ucnv_fromUChars(cc, data_, len, (const UChar*)(d.data()), d.size_s(), &e);
ucnv_close(cc); ucnv_close(cc);
data_[sz] = '\0'; data_[sz] = '\0';
return; return;
} }
#else #else
# ifdef WINDOWS # ifdef WINDOWS
sz = WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)d.data(), d.size_s(), 0, 0, NULL, NULL); int sz = WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)d.data(), d.size_s(), 0, 0, NULL, NULL);
if (sz <= 0) { if (sz <= 0) {
data_ = (char *)malloc(1); data_ = (char *)malloc(1);
data_[0] = '\0'; data_[0] = '\0';
@@ -337,7 +337,7 @@ void PIString::buildData(const char * cp) const {
return; return;
# else # else
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv; std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
std::string u8str = ucs2conv.to_bytes((char16_t*)PIDeque<PIChar>::data(), (char16_t*)PIDeque<PIChar>::data()+size()); std::string u8str = ucs2conv.to_bytes((char16_t*)d.data(), (char16_t*)d.data() + d.size());
data_ = (char *)malloc(u8str.size()+1); data_ = (char *)malloc(u8str.size()+1);
strcpy(data_, u8str.c_str()); strcpy(data_, u8str.c_str());
# endif # endif

View File

@@ -130,7 +130,13 @@ int main(int argc, char * argv[]) {
} }
piCout << l << "PIString::fromNumber" << tm.elapsed_m(); piCout << l << "PIString::fromNumber" << tm.elapsed_m();
tm.reset(); tm.reset();
for(int i=0; i<cc; ++i) {
PIString s = is;
l = s.size();
}
piCout << l << "PIString::assign empty" << tm.elapsed_m();
PIString is2 = PIString::fromUTF8(ba); PIString is2 = PIString::fromUTF8(ba);
tm.reset();
for(int i=0; i<cc; ++i) { for(int i=0; i<cc; ++i) {
PIString s = is; PIString s = is;
l = s.size(); l = s.size();