From 639b270eed81f7c2627d810057d188e2e8ee67f9 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 22 Jun 2025 18:18:44 +0100 Subject: nihil.ucl: improve construction and comparison --- nihil.ucl/string.ccm | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'nihil.ucl/string.ccm') diff --git a/nihil.ucl/string.ccm b/nihil.ucl/string.ccm index ffad847..64f88dc 100644 --- a/nihil.ucl/string.ccm +++ b/nihil.ucl/string.ccm @@ -36,8 +36,13 @@ export struct string final : object { throw type_mismatch(ucl_type, type()); } + // Create a new empty string. + string() + : string(std::string_view("")) + {} + // Create a new UCL string from a string. - string(std::string_view value) + explicit string(std::string_view value) : object(nihil::ucl::ref, ::ucl_object_fromstring_common( value.data(), value.size(), @@ -75,6 +80,7 @@ export struct string final : object { if (::ucl_object_tolstring_safe(uobj, &dptr, &dlen)) return {dptr, dlen}; + // This should never fail. std::abort(); } }; @@ -95,16 +101,42 @@ export auto operator<=> (string const &a, string const &b) return a.value() <=> b.value(); } -export auto operator== (string const &a, string::value_type b) - -> bool +/* + * For convenience, allow comparison with C++ strings without having to + * construct a temporary UCL object. + */ + +export auto operator==(string const &lhs, std::string_view rhs) -> bool +{ + return lhs.value() == rhs; +} + +export auto operator<=>(string const &lhs, std::string_view rhs) + -> std::strong_ordering +{ + return lhs.value() <=> rhs; +} + +export auto operator==(string const &lhs, std::string const &rhs) -> bool +{ + return lhs == std::string_view(rhs); +} + +export auto operator<=>(string const &lhs, std::string const &rhs) + -> std::strong_ordering +{ + return lhs <=> std::string_view(rhs); +} + +export auto operator==(string const &lhs, char const *rhs) -> bool { - return a.value() == b; + return lhs == std::string_view(rhs); } -export auto operator<=> (string const &a, string::value_type b) +export auto operator<=>(string const &lhs, char const *rhs) -> std::strong_ordering { - return a.value() <=> b; + return lhs <=> std::string_view(rhs); } } // namespace nihil::ucl -- cgit v1.2.3