AKString: add missing comparison operators

And some trivial tests.
This commit is contained in:
Lawrence Manning 2019-07-11 11:58:27 +01:00 committed by Andreas Kling
parent 26956db5ac
commit c3ecf753b2
2 changed files with 31 additions and 0 deletions

22
AK/AKString.h Normal file → Executable file
View file

@ -131,11 +131,17 @@ public:
bool operator==(const String&) const;
bool operator!=(const String& other) const { return !(*this == other); }
bool operator<(const String&) const;
bool operator<(const char*) const;
bool operator>=(const String& other) const { return !(*this < other); }
bool operator>=(const char* other) const { return !(*this < other); }
bool operator>(const String&) const;
bool operator>(const char*) const;
bool operator<=(const String& other) const { return !(*this > other); }
bool operator<=(const char* other) const { return !(*this > other); }
bool operator==(const char* cstring) const
{
if (is_null())
@ -229,6 +235,22 @@ inline bool operator>=(const char* characters, const String& string)
return !(characters < string);
}
inline bool operator>(const char* characters, const String& string)
{
if (!characters)
return !string.is_null();
if (string.is_null())
return false;
return strcmp(characters, string.characters()) > 0;
}
inline bool operator<=(const char* characters, const String& string)
{
return !(characters > string);
}
}
using AK::String;

9
AK/Tests/TestString.cpp Normal file → Executable file
View file

@ -25,6 +25,15 @@ int main()
EXPECT(test_string != "ABCDE");
EXPECT(test_string != "ABCDEFG");
EXPECT("a" < String("b"));
EXPECT(!("a" > String("b")));
EXPECT("b" > String("a"));
EXPECT(!("b" < String("b")));
EXPECT("a" >= String("a"));
EXPECT(!("a" >= String("b")));
EXPECT("a" <= String("a"));
EXPECT(!("b" <= String("a")));
EXPECT_EQ(test_string[0], 'A');
EXPECT_EQ(test_string[1], 'B');