From cb7cbf4cb3e3a12554a3c9dc1d291daab6bcf50b Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 22 May 2024 12:18:48 +0200 Subject: [PATCH] refactor: `fixed_string` refactoring to match the R2 specs --- src/core/include/mp-units/ext/fixed_string.h | 53 ++++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/core/include/mp-units/ext/fixed_string.h b/src/core/include/mp-units/ext/fixed_string.h index 2b16d51b..05c12f4e 100644 --- a/src/core/include/mp-units/ext/fixed_string.h +++ b/src/core/include/mp-units/ext/fixed_string.h @@ -76,7 +76,7 @@ public: } // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) - consteval explicit(false) basic_fixed_string(const CharT (&txt)[N + 1]) + consteval explicit(false) basic_fixed_string(const CharT (&txt)[N + 1]) noexcept { gsl_Expects(txt[N] == CharT{}); for (std::size_t i = 0; i < N; ++i) data_[i] = txt[i]; @@ -139,13 +139,13 @@ public: gsl_Expects(!empty()); return (*this)[N - 1]; } - [[nodiscard]] constexpr const_pointer data() const noexcept { return static_cast(data_); } // modifiers constexpr void swap(basic_fixed_string& s) noexcept { std::swap_ranges(begin(), end(), s.begin()); } // 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 { return std::basic_string_view(cbegin(), cend()); @@ -160,47 +160,56 @@ public: [[nodiscard]] constexpr friend basic_fixed_string operator+( const basic_fixed_string& lhs, const basic_fixed_string& rhs) noexcept { - CharT txt[N + N2] = {}; - for (size_t i = 0; i != N; ++i) txt[i] = lhs[i]; - for (size_t i = 0; i != N2; ++i) txt[N + i] = rhs[i]; - return basic_fixed_string(std::begin(txt), std::end(txt)); + 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); } [[nodiscard]] constexpr friend basic_fixed_string operator+(const basic_fixed_string& lhs, CharT rhs) noexcept { - CharT txt[N + 1] = {}; - for (size_t i = 0; i != N; ++i) txt[i] = lhs[i]; - txt[N] = rhs; - return basic_fixed_string(std::begin(txt), std::end(txt)); + CharT txt[N + 1]; + CharT* it = txt; + for (CharT c : lhs) *it++ = c; + *it++ = rhs; + return basic_fixed_string(txt, it); } [[nodiscard]] constexpr friend basic_fixed_string operator+( const CharT lhs, const basic_fixed_string& rhs) noexcept { - CharT txt[1 + N] = {lhs}; - for (size_t i = 0; i != N; ++i) txt[1 + i] = rhs[i]; - return basic_fixed_string(std::begin(txt), std::end(txt)); + CharT txt[1 + N]; + CharT* it = txt; + *it++ = lhs; + for (CharT c : rhs) *it++ = c; + return basic_fixed_string(txt, it); } template [[nodiscard]] consteval friend basic_fixed_string operator+( const basic_fixed_string& lhs, const CharT (&rhs)[N2]) noexcept { - CharT txt[N + N2 - 1] = {}; - for (size_t i = 0; i != N; ++i) txt[i] = lhs[i]; - for (size_t i = 0; i != N2 - 1; ++i) txt[N + i] = rhs[i]; - return basic_fixed_string(std::begin(txt), std::end(txt)); + gsl_Expects(rhs[N2 - 1] == CharT{}); + CharT txt[N + N2]; + CharT* it = txt; + for (CharT c : lhs) *it++ = c; + for (CharT c : rhs) *it++ = c; + return txt; } template [[nodiscard]] consteval friend basic_fixed_string operator+( const CharT (&lhs)[N1], const basic_fixed_string& rhs) noexcept { - CharT txt[N1 + N - 1] = {}; - for (size_t i = 0; i != N1 - 1; ++i) txt[i] = lhs[i]; - for (size_t i = 0; i != N; ++i) txt[N1 - 1 + i] = rhs[i]; - return basic_fixed_string(std::begin(txt), std::end(txt)); + gsl_Expects(lhs[N1 - 1] == CharT{}); + CharT txt[N1 + N]; + CharT* it = txt; + for (size_t i = 0; i != N1 - 1; ++i) *it++ = lhs[i]; + for (CharT c : rhs) *it++ = c; + *it++ = CharT(); + return txt; } // non-member comparison functions @@ -213,6 +222,7 @@ public: template [[nodiscard]] friend consteval bool operator==(const basic_fixed_string& lhs, const CharT (&rhs)[N2]) { + gsl_Expects(rhs[N2 - 1] == CharT{}); return lhs.view() == std::basic_string_view(std::cbegin(rhs), std::cend(rhs) - 1); } @@ -225,6 +235,7 @@ public: template [[nodiscard]] friend consteval auto operator<=>(const basic_fixed_string& lhs, const CharT (&rhs)[N2]) { + gsl_Expects(rhs[N2 - 1] == CharT{}); return lhs.view() <=> std::basic_string_view(std::cbegin(rhs), std::cend(rhs) - 1); }