Optimize String::copy_from(const CharType &p_char)

This commit is contained in:
Bartłomiej T. Listwon 2022-02-18 12:36:02 +01:00
parent 3d35f29f27
commit 66085e210e

View file

@ -220,11 +220,7 @@ void String::copy_from(const char *p_cstr) {
return; return;
} }
int len = 0; const size_t len = strlen(p_cstr);
const char *ptr = p_cstr;
while (*(ptr++) != 0) {
len++;
}
if (len == 0) { if (len == 0) {
resize(0); resize(0);
@ -235,7 +231,7 @@ void String::copy_from(const char *p_cstr) {
CharType *dst = this->ptrw(); CharType *dst = this->ptrw();
for (int i = 0; i < len + 1; i++) { for (size_t i = 0; i <= len; i++) {
dst[i] = p_cstr[i]; dst[i] = p_cstr[i];
} }
} }
@ -274,8 +270,9 @@ void String::copy_from_unchecked(const CharType *p_char, const int p_length) {
void String::copy_from(const CharType &p_char) { void String::copy_from(const CharType &p_char) {
resize(2); resize(2);
set(0, p_char); CharType *dst = ptrw();
set(1, 0); dst[0] = p_char;
dst[1] = 0;
} }
bool String::operator==(const String &p_str) const { bool String::operator==(const String &p_str) const {