diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-08-16 16:53:40 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-08-16 16:53:40 +0200 |
commit | 20e24bd2bb033d3fe019bd2a1a8c4f1590c1399b (patch) | |
tree | deba7be1213d13ea6fc63c4874fd35178638ce0e | |
parent | 3518a30674afed7d1e69fb260e7826eb9eda0a7f (diff) | |
parent | 230385b5875643c2e162e6c4d2a27aaef95e1cc8 (diff) | |
download | redot-engine-20e24bd2bb033d3fe019bd2a1a8c4f1590c1399b.tar.gz |
Merge pull request #78529 from Chaosus/string_reverse
Add `String.reverse` method
-rw-r--r-- | core/string/ustring.cpp | 17 | ||||
-rw-r--r-- | core/string/ustring.h | 1 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 1 | ||||
-rw-r--r-- | doc/classes/String.xml | 6 | ||||
-rw-r--r-- | doc/classes/StringName.xml | 6 | ||||
-rw-r--r-- | tests/core/string/test_string.h | 5 |
6 files changed, 36 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; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index dad9183216..ccf9b82022 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1659,6 +1659,7 @@ static void _register_variant_builtin_methods() { bind_string_methodv(replace, static_cast<String (String::*)(const String &, const String &) const>(&String::replace), sarray("what", "forwhat"), varray()); bind_string_method(replacen, sarray("what", "forwhat"), varray()); bind_string_method(repeat, sarray("count"), varray()); + bind_string_method(reverse, sarray(), varray()); bind_string_method(insert, sarray("position", "what"), varray()); bind_string_method(erase, sarray("position", "chars"), varray(1)); bind_string_method(capitalize, sarray(), varray()); diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 913f2f2654..9c31576b72 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -720,6 +720,12 @@ Replaces all [b]case-insensitive[/b] occurrences of [param what] inside the string with the given [param forwhat]. </description> </method> + <method name="reverse" qualifiers="const"> + <return type="String" /> + <description> + Returns the copy of this string in reverse order. + </description> + </method> <method name="rfind" qualifiers="const"> <return type="int" /> <param index="0" name="what" type="String" /> diff --git a/doc/classes/StringName.xml b/doc/classes/StringName.xml index 0814a30a8d..1c5032be9b 100644 --- a/doc/classes/StringName.xml +++ b/doc/classes/StringName.xml @@ -627,6 +627,12 @@ Replaces all [b]case-insensitive[/b] occurrences of [param what] inside the string with the given [param forwhat]. </description> </method> + <method name="reverse" qualifiers="const"> + <return type="String" /> + <description> + Returns the copy of this string in reverse order. + </description> + </method> <method name="rfind" qualifiers="const"> <return type="int" /> <param index="0" name="what" type="String" /> diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index fc1e0590fc..659fb003d3 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -1609,6 +1609,11 @@ TEST_CASE("[String] Repeat") { CHECK(t == s); } +TEST_CASE("[String] Reverse") { + String s = "Abcd"; + CHECK(s.reverse() == "dcbA"); +} + TEST_CASE("[String] SHA1/SHA256/MD5") { String s = "Godot"; String sha1 = "a1e91f39b9fce6a9998b14bdbe2aa2b39dc2d201"; |