Skip to content

Commit

Permalink
Helper functions for startsWith and endsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
NeeEoo committed Nov 3, 2024
1 parent 54af892 commit 32a2b61
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/hxString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions src/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 32a2b61

Please sign in to comment.