diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-02-09 09:23:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-09 09:23:55 +0100 |
commit | e9bc984bef146a73acd3060f634018a15df1b72d (patch) | |
tree | 93bdbf3e10d46a236341065b4bd509c14d2a0f24 /core/string | |
parent | 5f23fcf8479b1edaac540641cfdaf99aa5e0e226 (diff) | |
parent | ad0943e3d32d00d131a2364483dfdda9ed38319b (diff) |
Merge pull request #45736 from kleonc/string_ends_with
Make String::ends_with don't use String::rfind
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/ustring.cpp | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 0b6311dadf..59fda65d43 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -3072,39 +3072,46 @@ int String::rfindn(const String &p_str, int p_from) const { bool String::ends_with(const String &p_string) const { int l = p_string.length(); + if (l > length()) { + return false; + } + if (l == 0) { return true; } - int pos = rfind(p_string); - if (pos == -1) { - return false; + const char32_t *p = &p_string[0]; + const char32_t *s = &operator[](length() - l); + + for (int i = 0; i < l; i++) { + if (p[i] != s[i]) { + return false; + } } - return pos + l == length(); + + return true; } bool String::begins_with(const String &p_string) const { - if (p_string.length() > length()) { + int l = p_string.length(); + if (l > length()) { return false; } - int l = p_string.length(); if (l == 0) { return true; } - const char32_t *src = &p_string[0]; - const char32_t *str = &operator[](0); + const char32_t *p = &p_string[0]; + const char32_t *s = &operator[](0); - int i = 0; - for (; i < l; i++) { - if (src[i] != str[i]) { + for (int i = 0; i < l; i++) { + if (p[i] != s[i]) { return false; } } - // only if i == l the p_string matches the beginning - return i == l; + return true; } bool String::begins_with(const char *p_string) const { |