summaryrefslogtreecommitdiff
path: root/core/ustring.cpp
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2016-06-13 13:25:56 -0300
committerGitHub <noreply@github.com>2016-06-13 13:25:56 -0300
commit20b45678293551f9fdb5a4b13ec1d5871a3d9cf8 (patch)
tree24bf08bb47ef788c7b103eadf73f5d10ecb46c52 /core/ustring.cpp
parent7127f0943df4790f49afa8fda084ea6dff705e7e (diff)
parentfeb95fa9ace04a3f2eb883e39995b962fde09561 (diff)
Merge pull request #5177 from vnen/string-subsequence
Add subsequence search to tools
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r--core/ustring.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp
index a039ba11cd..309b9e08fa 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -2752,6 +2752,50 @@ bool String::begins_with(const char* p_string) const {
}
+bool String::is_subsequence_of(const String& p_string) const {
+
+ return _base_is_subsequence_of(p_string, false);
+}
+
+bool String::is_subsequence_ofi(const String& p_string) const {
+
+ return _base_is_subsequence_of(p_string, true);
+}
+
+bool String::_base_is_subsequence_of(const String& p_string, bool case_insensitive) const {
+
+ int len=length();
+ if (len == 0) {
+ // Technically an empty string is subsequence of any string
+ return true;
+ }
+
+ if (len > p_string.length()) {
+ return false;
+ }
+
+ const CharType *src = &operator[](0);
+ const CharType *tgt = &p_string[0];
+
+ for (;*src && *tgt;tgt++) {
+ bool match = false;
+ if (case_insensitive) {
+ CharType srcc = _find_lower(*src);
+ CharType tgtc = _find_lower(*tgt);
+ match = srcc == tgtc;
+ } else {
+ match = *src == *tgt;
+ }
+ if (match) {
+ src++;
+ if(!*src) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
static bool _wildcard_match(const CharType* p_pattern, const CharType* p_string,bool p_case_sensitive) {
switch (*p_pattern) {