Improved performance of String repeat method

This commit is contained in:
VolTer 2022-08-16 11:01:36 +02:00
parent b8a64313f0
commit dae64e5361

View File

@ -3451,18 +3451,19 @@ String String::replacen(const String &p_key, const String &p_with) const {
String String::repeat(int p_count) const { String String::repeat(int p_count) const {
ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number."); ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");
String new_string; int len = length();
const char32_t *src = this->get_data(); String new_string = *this;
new_string.resize(p_count * len + 1);
new_string.resize(length() * p_count + 1); char32_t *dst = new_string.ptrw();
new_string[length() * p_count] = 0; int offset = 1;
int stride = 1;
for (int i = 0; i < p_count; i++) { while (offset < p_count) {
for (int j = 0; j < length(); j++) { memcpy(dst + offset * len, dst, stride * len * sizeof(char32_t));
new_string[i * length() + j] = src[j]; offset += stride;
} stride = MIN(stride * 2, p_count - offset);
} }
dst[p_count * len] = _null;
return new_string; return new_string;
} }