瀏覽代碼

AK: Add comparison operator

s p a c e s h i p  o p e r a t o r

Comparing UTF-8 can be done by simple byte lexicographic comparison per
definition, so we just piggy-back on StringView's high-performance
comparator.
kleines Filmröllchen 2 年之前
父節點
當前提交
ca80353efe
共有 1 個文件被更改,包括 3 次插入0 次删除
  1. 3 0
      AK/String.h

+ 3 - 0
AK/String.h

@@ -73,6 +73,9 @@ public:
     [[nodiscard]] bool operator==(char const* cstring) const;
     [[nodiscard]] bool operator!=(char const* cstring) const { return !(*this == cstring); }
 
+    // NOTE: UTF-8 is defined in a way that lexicographic ordering of code points is equivalent to lexicographic ordering of bytes.
+    [[nodiscard]] int operator<=>(String const& other) const { return this->bytes_as_string_view().compare(other.bytes_as_string_view()); }
+
     [[nodiscard]] u32 hash() const;
 
     template<Arithmetic T>