From 6fa4f71ca686e68667f61185dcbe4ebb0f64af06 Mon Sep 17 00:00:00 2001 From: SilicDev <83308290+SilicDev@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:45:45 +0200 Subject: [PATCH] Reimplement String.erase --- core/string/ustring.cpp | 6 ++++++ core/string/ustring.h | 1 + core/variant/variant_call.cpp | 1 + doc/classes/String.xml | 8 ++++++++ doc/classes/StringName.xml | 8 ++++++++ tests/core/string/test_string.h | 6 ++++++ 6 files changed, 30 insertions(+) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 773445edb69..ae8485fcbe9 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -2840,6 +2840,12 @@ String String::insert(int p_at_pos, const String &p_string) const { return pre + p_string + post; } +String String::erase(int p_pos, int p_chars) const { + ERR_FAIL_COND_V_MSG(p_pos < 0, "", vformat("Invalid starting position for `String.erase()`: %d. Starting position must be positive or zero.", p_pos)); + ERR_FAIL_COND_V_MSG(p_chars < 0, "", vformat("Invalid character count for `String.erase()`: %d. Character count must be positive or zero.", p_chars)); + return left(p_pos) + substr(p_pos + p_chars); +} + String String::substr(int p_from, int p_chars) const { if (p_chars == -1) { p_chars = length() - p_from; diff --git a/core/string/ustring.h b/core/string/ustring.h index 90034b1b07a..42d97406bf0 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -304,6 +304,7 @@ public: String replacen(const String &p_key, const String &p_with) const; String repeat(int p_count) 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; String pad_zeros(int p_digits) const; String trim_prefix(const String &p_prefix) const; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 13e9da37f23..eb113060554 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_method(replacen, sarray("what", "forwhat"), varray()); bind_string_method(repeat, sarray("count"), varray()); bind_string_method(insert, sarray("position", "what"), varray()); + bind_string_method(erase, sarray("position", "chars"), varray(1)); bind_string_method(capitalize, sarray(), varray()); bind_string_method(to_camel_case, sarray(), varray()); bind_string_method(to_pascal_case, sarray(), varray()); diff --git a/doc/classes/String.xml b/doc/classes/String.xml index fd50b308c3d..d771566688d 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -175,6 +175,14 @@ Returns [code]true[/code] if the string ends with the given [param text]. See also [method begins_with]. + + + + + + Returns a string with [param chars] characters erased starting from [param position]. If [param chars] goes beyond the string's length given the specified [param position], fewer characters will be erased from the returned string. Returns an empty string if either [code]position[/code] or [code]chars[/code] is negative. Returns the original string unmodified if [param chars] is [code]0[/code]. + + diff --git a/doc/classes/StringName.xml b/doc/classes/StringName.xml index 5b630a092ee..a74b78f7e31 100644 --- a/doc/classes/StringName.xml +++ b/doc/classes/StringName.xml @@ -158,6 +158,14 @@ Returns [code]true[/code] if the string ends with the given [param text]. See also [method begins_with]. + + + + + + Returns a string with [param chars] characters erased starting from [param position]. If [param chars] goes beyond the string's length given the specified [param position], fewer characters will be erased from the returned string. Returns an empty string if either [code]position[/code] or [code]chars[/code] is negative. Returns the original string unmodified if [param chars] is [code]0[/code]. + + diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 5d19b5a1649..036daf13f88 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -395,6 +395,12 @@ TEST_CASE("[String] Insertion") { CHECK(s == "Who is Frederic Chopin?"); } +TEST_CASE("[String] Erasing") { + String s = "Josephine is such a cute girl!"; + s = s.erase(s.find("cute "), String("cute ").length()); + CHECK(s == "Josephine is such a girl!"); +} + TEST_CASE("[String] Number to string") { CHECK(String::num(0) == "0"); CHECK(String::num(0.0) == "0"); // No trailing zeros.