Optimize String::operator+= and CharString::operator+=
This commit is contained in:
parent
7384475493
commit
069e8f32d8
1 changed files with 25 additions and 30 deletions
|
@ -108,9 +108,12 @@ bool CharString::operator<(const CharString &p_right) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
CharString &CharString::operator+=(char p_char) {
|
CharString &CharString::operator+=(char p_char) {
|
||||||
resize(size() ? size() + 1 : 2);
|
const int lhs_len = length();
|
||||||
set(length(), 0);
|
resize(lhs_len + 2);
|
||||||
set(length() - 1, p_char);
|
|
||||||
|
char *dst = ptrw();
|
||||||
|
dst[lhs_len] = p_char;
|
||||||
|
dst[lhs_len + 1] = 0;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -312,27 +315,23 @@ String String::operator+(const String &p_str) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
String &String::operator+=(const String &p_str) {
|
String &String::operator+=(const String &p_str) {
|
||||||
if (empty()) {
|
const int lhs_len = length();
|
||||||
|
if (lhs_len == 0) {
|
||||||
*this = p_str;
|
*this = p_str;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p_str.empty()) {
|
const int rhs_len = p_str.length();
|
||||||
|
if (rhs_len == 0) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
int from = length();
|
resize(lhs_len + rhs_len + 1);
|
||||||
|
|
||||||
resize(length() + p_str.size());
|
|
||||||
|
|
||||||
const CharType *src = p_str.c_str();
|
const CharType *src = p_str.c_str();
|
||||||
CharType *dst = ptrw();
|
CharType *dst = ptrw() + lhs_len;
|
||||||
|
|
||||||
set(length(), 0);
|
memcpy(dst, src, (rhs_len + 1) * sizeof(CharType));
|
||||||
|
|
||||||
for (int i = 0; i < p_str.length(); i++) {
|
|
||||||
dst[from + i] = src[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -343,9 +342,12 @@ String &String::operator+=(const CharType *p_str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
String &String::operator+=(CharType p_char) {
|
String &String::operator+=(CharType p_char) {
|
||||||
resize(size() ? size() + 1 : 2);
|
const int lhs_len = length();
|
||||||
set(length(), 0);
|
resize(lhs_len + 2);
|
||||||
set(length() - 1, p_char);
|
|
||||||
|
CharType *dst = ptrw();
|
||||||
|
dst[lhs_len] = p_char;
|
||||||
|
dst[lhs_len + 1] = 0;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -355,22 +357,15 @@ String &String::operator+=(const char *p_str) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
int src_len = 0;
|
const size_t rhs_len = strlen(p_str);
|
||||||
const char *ptr = p_str;
|
const int lhs_len = length();
|
||||||
while (*(ptr++) != 0) {
|
|
||||||
src_len++;
|
|
||||||
}
|
|
||||||
|
|
||||||
int from = length();
|
resize(lhs_len + rhs_len + 1);
|
||||||
|
|
||||||
resize(from + src_len + 1);
|
CharType *dst = ptrw() + lhs_len;
|
||||||
|
|
||||||
CharType *dst = ptrw();
|
for (size_t i = 0; i <= rhs_len; i++) {
|
||||||
|
dst[i] = p_str[i];
|
||||||
set(length(), 0);
|
|
||||||
|
|
||||||
for (int i = 0; i < src_len; i++) {
|
|
||||||
dst[from + i] = p_str[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
|
|
Loading…
Reference in a new issue