mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-03 12:24:26 +02:00
refactor: fixed_string
refactoring to match the R2 specs
This commit is contained in:
@@ -76,7 +76,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
|
// 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{});
|
gsl_Expects(txt[N] == CharT{});
|
||||||
for (std::size_t i = 0; i < N; ++i) data_[i] = txt[i];
|
for (std::size_t i = 0; i < N; ++i) data_[i] = txt[i];
|
||||||
@@ -139,13 +139,13 @@ public:
|
|||||||
gsl_Expects(!empty());
|
gsl_Expects(!empty());
|
||||||
return (*this)[N - 1];
|
return (*this)[N - 1];
|
||||||
}
|
}
|
||||||
[[nodiscard]] constexpr const_pointer data() const noexcept { return static_cast<const_pointer>(data_); }
|
|
||||||
|
|
||||||
// modifiers
|
// modifiers
|
||||||
constexpr void swap(basic_fixed_string& s) noexcept { std::swap_ranges(begin(), end(), s.begin()); }
|
constexpr void swap(basic_fixed_string& s) noexcept { std::swap_ranges(begin(), end(), s.begin()); }
|
||||||
|
|
||||||
// string operations
|
// string operations
|
||||||
[[nodiscard]] constexpr const_pointer c_str() const noexcept { return data(); }
|
[[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
|
[[nodiscard]] constexpr std::basic_string_view<CharT, Traits> view() const noexcept
|
||||||
{
|
{
|
||||||
return std::basic_string_view<CharT, Traits>(cbegin(), cend());
|
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+(
|
[[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
|
const basic_fixed_string& lhs, const basic_fixed_string<CharT, N2, Traits>& rhs) noexcept
|
||||||
{
|
{
|
||||||
CharT txt[N + N2] = {};
|
CharT txt[N + N2];
|
||||||
for (size_t i = 0; i != N; ++i) txt[i] = lhs[i];
|
CharT* it = txt;
|
||||||
for (size_t i = 0; i != N2; ++i) txt[N + i] = rhs[i];
|
for (CharT c : lhs) *it++ = c;
|
||||||
return basic_fixed_string<CharT, N + N2, Traits>(std::begin(txt), std::end(txt));
|
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,
|
[[nodiscard]] constexpr friend basic_fixed_string<CharT, N + 1, Traits> operator+(const basic_fixed_string& lhs,
|
||||||
CharT rhs) noexcept
|
CharT rhs) noexcept
|
||||||
{
|
{
|
||||||
CharT txt[N + 1] = {};
|
CharT txt[N + 1];
|
||||||
for (size_t i = 0; i != N; ++i) txt[i] = lhs[i];
|
CharT* it = txt;
|
||||||
txt[N] = rhs;
|
for (CharT c : lhs) *it++ = c;
|
||||||
return basic_fixed_string<CharT, N + 1, Traits>(std::begin(txt), std::end(txt));
|
*it++ = rhs;
|
||||||
|
return basic_fixed_string<CharT, N + 1, Traits>(txt, it);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr friend basic_fixed_string<CharT, 1 + N, Traits> operator+(
|
[[nodiscard]] constexpr friend basic_fixed_string<CharT, 1 + N, Traits> operator+(
|
||||||
const CharT lhs, const basic_fixed_string& rhs) noexcept
|
const CharT lhs, const basic_fixed_string& rhs) noexcept
|
||||||
{
|
{
|
||||||
CharT txt[1 + N] = {lhs};
|
CharT txt[1 + N];
|
||||||
for (size_t i = 0; i != N; ++i) txt[1 + i] = rhs[i];
|
CharT* it = txt;
|
||||||
return basic_fixed_string<CharT, 1 + N, Traits>(std::begin(txt), std::end(txt));
|
*it++ = lhs;
|
||||||
|
for (CharT c : rhs) *it++ = c;
|
||||||
|
return basic_fixed_string<CharT, 1 + N, Traits>(txt, it);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t N2>
|
template<std::size_t N2>
|
||||||
[[nodiscard]] consteval friend basic_fixed_string<CharT, N + N2 - 1, Traits> operator+(
|
[[nodiscard]] consteval friend basic_fixed_string<CharT, N + N2 - 1, Traits> operator+(
|
||||||
const basic_fixed_string& lhs, const CharT (&rhs)[N2]) noexcept
|
const basic_fixed_string& lhs, const CharT (&rhs)[N2]) noexcept
|
||||||
{
|
{
|
||||||
CharT txt[N + N2 - 1] = {};
|
gsl_Expects(rhs[N2 - 1] == CharT{});
|
||||||
for (size_t i = 0; i != N; ++i) txt[i] = lhs[i];
|
CharT txt[N + N2];
|
||||||
for (size_t i = 0; i != N2 - 1; ++i) txt[N + i] = rhs[i];
|
CharT* it = txt;
|
||||||
return basic_fixed_string<CharT, N + N2 - 1, Traits>(std::begin(txt), std::end(txt));
|
for (CharT c : lhs) *it++ = c;
|
||||||
|
for (CharT c : rhs) *it++ = c;
|
||||||
|
return txt;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t N1>
|
template<std::size_t N1>
|
||||||
[[nodiscard]] consteval friend basic_fixed_string<CharT, N1 + N - 1, Traits> operator+(
|
[[nodiscard]] consteval friend basic_fixed_string<CharT, N1 + N - 1, Traits> operator+(
|
||||||
const CharT (&lhs)[N1], const basic_fixed_string& rhs) noexcept
|
const CharT (&lhs)[N1], const basic_fixed_string& rhs) noexcept
|
||||||
{
|
{
|
||||||
CharT txt[N1 + N - 1] = {};
|
gsl_Expects(lhs[N1 - 1] == CharT{});
|
||||||
for (size_t i = 0; i != N1 - 1; ++i) txt[i] = lhs[i];
|
CharT txt[N1 + N];
|
||||||
for (size_t i = 0; i != N; ++i) txt[N1 - 1 + i] = rhs[i];
|
CharT* it = txt;
|
||||||
return basic_fixed_string<CharT, N1 + N - 1, Traits>(std::begin(txt), std::end(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
|
// non-member comparison functions
|
||||||
@@ -213,6 +222,7 @@ public:
|
|||||||
template<size_t N2>
|
template<size_t N2>
|
||||||
[[nodiscard]] friend consteval bool operator==(const basic_fixed_string& lhs, const CharT (&rhs)[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);
|
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>
|
template<size_t N2>
|
||||||
[[nodiscard]] friend consteval auto operator<=>(const basic_fixed_string& lhs, const CharT (&rhs)[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);
|
return lhs.view() <=> std::basic_string_view<CharT, Traits>(std::cbegin(rhs), std::cend(rhs) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user