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

Improve string copy_from and copy_from_unchecked implementations, by making use of caller contracts and language spec (NULL termination and casts).

This commit is contained in:
Lukas Tenbrink
2024-11-29 01:40:12 +01:00
parent aa8d9b83f6
commit e1c42392c2

View File

@@ -329,21 +329,14 @@ void String::copy_from(const char *p_cstr) {
resize(len + 1); // include 0 resize(len + 1); // include 0
const char *end = p_cstr + len;
char32_t *dst = ptrw(); char32_t *dst = ptrw();
for (size_t i = 0; i <= len; i++) { for (; p_cstr < end; ++p_cstr, ++dst) {
#if CHAR_MIN == 0 // If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly.
uint8_t c = p_cstr[i]; *dst = static_cast<uint8_t>(*p_cstr);
#else
uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
#endif
if (c == 0 && i < len) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
} else {
dst[i] = c;
}
} }
*dst = 0;
} }
void String::copy_from(const char *p_cstr, const int p_clip_to) { void String::copy_from(const char *p_cstr, const int p_clip_to) {
@@ -366,22 +359,14 @@ void String::copy_from(const char *p_cstr, const int p_clip_to) {
resize(len + 1); // include 0 resize(len + 1); // include 0
const char *end = p_cstr + len;
char32_t *dst = ptrw(); char32_t *dst = ptrw();
for (int i = 0; i < len; i++) { for (; p_cstr < end; ++p_cstr, ++dst) {
#if CHAR_MIN == 0 // If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly.
uint8_t c = p_cstr[i]; *dst = static_cast<uint8_t>(*p_cstr);
#else
uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
#endif
if (c == 0) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
} else {
dst[i] = c;
} }
} *dst = 0;
dst[len] = 0;
} }
void String::copy_from(const wchar_t *p_cstr) { void String::copy_from(const wchar_t *p_cstr) {
@@ -469,27 +454,25 @@ void String::copy_from(const char32_t *p_cstr, const int p_clip_to) {
// p_length <= p_char strlen // p_length <= p_char strlen
void String::copy_from_unchecked(const char32_t *p_char, const int p_length) { void String::copy_from_unchecked(const char32_t *p_char, const int p_length) {
resize(p_length + 1); resize(p_length + 1);
char32_t *dst = ptrw();
dst[p_length] = 0;
for (int i = 0; i < p_length; i++) { const char32_t *end = p_char + p_length;
if (p_char[i] == 0) { char32_t *dst = ptrw();
print_unicode_error("NUL character", true);
dst[i] = _replacement_char; for (; p_char < end; ++p_char, ++dst) {
const char32_t chr = *p_char;
if ((chr & 0xfffff800) == 0xd800) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)chr));
*dst = _replacement_char;
continue; continue;
} }
if ((p_char[i] & 0xfffff800) == 0xd800) { if (chr > 0x10ffff) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char[i])); print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)chr));
dst[i] = _replacement_char; *dst = _replacement_char;
continue; continue;
} }
if (p_char[i] > 0x10ffff) { *dst = chr;
print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char[i]));
dst[i] = _replacement_char;
continue;
}
dst[i] = p_char[i];
} }
*dst = 0;
} }
void String::operator=(const char *p_str) { void String::operator=(const char *p_str) {