mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 02:17:16 +02:00
symbol_text refactored
This commit is contained in:
@ -1,21 +1,47 @@
|
|||||||
|
// The MIT License (MIT)
|
||||||
|
//
|
||||||
|
// Copyright (c) 2018 Mateusz Pusz
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <units/bits/external/fixed_string.h>
|
#include <units/bits/external/fixed_string.h>
|
||||||
#include <units/bits/external/hacks.h>
|
#include <units/bits/external/hacks.h>
|
||||||
#include <gsl/gsl_assert>
|
#include <gsl/gsl_assert>
|
||||||
|
|
||||||
|
#if COMP_GCC >= 10
|
||||||
|
#include <compare>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
constexpr void validate_ascii_char([[maybe_unused]] char c) noexcept { Expects((c & 0x80) == 0); }
|
constexpr void validate_ascii_char([[maybe_unused]] char c) noexcept { Expects((c & 0x80) == 0); }
|
||||||
|
|
||||||
template<std::size_t P>
|
template<std::size_t N>
|
||||||
constexpr void validate_ascii_string([[maybe_unused]] const char (&s)[P + 1]) noexcept
|
constexpr void validate_ascii_string([[maybe_unused]] const char (&s)[N + 1]) noexcept
|
||||||
{
|
{
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if constexpr (P != 0)
|
if constexpr (N != 0)
|
||||||
for (size_t i = 0; i < P; ++i)
|
for (size_t i = 0; i < N; ++i)
|
||||||
validate_ascii_char(s[i]);
|
validate_ascii_char(s[i]);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -39,10 +65,10 @@ struct basic_symbol_text {
|
|||||||
basic_fixed_string<StandardCharT, N> standard_;
|
basic_fixed_string<StandardCharT, N> standard_;
|
||||||
basic_fixed_string<char, M> ascii_;
|
basic_fixed_string<char, M> ascii_;
|
||||||
|
|
||||||
constexpr basic_symbol_text(StandardCharT s) noexcept: standard_(s), ascii_(s) { detail::validate_ascii_char(s); }
|
constexpr basic_symbol_text(char s) noexcept: standard_(s), ascii_(s) { detail::validate_ascii_char(s); }
|
||||||
constexpr basic_symbol_text(StandardCharT s, char a) noexcept: standard_(s), ascii_(a) { detail::validate_ascii_char(a); }
|
constexpr basic_symbol_text(StandardCharT s, char a) noexcept: standard_(s), ascii_(a) { detail::validate_ascii_char(a); }
|
||||||
constexpr basic_symbol_text(const StandardCharT (&s)[N + 1]) noexcept: standard_(s), ascii_(s) { detail::validate_ascii_string<N>(s); }
|
constexpr basic_symbol_text(const char (&s)[N + 1]) noexcept: standard_(s), ascii_(s) { detail::validate_ascii_string<N>(s); }
|
||||||
constexpr basic_symbol_text(const basic_fixed_string<StandardCharT, N>& s) noexcept: standard_(s), ascii_(s) { detail::validate_ascii_string<N>(s.data_); }
|
constexpr basic_symbol_text(const basic_fixed_string<char, N>& s) noexcept: standard_(s), ascii_(s) { detail::validate_ascii_string<N>(s.data_); }
|
||||||
constexpr basic_symbol_text(const StandardCharT (&s)[N + 1], const char (&a)[M + 1]) noexcept: standard_(s), ascii_(a) { detail::validate_ascii_string<M>(a); }
|
constexpr basic_symbol_text(const StandardCharT (&s)[N + 1], const char (&a)[M + 1]) noexcept: standard_(s), ascii_(a) { detail::validate_ascii_string<M>(a); }
|
||||||
constexpr basic_symbol_text(const basic_fixed_string<StandardCharT, N>& s, const basic_fixed_string<char, M>& a) noexcept: standard_(s), ascii_(a) { detail::validate_ascii_string<M>(a.data_); }
|
constexpr basic_symbol_text(const basic_fixed_string<StandardCharT, N>& s, const basic_fixed_string<char, M>& a) noexcept: standard_(s), ascii_(a) { detail::validate_ascii_string<M>(a.data_); }
|
||||||
|
|
||||||
@ -56,7 +82,7 @@ struct basic_symbol_text {
|
|||||||
const basic_symbol_text& lhs, const basic_symbol_text<StandardCharT, N2, M2>& rhs) noexcept
|
const basic_symbol_text& lhs, const basic_symbol_text<StandardCharT, N2, M2>& rhs) noexcept
|
||||||
{
|
{
|
||||||
return basic_symbol_text<StandardCharT, N + N2, M + M2>(
|
return basic_symbol_text<StandardCharT, N + N2, M + M2>(
|
||||||
lhs.standard_ + rhs.standard_, lhs.ascii_ + rhs.ascii_);
|
lhs.standard() + rhs.standard(), lhs.ascii() + rhs.ascii());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t N2>
|
template<std::size_t N2>
|
||||||
@ -99,21 +125,21 @@ struct basic_symbol_text {
|
|||||||
return basic_symbol_text<StandardCharT, 1, 1>(lhs) + rhs;
|
return basic_symbol_text<StandardCharT, 1, 1>(lhs) + rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if __GNUC__ >= 10
|
#if COMP_GCC >= 10
|
||||||
|
|
||||||
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
||||||
[[nodiscard]] friend constexpr auto operator<=>(const basic_symbol_text& lhs,
|
[[nodiscard]] friend constexpr auto operator<=>(const basic_symbol_text& lhs,
|
||||||
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
||||||
{
|
{
|
||||||
if (const auto cmp = lhs.standard_ <=> rhs.standard_; cmp != 0) return cmp;
|
if (const auto cmp = lhs.standard() <=> rhs.standard(); cmp != 0) return cmp;
|
||||||
return lhs.ascii_ <=> rhs.ascii_;
|
return lhs.ascii() <=> rhs.ascii();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
||||||
[[nodiscard]] friend constexpr bool operator==(const basic_symbol_text& lhs,
|
[[nodiscard]] friend constexpr bool operator==(const basic_symbol_text& lhs,
|
||||||
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ == rhs.standard_ && lhs.ascii_ == rhs.ascii_;
|
return lhs.standard() == rhs.standard() && lhs.ascii() == rhs.ascii();
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@ -124,7 +150,7 @@ struct basic_symbol_text {
|
|||||||
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
||||||
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ == rhs.standard_;
|
return lhs.standard() == rhs.standard();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
||||||
@ -137,7 +163,7 @@ struct basic_symbol_text {
|
|||||||
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
||||||
const basic_fixed_string<StandardCharT, N>& rhs) noexcept
|
const basic_fixed_string<StandardCharT, N>& rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ == rhs;
|
return lhs.standard() == rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr friend bool operator!=(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator!=(const basic_symbol_text& lhs,
|
||||||
@ -163,7 +189,7 @@ struct basic_symbol_text {
|
|||||||
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
||||||
const StandardCharT (&rhs)[N + 1]) noexcept
|
const StandardCharT (&rhs)[N + 1]) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ == rhs;
|
return lhs.standard() == rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr friend bool operator!=(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator!=(const basic_symbol_text& lhs,
|
||||||
@ -189,7 +215,7 @@ struct basic_symbol_text {
|
|||||||
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator==(const basic_symbol_text& lhs,
|
||||||
StandardCharT rhs) noexcept
|
StandardCharT rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ == rhs;
|
return lhs.standard() == rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr friend bool operator!=(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator!=(const basic_symbol_text& lhs,
|
||||||
@ -216,28 +242,28 @@ struct basic_symbol_text {
|
|||||||
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
||||||
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
const basic_symbol_text<StandardCharT2, N2, M2>& rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ < rhs.standard_;
|
return lhs.standard() < rhs.standard();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StandardCharT2, std::size_t N2>
|
template<typename StandardCharT2, std::size_t N2>
|
||||||
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
||||||
const basic_fixed_string<StandardCharT2, N2>& rhs) noexcept
|
const basic_fixed_string<StandardCharT2, N2>& rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ < rhs;
|
return lhs.standard() < rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StandardCharT2, std::size_t N2>
|
template<typename StandardCharT2, std::size_t N2>
|
||||||
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
||||||
const StandardCharT2 (&rhs)[N2]) noexcept
|
const StandardCharT2 (&rhs)[N2]) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ < basic_fixed_string(rhs);
|
return lhs.standard() < basic_fixed_string(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StandardCharT2>
|
template<typename StandardCharT2>
|
||||||
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
[[nodiscard]] constexpr friend bool operator<(const basic_symbol_text& lhs,
|
||||||
StandardCharT2 rhs) noexcept
|
StandardCharT2 rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.standard_ < basic_fixed_string(rhs);
|
return lhs.standard() < basic_fixed_string(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
template<typename StandardCharT2, std::size_t N2, std::size_t M2>
|
||||||
@ -344,7 +370,6 @@ basic_symbol_text(const StandardCharT (&)[N], const char (&)[M]) -> basic_symbol
|
|||||||
|
|
||||||
template<typename StandardCharT, std::size_t N, std::size_t M>
|
template<typename StandardCharT, std::size_t N, std::size_t M>
|
||||||
basic_symbol_text(const basic_fixed_string<StandardCharT, N>&,
|
basic_symbol_text(const basic_fixed_string<StandardCharT, N>&,
|
||||||
const basic_fixed_string<char, M>&)
|
const basic_fixed_string<char, M>&) -> basic_symbol_text<StandardCharT, N, M>;
|
||||||
-> basic_symbol_text<StandardCharT, N, M>;
|
|
||||||
|
|
||||||
} // namespace units
|
} // namespace units
|
||||||
|
Reference in New Issue
Block a user