diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-22 18:18:44 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-22 18:18:44 +0100 |
| commit | 639b270eed81f7c2627d810057d188e2e8ee67f9 (patch) | |
| tree | 7b8f7628b799dba307f06cf813583d42bdc1838a /nihil.ucl/string.ccm | |
| parent | 09b7a494bd5de24f380095003fb7da4939de43e5 (diff) | |
| download | nihil-639b270eed81f7c2627d810057d188e2e8ee67f9.tar.gz nihil-639b270eed81f7c2627d810057d188e2e8ee67f9.tar.bz2 | |
nihil.ucl: improve construction and comparison
Diffstat (limited to 'nihil.ucl/string.ccm')
| -rw-r--r-- | nihil.ucl/string.ccm | 44 |
1 files changed, 38 insertions, 6 deletions
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 |
