mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-21 18:15:58 +00:00
AKString: add missing comparison operators
And some trivial tests.
This commit is contained in:
parent
26956db5ac
commit
c3ecf753b2
22
AK/AKString.h
Normal file → Executable file
22
AK/AKString.h
Normal file → Executable 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
9
AK/Tests/TestString.cpp
Normal file → Executable 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');
|
||||
|
||||
|
|
Loading…
Reference in a new issue