From 931656254f125e7be485e2f66ed7fbacc6900be0 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Tue, 15 Oct 2024 21:00:50 +0200 Subject: [PATCH] refactor: :boom: `char_traits` removed from `fixed_string` --- src/core/include/mp-units/ext/fixed_string.h | 53 +++++++++----------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/src/core/include/mp-units/ext/fixed_string.h b/src/core/include/mp-units/ext/fixed_string.h index 048cf2c2..05fb4c83 100644 --- a/src/core/include/mp-units/ext/fixed_string.h +++ b/src/core/include/mp-units/ext/fixed_string.h @@ -61,13 +61,12 @@ namespace mp_units { * @tparam CharT Character type to be used by the string * @tparam N The size of the string */ -template> +template class basic_fixed_string { public: CharT data_[N + 1] = {}; // exposition only // types - using traits_type = Traits; using value_type = CharT; using pointer = value_type*; using const_pointer = const value_type*; @@ -161,50 +160,47 @@ public: // string operations [[nodiscard]] constexpr const_pointer c_str() const noexcept { return data(); } [[nodiscard]] constexpr const_pointer data() const noexcept { return static_cast(data_); } - [[nodiscard]] constexpr std::basic_string_view view() const noexcept + [[nodiscard]] constexpr std::basic_string_view view() const noexcept { - return std::basic_string_view(cbegin(), cend()); + return std::basic_string_view(cbegin(), cend()); } // NOLINTNEXTLINE(*-explicit-conversions, google-explicit-constructor) - [[nodiscard]] constexpr explicit(false) operator std::basic_string_view() const noexcept - { - return view(); - } + [[nodiscard]] constexpr explicit(false) operator std::basic_string_view() const noexcept { return view(); } template - [[nodiscard]] constexpr friend basic_fixed_string operator+( - const basic_fixed_string& lhs, const basic_fixed_string& rhs) noexcept + [[nodiscard]] constexpr friend basic_fixed_string operator+( + const basic_fixed_string& lhs, const basic_fixed_string& rhs) noexcept { CharT txt[N + N2]; CharT* it = txt; for (CharT c : lhs) *it++ = c; for (CharT c : rhs) *it++ = c; - return basic_fixed_string(txt, it); + return basic_fixed_string(txt, it); } - [[nodiscard]] constexpr friend basic_fixed_string operator+(const basic_fixed_string& lhs, - CharT rhs) noexcept + [[nodiscard]] constexpr friend basic_fixed_string operator+(const basic_fixed_string& lhs, + CharT rhs) noexcept { CharT txt[N + 1]; CharT* it = txt; for (CharT c : lhs) *it++ = c; *it++ = rhs; - return basic_fixed_string(txt, it); + return basic_fixed_string(txt, it); } - [[nodiscard]] constexpr friend basic_fixed_string operator+( - const CharT lhs, const basic_fixed_string& rhs) noexcept + [[nodiscard]] constexpr friend basic_fixed_string operator+(const CharT lhs, + const basic_fixed_string& rhs) noexcept { CharT txt[1 + N]; CharT* it = txt; *it++ = lhs; for (CharT c : rhs) *it++ = c; - return basic_fixed_string(txt, it); + return basic_fixed_string(txt, it); } template - [[nodiscard]] consteval friend basic_fixed_string operator+( - const basic_fixed_string& lhs, const CharT (&rhs)[N2]) noexcept + [[nodiscard]] consteval friend basic_fixed_string operator+(const basic_fixed_string& lhs, + const CharT (&rhs)[N2]) noexcept { MP_UNITS_EXPECTS(rhs[N2 - 1] == CharT{}); CharT txt[N + N2]; @@ -215,8 +211,8 @@ public: } template - [[nodiscard]] consteval friend basic_fixed_string operator+( - const CharT (&lhs)[N1], const basic_fixed_string& rhs) noexcept + [[nodiscard]] consteval friend basic_fixed_string operator+(const CharT (&lhs)[N1], + const basic_fixed_string& rhs) noexcept { MP_UNITS_EXPECTS(lhs[N1 - 1] == CharT{}); CharT txt[N1 + N]; @@ -230,7 +226,7 @@ public: // non-member comparison functions template [[nodiscard]] friend constexpr bool operator==(const basic_fixed_string& lhs, - const basic_fixed_string& rhs) + const basic_fixed_string& rhs) { return lhs.view() == rhs.view(); } @@ -238,12 +234,12 @@ public: [[nodiscard]] friend consteval bool operator==(const basic_fixed_string& lhs, const CharT (&rhs)[N2]) { MP_UNITS_EXPECTS(rhs[N2 - 1] == CharT{}); - return lhs.view() == std::basic_string_view(std::cbegin(rhs), std::cend(rhs) - 1); + return lhs.view() == std::basic_string_view(std::cbegin(rhs), std::cend(rhs) - 1); } template [[nodiscard]] friend constexpr auto operator<=>(const basic_fixed_string& lhs, - const basic_fixed_string& rhs) + const basic_fixed_string& rhs) { return lhs.view() <=> rhs.view(); } @@ -251,13 +247,12 @@ public: [[nodiscard]] friend consteval auto operator<=>(const basic_fixed_string& lhs, const CharT (&rhs)[N2]) { MP_UNITS_EXPECTS(rhs[N2 - 1] == CharT{}); - return lhs.view() <=> std::basic_string_view(std::cbegin(rhs), std::cend(rhs) - 1); + return lhs.view() <=> std::basic_string_view(std::cbegin(rhs), std::cend(rhs) - 1); } // inserters and extractors #if MP_UNITS_HOSTED - friend std::basic_ostream& operator<<(std::basic_ostream& os, - const basic_fixed_string& str) + friend std::basic_ostream& operator<<(std::basic_ostream& os, const basic_fixed_string& str) { return os << str.c_str(); } @@ -275,8 +270,8 @@ template basic_fixed_string(std::from_range_t, std::array) -> basic_fixed_string; // specialized algorithms -template -constexpr void swap(basic_fixed_string& x, basic_fixed_string& y) noexcept +template +constexpr void swap(basic_fixed_string& x, basic_fixed_string& y) noexcept { x.swap(y); }