Merge pull request #75510 from SilicDev/string_erase

Reimplement `String.erase()` as immutable method
This commit is contained in:
Clay John
2023-05-05 10:03:49 -07:00
committed by GitHub
6 changed files with 30 additions and 0 deletions

View File

@@ -2857,6 +2857,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;

View File

@@ -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;