diff options
author | George Marques <george@gmarqu.es> | 2018-04-22 12:29:44 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-22 12:29:44 -0300 |
commit | ca25f1e6eae9ffd498ddba05f94e341827269fea (patch) | |
tree | 66920ff079533dc4647378a9907756c638fdca5a /core/ustring.cpp | |
parent | 06c5a9ed5f728cff6e6460a67e4b14d3419d41fe (diff) | |
parent | 79ecdee49631c1571b6629005b73b0d9aa3dbc34 (diff) | |
download | redot-engine-ca25f1e6eae9ffd498ddba05f94e341827269fea.tar.gz |
Merge pull request #18176 from nikibobi/string-trim
Add string trim_prefix, trim_suffix, lstrip and rstrip methods
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r-- | core/ustring.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 0eb6cc1be4..b98e202175 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2987,6 +2987,40 @@ String String::strip_escapes() const { return substr(beg, end - beg); } +String String::lstrip(const Vector<CharType> &p_chars) const { + + int len = length(); + int beg; + + for (beg = 0; beg < len; beg++) { + + if (p_chars.find(operator[](beg)) == -1) + break; + } + + if (beg == 0) + return *this; + + return substr(beg, len - beg); +} + +String String::rstrip(const Vector<CharType> &p_chars) const { + + int len = length(); + int end; + + for (end = len - 1; end >= 0; end--) { + + if (p_chars.find(operator[](end)) == -1) + break; + } + + if (end == len - 1) + return *this; + + return substr(0, end + 1); +} + String String::simplify_path() const { String s = *this; @@ -3438,6 +3472,24 @@ String String::pad_zeros(int p_digits) const { return s; } +String String::trim_prefix(const String &p_prefix) const { + + String s = *this; + if (s.begins_with(p_prefix)) { + return s.substr(p_prefix.length(), s.length() - p_prefix.length()); + } + return s; +} + +String String::trim_suffix(const String &p_suffix) const { + + String s = *this; + if (s.ends_with(p_suffix)) { + return s.substr(0, s.length() - p_suffix.length()); + } + return s; +} + bool String::is_valid_integer() const { int len = length(); |