From c3ecf753b2da6127b14f2e4933cb004167ced2f8 Mon Sep 17 00:00:00 2001 From: Lawrence Manning Date: Thu, 11 Jul 2019 11:58:27 +0100 Subject: [PATCH] AKString: add missing comparison operators And some trivial tests. --- AK/AKString.h | 22 ++++++++++++++++++++++ AK/Tests/TestString.cpp | 9 +++++++++ 2 files changed, 31 insertions(+) mode change 100644 => 100755 AK/AKString.h mode change 100644 => 100755 AK/Tests/TestString.cpp diff --git a/AK/AKString.h b/AK/AKString.h old mode 100644 new mode 100755 index a2bafbc6bf..39a78cfa36 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -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; diff --git a/AK/Tests/TestString.cpp b/AK/Tests/TestString.cpp old mode 100644 new mode 100755 index 851971a77e..808d0904be --- a/AK/Tests/TestString.cpp +++ b/AK/Tests/TestString.cpp @@ -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');