From 32a2b6119dc1926943161d5f75d03b6a8592c05e Mon Sep 17 00:00:00 2001 From: Ne_Eo Date: Sun, 3 Nov 2024 22:30:17 +0100 Subject: [PATCH] Helper functions for startsWith and endsWith --- include/hxString.h | 3 +++ src/String.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/hxString.h b/include/hxString.h index b53ddd4a3..b440f6b14 100644 --- a/include/hxString.h +++ b/include/hxString.h @@ -267,6 +267,9 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES String } #endif + bool __StartsWith(const String &inValue) const; + bool __EndsWith(const String &inValue) const; + ::String &operator+=(const ::String &inRHS); ::String operator+(const ::String &inRHS) const; diff --git a/src/String.cpp b/src/String.cpp index a9a8cb8af..cd4e74f89 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -1676,6 +1676,48 @@ int String::compare(const ::String &inRHS) const } #endif +bool String::__StartsWith(const String &inValue) const +{ + int l = inValue.length; + + if (l > length) + return false; + + bool s016 = isUTF16Encoded(); + bool s116 = inValue.isUTF16Encoded(); + if (s016 && s116) + { + return memcmp(__w, inValue.__w, l * sizeof(char16_t)) == 0; + } + else if (s016 || s116) + { + return s016 ? StrMatch(__w, inValue.__s, l) : + StrMatch(inValue.__w, __s, l); + } + return memcmp(__s, inValue.__s, l) == 0; +} + +bool String::__EndsWith(const String &inValue) const +{ + int l = inValue.length; + + if (l > length) + return false; + + bool s016 = isUTF16Encoded(); + bool s116 = inValue.isUTF16Encoded(); + if (s016 && s116) + { + return memcmp(__w + (length - l), inValue.__w, l * sizeof(char16_t)) == 0; + } + else if (s016 || s116) + { + return s016 ? StrMatch(__w + (length - l), inValue.__s, l) : + StrMatch(inValue.__w, __s + (length - l), l); + } + return memcmp(__s + (length - l), inValue.__s, l) == 0; +} + namespace hx {