summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-08-16 10:33:47 +0200
committerRémi Verschelde <rverschelde@gmail.com>2024-08-16 10:33:47 +0200
commit71ca5aa5ec9e84d91714cb4b21421e4703674e66 (patch)
treebc07474ec2925fea333fd5c76cd2a0a2c8fd9ac5
parente057c49bf71aa35d0f3e0b45fafc20c919940adf (diff)
parent746c6b87eb8223ac99749f4ee6459b942e1ae261 (diff)
downloadredot-engine-71ca5aa5ec9e84d91714cb4b21421e4703674e66.tar.gz
Merge pull request #92555 from AThousandShips/insert_improve
[Core] Optimize `String::insert`
-rw-r--r--core/string/ustring.cpp22
1 files changed, 16 insertions, 6 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index b15ed7b732..c41ae1ccd8 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -3179,7 +3179,7 @@ Vector<uint8_t> String::sha256_buffer() const {
}
String String::insert(int p_at_pos, const String &p_string) const {
- if (p_at_pos < 0) {
+ if (p_string.is_empty() || p_at_pos < 0) {
return *this;
}
@@ -3187,17 +3187,27 @@ String String::insert(int p_at_pos, const String &p_string) const {
p_at_pos = length();
}
- String pre;
+ String ret;
+ ret.resize(length() + p_string.length() + 1);
+ char32_t *ret_ptrw = ret.ptrw();
+ const char32_t *this_ptr = ptr();
+
if (p_at_pos > 0) {
- pre = substr(0, p_at_pos);
+ memcpy(ret_ptrw, this_ptr, p_at_pos * sizeof(char32_t));
+ ret_ptrw += p_at_pos;
}
- String post;
+ memcpy(ret_ptrw, p_string.ptr(), p_string.length() * sizeof(char32_t));
+ ret_ptrw += p_string.length();
+
if (p_at_pos < length()) {
- post = substr(p_at_pos, length() - p_at_pos);
+ memcpy(ret_ptrw, this_ptr + p_at_pos, (length() - p_at_pos) * sizeof(char32_t));
+ ret_ptrw += length() - p_at_pos;
}
- return pre + p_string + post;
+ *ret_ptrw = 0;
+
+ return ret;
}
String String::erase(int p_pos, int p_chars) const {