summaryrefslogtreecommitdiffstats
path: root/core/string
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-08-16 16:53:40 +0200
committerRémi Verschelde <rverschelde@gmail.com>2023-08-16 16:53:40 +0200
commit20e24bd2bb033d3fe019bd2a1a8c4f1590c1399b (patch)
treedeba7be1213d13ea6fc63c4874fd35178638ce0e /core/string
parent3518a30674afed7d1e69fb260e7826eb9eda0a7f (diff)
parent230385b5875643c2e162e6c4d2a27aaef95e1cc8 (diff)
downloadredot-engine-20e24bd2bb033d3fe019bd2a1a8c4f1590c1399b.tar.gz
Merge pull request #78529 from Chaosus/string_reverse
Add `String.reverse` method
Diffstat (limited to 'core/string')
-rw-r--r--core/string/ustring.cpp17
-rw-r--r--core/string/ustring.h1
2 files changed, 18 insertions, 0 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index 376d0832d4..80ca51573c 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -3635,6 +3635,23 @@ String String::repeat(int p_count) const {
return new_string;
}
+String String::reverse() const {
+ int len = length();
+ if (len <= 1) {
+ return *this;
+ }
+ String new_string;
+ new_string.resize(len + 1);
+
+ const char32_t *src = ptr();
+ char32_t *dst = new_string.ptrw();
+ for (int i = 0; i < len; i++) {
+ dst[i] = src[len - i - 1];
+ }
+ dst[len] = _null;
+ return new_string;
+}
+
String String::left(int p_len) const {
if (p_len < 0) {
p_len = length() + p_len;
diff --git a/core/string/ustring.h b/core/string/ustring.h
index 295625395d..f45392eee1 100644
--- a/core/string/ustring.h
+++ b/core/string/ustring.h
@@ -305,6 +305,7 @@ public:
String replace(const char *p_key, const char *p_with) const;
String replacen(const String &p_key, const String &p_with) const;
String repeat(int p_count) const;
+ String reverse() const;
String insert(int p_at_pos, const String &p_string) const;
String erase(int p_pos, int p_chars = 1) const;
String pad_decimals(int p_digits) const;