Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add String.reverse method #78529

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/string/ustring.cpp
Expand Up @@ -3616,6 +3616,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;
Expand Down
1 change: 1 addition & 0 deletions core/string/ustring.h
Expand Up @@ -304,6 +304,7 @@ class String {
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;
Expand Down
1 change: 1 addition & 0 deletions core/variant/variant_call.cpp
Expand Up @@ -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());
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/String.xml
Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/StringName.xml
Expand Up @@ -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" />
Expand Down
5 changes: 5 additions & 0 deletions tests/core/string/test_string.h
Expand Up @@ -1607,6 +1607,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";
Expand Down