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 ++++++++++++++++++++++++++++++++++++++++++++ core/ustring.h | 3 +++ core/variant_call.cpp | 4 ++++ 3 files changed, 51 insertions(+) (limited to 'core') 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) { diff --git a/core/ustring.h b/core/ustring.h index e03f74f506..fddb77b040 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -66,6 +66,7 @@ class String : public Vector { void copy_from(const char *p_cstr); void copy_from(const CharType* p_cstr, int p_clip_to=-1); void copy_from(const CharType& p_char); + bool _base_is_subsequence_of(const String& p_string, bool case_insensitive) const; public: @@ -122,6 +123,8 @@ public: bool begins_with(const String& p_string) const; bool begins_with(const char* p_string) const; bool ends_with(const String& p_string) const; + bool is_subsequence_of(const String& p_string) const; + bool is_subsequence_ofi(const String& p_string) const; String replace_first(String p_key,String p_with) const; String replace(String p_key,String p_with) const; String replacen(String p_key,String p_with) const; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index cc2d15b88c..94ab93d55c 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -247,6 +247,8 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM1R(String,matchn); VCALL_LOCALMEM1R(String,begins_with); VCALL_LOCALMEM1R(String,ends_with); + VCALL_LOCALMEM1R(String,is_subsequence_of); + VCALL_LOCALMEM1R(String,is_subsequence_ofi); VCALL_LOCALMEM2R(String,replace); VCALL_LOCALMEM2R(String,replacen); VCALL_LOCALMEM2R(String,insert); @@ -1269,6 +1271,8 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC1(STRING,BOOL,String,matchn,STRING,"expr",varray()); ADDFUNC1(STRING,BOOL,String,begins_with,STRING,"text",varray()); ADDFUNC1(STRING,BOOL,String,ends_with,STRING,"text",varray()); + ADDFUNC1(STRING,BOOL,String,is_subsequence_of,STRING,"text",varray()); + ADDFUNC1(STRING,BOOL,String,is_subsequence_ofi,STRING,"text",varray()); ADDFUNC2(STRING,STRING,String,replace,STRING,"what",STRING,"forwhat",varray()); ADDFUNC2(STRING,STRING,String,replacen,STRING,"what",STRING,"forwhat",varray()); -- cgit v1.2.3