diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 9238e74ab18..aff0554ff4e 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -329,21 +329,14 @@ void String::copy_from(const char *p_cstr) { resize(len + 1); // include 0 + const char *end = p_cstr + len; char32_t *dst = ptrw(); - for (size_t i = 0; i <= len; i++) { -#if CHAR_MIN == 0 - uint8_t c = p_cstr[i]; -#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; - } + for (; p_cstr < end; ++p_cstr, ++dst) { + // If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly. + *dst = static_cast(*p_cstr); } + *dst = 0; } 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 + const char *end = p_cstr + len; char32_t *dst = ptrw(); - for (int i = 0; i < len; i++) { -#if CHAR_MIN == 0 - uint8_t c = p_cstr[i]; -#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; - } + for (; p_cstr < end; ++p_cstr, ++dst) { + // If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly. + *dst = static_cast(*p_cstr); } - dst[len] = 0; + *dst = 0; } 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 void String::copy_from_unchecked(const char32_t *p_char, const int p_length) { resize(p_length + 1); - char32_t *dst = ptrw(); - dst[p_length] = 0; - for (int i = 0; i < p_length; i++) { - if (p_char[i] == 0) { - print_unicode_error("NUL character", true); - dst[i] = _replacement_char; + const char32_t *end = p_char + p_length; + char32_t *dst = ptrw(); + + 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; } - if ((p_char[i] & 0xfffff800) == 0xd800) { - print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char[i])); - dst[i] = _replacement_char; + if (chr > 0x10ffff) { + print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)chr)); + *dst = _replacement_char; continue; } - if (p_char[i] > 0x10ffff) { - print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char[i])); - dst[i] = _replacement_char; - continue; - } - dst[i] = p_char[i]; + *dst = chr; } + *dst = 0; } void String::operator=(const char *p_str) {