diff options
author | Chaosus <chaosus89@gmail.com> | 2019-01-18 11:29:28 +0300 |
---|---|---|
committer | Chaosus <chaosus89@gmail.com> | 2019-07-23 18:55:54 +0300 |
commit | 080c0bb7fea824f231e8972fe1e7f82290a2f453 (patch) | |
tree | 70c99fe65ad4e7a4d294206e1c65345e6f30116b /core | |
parent | c7a427241e7f4dd6ab9ac89f51d3bb9f6632008b (diff) |
Added count method to String
Diffstat (limited to 'core')
-rw-r--r-- | core/ustring.cpp | 45 | ||||
-rw-r--r-- | core/ustring.h | 4 | ||||
-rw-r--r-- | core/variant_call.cpp | 5 |
3 files changed, 54 insertions, 0 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 75e3b6f22e..ed401c3763 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2729,6 +2729,51 @@ bool String::is_quoted() const { return is_enclosed_in("\"") || is_enclosed_in("'"); } +int String::_count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const { + if (p_string.empty()) { + return 0; + } + int len = length(); + int slen = p_string.length(); + if (len < slen) { + return 0; + } + String str; + if (p_from >= 0 && p_to >= 0) { + if (p_to == 0) { + p_to = len; + } else if (p_from >= p_to) { + return 0; + } + if (p_from == 0 && p_to == len) { + str = String(); + str.copy_from_unchecked(&c_str()[0], len); + } else { + str = substr(p_from, p_to - p_from); + } + } else { + return 0; + } + int c = 0; + int idx = -1; + do { + idx = p_case_insensitive ? str.findn(p_string) : str.find(p_string); + if (idx != -1) { + str = str.substr(idx + slen, str.length() - slen); + ++c; + } + } while (idx != -1); + return c; +} + +int String::count(const String &p_string, int p_from, int p_to) const { + return _count(p_string, p_from, p_to, false); +} + +int String::countn(const String &p_string, int p_from, int p_to) const { + return _count(p_string, p_from, p_to, true); +} + bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const { int len = length(); diff --git a/core/ustring.h b/core/ustring.h index 8a52c53238..3eb5c47b3a 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -137,6 +137,7 @@ class String { void copy_from(const CharType &p_char); void copy_from_unchecked(const CharType *p_char, const int p_length); bool _base_is_subsequence_of(const String &p_string, bool case_insensitive) const; + int _count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const; public: enum { @@ -279,6 +280,9 @@ public: String to_upper() const; String to_lower() const; + int count(const String &p_string, int p_from = 0, int p_to = 0) const; + int countn(const String &p_string, int p_from = 0, int p_to = 0) const; + String left(int p_pos) const; String right(int p_pos) const; String dedent() const; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index b637e745af..377cc889b8 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -237,6 +237,8 @@ struct _VariantCall { VCALL_LOCALMEM1R(String, casecmp_to); VCALL_LOCALMEM1R(String, nocasecmp_to); VCALL_LOCALMEM0R(String, length); + VCALL_LOCALMEM3R(String, count); + VCALL_LOCALMEM3R(String, countn); VCALL_LOCALMEM2R(String, substr); VCALL_LOCALMEM2R(String, find); VCALL_LOCALMEM1R(String, find_last); @@ -1502,6 +1504,9 @@ void register_variant_methods() { ADDFUNC2R(STRING, INT, String, find, STRING, "what", INT, "from", varray(0)); + ADDFUNC3R(STRING, INT, String, count, STRING, "what", INT, "from", INT, "to", varray(0, 0)); + ADDFUNC3R(STRING, INT, String, countn, STRING, "what", INT, "from", INT, "to", varray(0, 0)); + ADDFUNC1R(STRING, INT, String, find_last, STRING, "what", varray()); ADDFUNC2R(STRING, INT, String, findn, STRING, "what", INT, "from", varray(0)); ADDFUNC2R(STRING, INT, String, rfind, STRING, "what", INT, "from", varray(-1)); |