1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Expose copy_from_unchecked as append_utf32_unchecked and String::utf32_unchecked in String for high performance string copies. Expose append_wstring and String::wstring for platform strings.

This commit is contained in:
Lukas Tenbrink
2025-03-29 23:27:21 +01:00
parent 8b4b93a82e
commit d1fd42bf3c
2 changed files with 44 additions and 31 deletions

View File

@@ -166,11 +166,13 @@ void String::append_latin1(const Span<char> &p_cstr) {
*dst = 0;
}
void String::append_utf32(const Span<char32_t> &p_cstr) {
Error String::append_utf32(const Span<char32_t> &p_cstr) {
if (p_cstr.is_empty()) {
return;
return OK;
}
Error error = OK;
const int prev_length = length();
resize_uninitialized(prev_length + p_cstr.size() + 1);
const char32_t *src = p_cstr.ptr();
@@ -184,29 +186,29 @@ void String::append_utf32(const Span<char32_t> &p_cstr) {
// NUL in string is allowed by the unicode standard, but unsupported in our implementation right now.
print_unicode_error("Unexpected NUL character", true);
*dst = _replacement_char;
error = ERR_PARSE_ERROR;
} else if (unlikely((chr & 0xfffff800) == 0xd800)) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)chr), true);
*dst = _replacement_char;
error = ERR_PARSE_ERROR;
} else if (unlikely(chr > 0x10ffff)) {
print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)chr), true);
*dst = _replacement_char;
error = ERR_PARSE_ERROR;
} else {
*dst = chr;
}
}
*dst = 0;
return error;
}
// assumes the following have already been validated:
// p_char != nullptr
// p_length > 0
// p_length <= p_char strlen
// p_char is a valid UTF32 string
void String::copy_from_unchecked(const char32_t *p_char, const int p_length) {
resize_uninitialized(p_length + 1); // + 1 for \0
char32_t *dst = ptrw();
memcpy(dst, p_char, p_length * sizeof(char32_t));
*(dst + p_length) = _null;
void String::append_utf32_unchecked(const Span<char32_t> &p_span) {
const int prev_length = length();
resize_uninitialized(prev_length + p_span.size() + 1); // + 1 for \0
char32_t *dst = ptrw() + prev_length;
memcpy(dst, p_span.ptr(), p_span.size() * sizeof(char32_t));
*(dst + p_span.size()) = _null;
}
String String::operator+(const String &p_str) const {
@@ -266,7 +268,7 @@ String &String::operator+=(const String &p_str) {
*this = p_str;
return *this;
}
append_utf32(p_str);
append_utf32_unchecked(p_str);
return *this;
}
@@ -3048,7 +3050,7 @@ String String::substr(int p_from, int p_chars) const {
}
String s;
s.copy_from_unchecked(&get_data()[p_from], p_chars);
s.append_utf32_unchecked(Span(ptr() + p_from, p_chars));
return s;
}
@@ -4225,7 +4227,7 @@ String String::left(int p_len) const {
}
String s;
s.copy_from_unchecked(&get_data()[0], p_len);
s.append_utf32_unchecked(Span(ptr(), p_len));
return s;
}
@@ -4243,7 +4245,7 @@ String String::right(int p_len) const {
}
String s;
s.copy_from_unchecked(&get_data()[length() - p_len], p_len);
s.append_utf32_unchecked(Span(ptr() + length() - p_len, p_len));
return s;
}