From dff330373333b7be8a6468708c291c7cdb252d6d Mon Sep 17 00:00:00 2001 From: rexy712 Date: Wed, 20 Jul 2022 13:31:11 -0700 Subject: [PATCH] Add string comparisons --- include/rexy/compat/cpp17/string_base.hpp | 47 +++++++++++++++-------- include/rexy/compat/cpp20/string_base.hpp | 36 +++++++++-------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/include/rexy/compat/cpp17/string_base.hpp b/include/rexy/compat/cpp17/string_base.hpp index ffced5e..8c39dac 100644 --- a/include/rexy/compat/cpp17/string_base.hpp +++ b/include/rexy/compat/cpp17/string_base.hpp @@ -23,6 +23,7 @@ #include //forward #include //{false,true}_type, declval, enable_if, remove_reference, decay +#include //lexicographically_compare #include "../../utility.hpp" //strlen, strncmp #include "../../traits.hpp" @@ -124,45 +125,61 @@ namespace rexy{ //Compare template::value,int> = 0> - constexpr bool operator==(Str1&& left, Str2&& right){ + constexpr bool operator==(const Str1& left, const Str2& right){ if(left.length() != right.length()){ return false; } return !rexy::strncmp(left.c_str(), right.c_str(), left.length()+1); } template::value,int> = 0> - constexpr bool operator==(Str1&& left, typename std::decay_t::const_pointer right){ + constexpr bool operator==(const Str1& left, typename std::decay_t::const_pointer right){ if(right == nullptr){ return false; } - const auto rlen = rexy::strlen(right); - if(rlen != left.length()){ + const rexy::basic_string_view rstr(right); + if(rstr.length() != left.length()){ return false; } - return !rexy::strncmp(left.c_str(), right, rlen+1); + return !rexy::strncmp(left.c_str(), rstr.c_str(), left.length()); } template::value,int> = 0> - constexpr bool operator==(typename std::decay_t::const_pointer left, Str1&& right){ + constexpr bool operator==(typename std::decay_t::const_pointer left, const Str1& right){ if(left == nullptr){ return false; } - const auto llen = rexy::strlen(left); - if(llen != right.length()){ + const rexy::basic_string_view lstr(left); + if(lstr.length() != right.length()){ return false; } - return !rexy::strncmp(left, right.c_str(), llen+1); + return !rexy::strncmp(lstr.c_str(), right.c_str(), right.length()); } template::value,int> = 0> - constexpr bool operator!=(Str1&& left, Str2&& right)noexcept{ - return !(std::forward(left) == std::forward(right)); + constexpr bool operator!=(const Str1& left, const Str2& right)noexcept{ + return !(left == right); } template::value,int> = 0> - constexpr bool operator!=(Str1&& left, typename std::decay_t::const_pointer right)noexcept{ - return !(std::forward(left) == right); + constexpr bool operator!=(const Str1& left, typename std::decay_t::const_pointer right)noexcept{ + return !(left == right); } template::value,int> = 0> - constexpr bool operator!=(typename std::decay_t::const_pointer left, Str1&& right)noexcept{ - return !(left == std::forward(right)); + constexpr bool operator!=(typename std::decay_t::const_pointer left, const Str1& right)noexcept{ + return !(left == right); + } + template::value,int> = 0> + constexpr bool operator<(const Str1& left, const Str2& right)noexcept{ + return std::lexicographical_compare(left.begin(), left.end(), right.begin(), right.end()); + } + template::value,int> = 0> + constexpr bool operator<=(const Str1& left, const Str2& right)noexcept{ + return !(right < left); + } + template::value,int> = 0> + constexpr bool operator>(const Str1& left, const Str2& right)noexcept{ + return (right < left); + } + template::value,int> = 0> + constexpr bool operator>=(const Str1& left, const Str2& right)noexcept{ + return !(left < right); } //String + string concat diff --git a/include/rexy/compat/cpp20/string_base.hpp b/include/rexy/compat/cpp20/string_base.hpp index 9745cf4..196380a 100644 --- a/include/rexy/compat/cpp20/string_base.hpp +++ b/include/rexy/compat/cpp20/string_base.hpp @@ -23,6 +23,7 @@ #include //forward #include //decay, is_nothrow_assignable +#include //lexicographically_compare_three_way #include "../../utility.hpp" //strlen, strncmp @@ -91,45 +92,50 @@ namespace rexy{ //Compare template - constexpr bool operator==(Str1&& left, Str2&& right){ + constexpr bool operator==(const Str1& left, const Str2& right){ if(left.length() != right.length()){ return false; } return !rexy::strncmp(left.c_str(), right.c_str(), left.length()+1); } template - constexpr bool operator==(Str1&& left, typename std::decay_t::const_pointer right){ + constexpr bool operator==(const Str1& left, typename std::decay_t::const_pointer right)noexcept{ if(right == nullptr){ return false; } - const auto rlen = rexy::strlen(right); - if(rlen != left.length()){ + const rexy::basic_string_view rstr(right); + if(rstr.length() != left.length()){ return false; } - return !rexy::strncmp(left.c_str(), right, rlen+1); + return !rexy::strncmp(left.c_str(), rstr.c_str(), left.length()); } template - constexpr bool operator==(typename std::decay_t::const_pointer left, Str1&& right){ + constexpr bool operator==(typename std::decay_t::const_pointer left, const Str1& right)noexcept{ if(left == nullptr){ return false; } - const auto llen = rexy::strlen(left); - if(llen != right.length()){ + const rexy::basic_string_view lstr(left); + if(lstr.length() != right.length()){ return false; } - return !rexy::strncmp(left, right.c_str(), llen+1); + return !rexy::strncmp(lstr.c_str(), right.c_str(), right.length()); } template - constexpr bool operator!=(Str1&& left, Str2&& right){ - return !(std::forward(left) == std::forward(right)); + constexpr bool operator!=(const Str1& left, const Str2& right)noexcept{ + return !(left == right); } template - constexpr bool operator!=(Str1&& left, typename std::decay_t::const_pointer right){ - return !(std::forward(left) == right); + constexpr bool operator!=(const Str1& left, typename std::decay_t::const_pointer right)noexcept{ + return !(left == right); } template - constexpr bool operator!=(typename std::decay_t::const_pointer left, Str1&& right){ - return !(left == std::forward(right)); + constexpr bool operator!=(typename std::decay_t::const_pointer left, const Str1& right)noexcept{ + return !(left == right); + } + + template + constexpr auto operator<=>(const Str1& left, const Str2& right)noexcept{ + return std::lexicographical_compare_three_way(left.begin(), left.end(), right.begin(), right.end()); } //String + string concat