From a9c91e2e5f30f930376a7ee76075773cafb42068 Mon Sep 17 00:00:00 2001 From: Gilles Vollant Date: Sun, 1 Dec 2024 14:13:09 +0100 Subject: [PATCH 1/3] add length() alias to size() on frozen::string (like std::string_view) --- include/frozen/string.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/frozen/string.h b/include/frozen/string.h index 354ed9c..830e364 100644 --- a/include/frozen/string.h +++ b/include/frozen/string.h @@ -59,6 +59,7 @@ class basic_string { constexpr basic_string(const basic_string &) noexcept = default; constexpr basic_string &operator=(const basic_string &) noexcept = default; + constexpr std::size_t length() const { return size_; } constexpr std::size_t size() const { return size_; } constexpr chr_t operator[](std::size_t i) const { return data_[i]; } From a6d0e10b66f67bac73960cc6267942caf76cf91e Mon Sep 17 00:00:00 2001 From: Gilles Vollant Date: Sun, 1 Dec 2024 14:13:35 +0100 Subject: [PATCH 2/3] add operator to convert to std::string_view on frozen::string --- include/frozen/string.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/frozen/string.h b/include/frozen/string.h index 830e364..f054941 100644 --- a/include/frozen/string.h +++ b/include/frozen/string.h @@ -54,6 +54,17 @@ class basic_string { #ifdef FROZEN_LETITGO_HAS_STRING_VIEW constexpr basic_string(std::basic_string_view data) : data_(data.data()), size_(data.size()) {} + + constexpr operator std::basic_string_view() const { return std::basic_string_view(data_, size_); } + + constexpr bool operator==(std::basic_string_view other) const { + if (size_ != other.size()) + return false; + for (std::size_t i = 0; i < size_; ++i) + if (data_[i] != other.data()[i]) + return false; + return true; + } #endif constexpr basic_string(const basic_string &) noexcept = default; From d08e33c36d1743f3f2523715cda2ffd6a7381b9e Mon Sep 17 00:00:00 2001 From: Gilles Vollant Date: Mon, 9 Dec 2024 13:44:37 +0100 Subject: [PATCH 3/3] use self-build std::string_view optimized comparator to compare frozen::string --- include/frozen/string.h | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/include/frozen/string.h b/include/frozen/string.h index f054941..6d29d7e 100644 --- a/include/frozen/string.h +++ b/include/frozen/string.h @@ -51,29 +51,39 @@ class basic_string { constexpr basic_string(chr_t const *data, std::size_t size) : data_(data), size_(size) {} + constexpr basic_string(const basic_string &) noexcept = default; + constexpr basic_string &operator=(const basic_string &) noexcept = default; + + constexpr std::size_t length() const { return size_; } + constexpr std::size_t size() const { return size_; } + + constexpr chr_t operator[](std::size_t i) const { return data_[i]; } + #ifdef FROZEN_LETITGO_HAS_STRING_VIEW constexpr basic_string(std::basic_string_view data) : data_(data.data()), size_(data.size()) {} - constexpr operator std::basic_string_view() const { return std::basic_string_view(data_, size_); } + constexpr operator std::basic_string_view() const { + return std::basic_string_view(data_, size_); + } constexpr bool operator==(std::basic_string_view other) const { - if (size_ != other.size()) - return false; - for (std::size_t i = 0; i < size_; ++i) - if (data_[i] != other.data()[i]) - return false; - return true; + return (std::basic_string_view(data_, size_) == other); } -#endif - constexpr basic_string(const basic_string &) noexcept = default; - constexpr basic_string &operator=(const basic_string &) noexcept = default; + constexpr bool operator<(const std::basic_string_view &other) const { + return (std::basic_string_view(data_, size_) < other); + } - constexpr std::size_t length() const { return size_; } - constexpr std::size_t size() const { return size_; } + constexpr bool operator==(basic_string other) const { + return (std::basic_string_view(data_, size_) == std::basic_string_view(other.data_, other.size_)); + } - constexpr chr_t operator[](std::size_t i) const { return data_[i]; } + constexpr bool operator<(const basic_string &other) const { + return (std::basic_string_view(data_, size_) < std::basic_string_view(other.data_, other.size_)); + } + +#else constexpr bool operator==(basic_string other) const { if (size_ != other.size_) @@ -98,6 +108,8 @@ class basic_string { return size() < other.size(); } +#endif + friend constexpr bool operator>(const basic_string& lhs, const basic_string& rhs) { return rhs < lhs; }