From a6c37d2b5d058d3bad2a942d965564c5054424c5 Mon Sep 17 00:00:00 2001 From: George Marques Date: Sun, 12 Jun 2016 00:25:01 -0300 Subject: Add 'is_subsequence_of' function to String --- core/ustring.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'core/ustring.cpp') 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) { -- cgit v1.2.3