refactor: fixed_string refactoring to match the R2 specs

This commit is contained in:
Mateusz Pusz
2024-05-22 12:18:48 +02:00
parent 7322d9a10f
commit cb7cbf4cb3

View File

@@ -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<const_pointer>(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<const_pointer>(data_); }
[[nodiscard]] constexpr std::basic_string_view<CharT, Traits> view() const noexcept
{
return std::basic_string_view<CharT, Traits>(cbegin(), cend());
@@ -160,47 +160,56 @@ public:
[[nodiscard]] constexpr friend basic_fixed_string<CharT, N + N2, Traits> operator+(
const basic_fixed_string& lhs, const basic_fixed_string<CharT, N2, Traits>& 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<CharT, N + N2, Traits>(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<CharT, N + N2, Traits>(txt, it);
}
[[nodiscard]] constexpr friend basic_fixed_string<CharT, N + 1, Traits> 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<CharT, N + 1, Traits>(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<CharT, N + 1, Traits>(txt, it);
}
[[nodiscard]] constexpr friend basic_fixed_string<CharT, 1 + N, Traits> 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<CharT, 1 + N, Traits>(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<CharT, 1 + N, Traits>(txt, it);
}
template<std::size_t N2>
[[nodiscard]] consteval friend basic_fixed_string<CharT, N + N2 - 1, Traits> 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<CharT, N + N2 - 1, Traits>(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<std::size_t N1>
[[nodiscard]] consteval friend basic_fixed_string<CharT, N1 + N - 1, Traits> 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<CharT, N1 + N - 1, Traits>(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<size_t N2>
[[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<CharT, Traits>(std::cbegin(rhs), std::cend(rhs) - 1);
}
@@ -225,6 +235,7 @@ public:
template<size_t N2>
[[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<CharT, Traits>(std::cbegin(rhs), std::cend(rhs) - 1);
}