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

Rename String::resize to resize_uninitialized, to better communicate to callers that new characters must be initialized.

This commit is contained in:
Lukas Tenbrink
2025-05-28 20:33:21 +02:00
parent 51b0379e55
commit b13a0e1834
26 changed files with 103 additions and 98 deletions

View File

@@ -175,7 +175,7 @@ void String::append_latin1(const Span<char> &p_cstr) {
}
const int prev_length = length();
resize(prev_length + p_cstr.size() + 1); // include 0
resize_uninitialized(prev_length + p_cstr.size() + 1); // include 0
const char *src = p_cstr.ptr();
const char *end = src + p_cstr.size();
@@ -194,7 +194,7 @@ void String::append_utf32(const Span<char32_t> &p_cstr) {
}
const int prev_length = length();
resize(prev_length + p_cstr.size() + 1);
resize_uninitialized(prev_length + p_cstr.size() + 1);
const char32_t *src = p_cstr.ptr();
const char32_t *end = p_cstr.ptr() + p_cstr.size();
char32_t *dst = ptrw() + prev_length;
@@ -223,7 +223,7 @@ void String::append_utf32(const Span<char32_t> &p_cstr) {
// 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(p_length + 1); // + 1 for \0
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;
@@ -1365,7 +1365,7 @@ String String::join(const Vector<String> &parts) const {
new_size += 1;
String ret;
ret.resize(new_size);
ret.resize_uninitialized(new_size);
char32_t *ret_ptrw = ret.ptrw();
const char32_t *this_ptr = ptr();
@@ -1404,7 +1404,7 @@ String String::to_upper() const {
}
String upper;
upper.resize(size());
upper.resize_uninitialized(size());
const char32_t *old_ptr = ptr();
char32_t *upper_ptrw = upper.ptrw();
@@ -1423,7 +1423,7 @@ String String::to_lower() const {
}
String lower;
lower.resize(size());
lower.resize_uninitialized(size());
const char32_t *old_ptr = ptr();
char32_t *lower_ptrw = lower.ptrw();
@@ -1549,7 +1549,7 @@ String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
chars++;
}
String s;
s.resize(chars + 1);
s.resize_uninitialized(chars + 1);
char32_t *c = s.ptrw();
c[chars] = 0;
n = p_num;
@@ -1584,7 +1584,7 @@ String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
} while (n);
String s;
s.resize(chars + 1);
s.resize_uninitialized(chars + 1);
char32_t *c = s.ptrw();
c[chars] = 0;
n = p_num;
@@ -1675,7 +1675,7 @@ String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
String ret;
ret.resize(p_len * 2 + 1);
ret.resize_uninitialized(p_len * 2 + 1);
char32_t *ret_ptrw = ret.ptrw();
for (int i = 0; i < p_len; i++) {
@@ -1706,7 +1706,7 @@ Vector<uint8_t> String::hex_decode() const {
Vector<uint8_t> out;
int len = length() / 2;
out.resize(len);
out.resize_uninitialized(len);
uint8_t *out_ptrw = out.ptrw();
for (int i = 0; i < len; i++) {
char32_t c;
@@ -1732,7 +1732,7 @@ CharString String::ascii(bool p_allow_extended) const {
}
CharString cs;
cs.resize(size());
cs.resize_uninitialized(size());
char *cs_ptrw = cs.ptrw();
const char32_t *this_ptr = ptr();
@@ -1755,7 +1755,7 @@ Error String::append_ascii(const Span<char> &p_range) {
}
const int prev_length = length();
resize(prev_length + p_range.size() + 1); // Include \0
resize_uninitialized(prev_length + p_range.size() + 1); // Include \0
const char *src = p_range.ptr();
const char *end = src + p_range.size();
@@ -1800,7 +1800,7 @@ Error String::append_utf8(const char *p_utf8, int p_len, bool p_skip_cr) {
const int prev_length = length();
// If all utf8 characters maps to ASCII, then the max size will be p_len, and we add +1 for the null termination.
resize(prev_length + p_len + 1);
resize_uninitialized(prev_length + p_len + 1);
char32_t *dst = ptrw() + prev_length;
Error result = Error::OK;
@@ -1946,7 +1946,7 @@ Error String::append_utf8(const char *p_utf8, int p_len, bool p_skip_cr) {
}
(*dst++) = 0;
resize(prev_length + dst - ptr());
resize_uninitialized(prev_length + dst - ptr());
return result;
}
@@ -1959,7 +1959,7 @@ CharString String::utf8(Vector<uint8_t> *r_ch_length_map) const {
uint8_t *map_ptr = nullptr;
if (r_ch_length_map) {
r_ch_length_map->resize(l);
r_ch_length_map->resize_uninitialized(l);
map_ptr = r_ch_length_map->ptrw();
}
@@ -1997,7 +1997,7 @@ CharString String::utf8(Vector<uint8_t> *r_ch_length_map) const {
return utf8s;
}
utf8s.resize(fl + 1);
utf8s.resize_uninitialized(fl + 1);
uint8_t *cdst = (uint8_t *)utf8s.get_data();
#define APPEND_CHAR(m_c) *(cdst++) = m_c
@@ -2124,7 +2124,7 @@ Error String::append_utf16(const char16_t *p_utf16, int p_len, bool p_default_li
}
const int prev_length = length();
resize(prev_length + str_size + 1);
resize_uninitialized(prev_length + str_size + 1);
char32_t *dst = ptrw() + prev_length;
dst[str_size] = 0;
@@ -2194,7 +2194,7 @@ Char16String String::utf16() const {
return utf16s;
}
utf16s.resize(fl + 1);
utf16s.resize_uninitialized(fl + 1);
uint16_t *cdst = (uint16_t *)utf16s.get_data();
#define APPEND_CHAR(m_c) *(cdst++) = m_c
@@ -2847,7 +2847,7 @@ Vector<uint8_t> String::md5_buffer() const {
CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
Vector<uint8_t> ret;
ret.resize(16);
ret.resize_uninitialized(16);
uint8_t *ret_ptrw = ret.ptrw();
for (int i = 0; i < 16; i++) {
ret_ptrw[i] = hash[i];
@@ -2861,7 +2861,7 @@ Vector<uint8_t> String::sha1_buffer() const {
CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
Vector<uint8_t> ret;
ret.resize(20);
ret.resize_uninitialized(20);
uint8_t *ret_ptrw = ret.ptrw();
for (int i = 0; i < 20; i++) {
ret_ptrw[i] = hash[i];
@@ -2876,7 +2876,7 @@ Vector<uint8_t> String::sha256_buffer() const {
CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
Vector<uint8_t> ret;
ret.resize(32);
ret.resize_uninitialized(32);
uint8_t *ret_ptrw = ret.ptrw();
for (int i = 0; i < 32; i++) {
ret_ptrw[i] = hash[i];
@@ -2894,7 +2894,7 @@ String String::insert(int p_at_pos, const String &p_string) const {
}
String ret;
ret.resize(length() + p_string.length() + 1);
ret.resize_uninitialized(length() + p_string.length() + 1);
char32_t *ret_ptrw = ret.ptrw();
const char32_t *this_ptr = ptr();
@@ -2958,7 +2958,7 @@ String String::remove_char(char32_t p_char) const {
// If we found at least one occurrence of `char`, create new string, allocating enough space for the current length minus one.
String new_string;
new_string.resize(len);
new_string.resize_uninitialized(len);
char32_t *new_ptr = new_string.ptrw();
// Copy part of input before `char`.
@@ -2978,7 +2978,7 @@ String String::remove_char(char32_t p_char) const {
new_ptr[new_size] = _null;
// Shrink new string to fit.
new_string.resize(new_size + 1);
new_string.resize_uninitialized(new_size + 1);
return new_string;
}
@@ -3013,7 +3013,7 @@ static String _remove_chars_common(const String &p_this, const T *p_chars, int p
// If we found at least one occurrence of `chars`, create new string, allocating enough space for the current length minus one.
String new_string;
new_string.resize(len);
new_string.resize_uninitialized(len);
char32_t *new_ptr = new_string.ptrw();
// Copy part of input before `char`.
@@ -3033,7 +3033,7 @@ static String _remove_chars_common(const String &p_this, const T *p_chars, int p
new_ptr[new_size] = 0;
// Shrink new string to fit.
new_string.resize(new_size + 1);
new_string.resize_uninitialized(new_size + 1);
return new_string;
}
@@ -3747,7 +3747,7 @@ Vector<String> String::bigrams() const {
if (n_pairs <= 0) {
return b;
}
b.resize(n_pairs);
b.resize_initialized(n_pairs);
String *b_ptrw = b.ptrw();
for (int i = 0; i < n_pairs; i++) {
b_ptrw[i] = substr(i, 2);
@@ -3893,7 +3893,7 @@ static String _replace_common(const String &p_this, const String &p_key, const S
const int with_length = p_with.length();
const int old_length = p_this.length();
new_string.resize(old_length + int(found.size()) * (with_length - key_length) + 1);
new_string.resize_uninitialized(old_length + int(found.size()) * (with_length - key_length) + 1);
char32_t *new_ptrw = new_string.ptrw();
const char32_t *old_ptr = p_this.ptr();
@@ -3952,7 +3952,7 @@ static String _replace_common(const String &p_this, char const *p_key, char cons
const int with_length = with_string.length();
const int old_length = p_this.length();
new_string.resize(old_length + int(found.size()) * (with_length - key_length) + 1);
new_string.resize_uninitialized(old_length + int(found.size()) * (with_length - key_length) + 1);
char32_t *new_ptrw = new_string.ptrw();
const char32_t *old_ptr = p_this.ptr();
@@ -3998,7 +3998,7 @@ String String::replace_first(const String &p_key, const String &p_with) const {
const int with_length = p_with.length();
String new_string;
new_string.resize(old_length + (with_length - key_length) + 1);
new_string.resize_uninitialized(old_length + (with_length - key_length) + 1);
char32_t *new_ptrw = new_string.ptrw();
const char32_t *old_ptr = ptr();
@@ -4036,7 +4036,7 @@ String String::replace_first(const char *p_key, const char *p_with) const {
const int with_length = strlen(p_with);
String new_string;
new_string.resize(old_length + (with_length - key_length) + 1);
new_string.resize_uninitialized(old_length + (with_length - key_length) + 1);
char32_t *new_ptrw = new_string.ptrw();
const char32_t *old_ptr = ptr();
@@ -4091,7 +4091,7 @@ String String::replace_char(char32_t p_key, char32_t p_with) const {
// If we found at least one occurrence of `key`, create new string.
String new_string;
new_string.resize(len + 1);
new_string.resize_uninitialized(len + 1);
char32_t *new_ptr = new_string.ptrw();
// Copy part of input before `key`.
@@ -4144,7 +4144,7 @@ static String _replace_chars_common(const String &p_this, const T *p_keys, int p
// If we found at least one occurrence of `keys`, create new string.
String new_string;
new_string.resize(len + 1);
new_string.resize_uninitialized(len + 1);
char32_t *new_ptr = new_string.ptrw();
// Copy part of input before `key`.
@@ -4196,7 +4196,7 @@ String String::repeat(int p_count) const {
int len = length();
String new_string = *this;
new_string.resize(p_count * len + 1);
new_string.resize_uninitialized(p_count * len + 1);
char32_t *dst = new_string.ptrw();
int offset = 1;
@@ -4216,7 +4216,7 @@ String String::reverse() const {
return *this;
}
String new_string;
new_string.resize(len + 1);
new_string.resize_uninitialized(len + 1);
const char32_t *src = ptr();
char32_t *dst = new_string.ptrw();
@@ -4909,7 +4909,7 @@ String String::xml_unescape() const {
if (len == 0) {
return String();
}
str.resize(len + 1);
str.resize_uninitialized(len + 1);
char32_t *str_ptrw = str.ptrw();
_xml_unescape(get_data(), l, str_ptrw);
str_ptrw[len] = 0;
@@ -5880,7 +5880,7 @@ Vector<uint8_t> String::to_ascii_buffer() const {
Vector<uint8_t> retval;
size_t len = charstr.length();
retval.resize(len);
retval.resize_uninitialized(len);
uint8_t *w = retval.ptrw();
memcpy(w, charstr.ptr(), len);
@@ -5896,7 +5896,7 @@ Vector<uint8_t> String::to_utf8_buffer() const {
Vector<uint8_t> retval;
size_t len = charstr.length();
retval.resize(len);
retval.resize_uninitialized(len);
uint8_t *w = retval.ptrw();
memcpy(w, charstr.ptr(), len);
@@ -5912,7 +5912,7 @@ Vector<uint8_t> String::to_utf16_buffer() const {
Vector<uint8_t> retval;
size_t len = charstr.length() * sizeof(char16_t);
retval.resize(len);
retval.resize_uninitialized(len);
uint8_t *w = retval.ptrw();
memcpy(w, (const void *)charstr.ptr(), len);
@@ -5927,7 +5927,7 @@ Vector<uint8_t> String::to_utf32_buffer() const {
Vector<uint8_t> retval;
size_t len = s->length() * sizeof(char32_t);
retval.resize(len);
retval.resize_uninitialized(len);
uint8_t *w = retval.ptrw();
memcpy(w, (const void *)s->ptr(), len);