Core: String / char * literal comparison optimised

git-svn-id: svn://ultimatepp.org/upp/trunk@14540 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2020-06-03 09:30:29 +00:00
parent fd4608b573
commit 53e2e04ccd
3 changed files with 17 additions and 8 deletions

View file

@ -262,6 +262,15 @@ void String0::Set(const char *s, int len)
Dsyn();
}
inline
bool String0::IsEqual(const char *s) const
{ // This optimized for comparison with string literals...
int len = strlen(s);
if(len != GetCount()) return false;
const char *t = begin();
return memcmp(begin(), s, len) == 0; // compiler is happy to optimize memcmp out...
}
force_inline
String& String::operator=(const char *s)
{

View file

@ -76,7 +76,7 @@ public:
int Compare(const tchar *s) const;
bool IsEqual(const String& s) const { return B::IsEqual(s); }
bool IsEqual(const tchar *s) const { return Compare(s) == 0; }
bool IsEqual(const tchar *s) const { return B::IsEqual(s); }
String Mid(int pos, int length) const;
String Mid(int pos) const { return Mid(pos, GetLength() - pos); }
@ -150,10 +150,10 @@ public:
friend bool operator==(const String& a, const String& b) { return a.IsEqual(b); }
friend bool operator!=(const String& a, const String& b) { return !a.IsEqual(b); }
friend bool operator==(const String& a, const tchar *b) { return a.Compare(b) == 0; }
friend bool operator==(const tchar *a, const String& b) { return b.Compare(a) == 0; }
friend bool operator!=(const String& a, const tchar *b) { return a.Compare(b) != 0; }
friend bool operator!=(const tchar *a, const String& b) { return b.Compare(a) != 0; }
friend bool operator==(const String& a, const tchar *b) { return a.IsEqual(b); }
friend bool operator==(const tchar *a, const String& b) { return b.IsEqual(a); }
friend bool operator!=(const String& a, const tchar *b) { return !a.IsEqual(b); }
friend bool operator!=(const tchar *a, const String& b) { return !b.IsEqual(a); }
friend String operator+(const String& a, const String& b) { String c(a); c += b; return c; }
friend String operator+(const String& a, const tchar *b) { String c(a); c += b; return c; }
@ -282,6 +282,7 @@ public:
uint64 sq1 = s.q[1];
return q1 == sq1 && q[0] == s.q[0] || ((q1 | sq1) & I64(0x00ff000000000000)) && LEq(s);
}
bool IsEqual(const char *s) const;
int Compare(const String0& s) const;
@ -761,6 +762,7 @@ public:
hash_t GetHashValue() const { return memhash(ptr, length * sizeof(wchar)); }
bool IsEqual(const WString0& s) const { return s.length == length && memeq16(ptr, s.ptr, length); }
bool IsEqual(const wchar *s) const { int l = wstrlen(s); return l == GetCount() && memeq16(begin(), s, l); }
int Compare(const WString0& s) const;
void Remove(int pos, int count = 1);

View file

@ -9,9 +9,7 @@
#if __GNUC__
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#define COMPILER_GCC 1