From c59ab3e11e03488f343ec8c94ae447ee45f1a4a5 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 3 Jul 2024 22:57:50 +0100 Subject: [PATCH] refactor: `dimension_symbol` and `units_symbol` refactored to use `inplace_vector` --- .../include/mp-units/framework/dimension.h | 41 ++++--------------- src/core/include/mp-units/framework/unit.h | 41 ++++--------------- 2 files changed, 18 insertions(+), 64 deletions(-) diff --git a/src/core/include/mp-units/framework/dimension.h b/src/core/include/mp-units/framework/dimension.h index 71d24719..c1279ce4 100644 --- a/src/core/include/mp-units/framework/dimension.h +++ b/src/core/include/mp-units/framework/dimension.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -301,47 +302,23 @@ constexpr Out dimension_symbol_to(Out out, D d, const dimension_symbol_formattin return detail::dimension_symbol_impl(out, d, fmt, false); } -namespace detail { - -template -[[nodiscard]] consteval std::array get_symbol_buffer(D) -{ - std::array buffer{}; - dimension_symbol_to(buffer.begin(), D{}, fmt); - return buffer; -} - -} // namespace detail - - // TODO Refactor to `dimension_symbol(D, fmt)` when P1045: constexpr Function Parameters is available MP_UNITS_EXPORT template -#if defined MP_UNITS_COMP_CLANG && MP_UNITS_COMP_CLANG <= 18 [[nodiscard]] constexpr auto dimension_symbol(D) -#else -[[nodiscard]] consteval auto dimension_symbol(D) -#endif { - auto get_size = []() consteval { -#if MP_UNITS_HOSTED - std::basic_string buffer; - dimension_symbol_to(std::back_inserter(buffer), D{}, fmt); - return buffer.size(); -#else - std::array buffer; // TODO unsafe - auto end = dimension_symbol_to(buffer.begin(), D{}, fmt); - return end - buffer.begin(); -#endif + auto get_symbol_text = []() consteval { + detail::inplace_vector text; // TODO can we improve here? + dimension_symbol_to(std::back_inserter(text), D{}, fmt); + return text; }; + constexpr auto text = get_symbol_text(); #if MP_UNITS_API_STRING_VIEW_RET // Permitting static constexpr variables in constexpr functions - static constexpr std::size_t size = get_size(); - static constexpr auto buffer = detail::get_symbol_buffer(D{}); - return std::string_view(buffer.data(), size); + static constexpr basic_fixed_string buffer(std::from_range, text); + return buffer.view(); #else - constexpr std::size_t size = get_size(); - return basic_fixed_string(std::from_range, detail::get_symbol_buffer(D{})); + return basic_fixed_string(std::from_range, text); #endif } diff --git a/src/core/include/mp-units/framework/unit.h b/src/core/include/mp-units/framework/unit.h index f608e63a..cc285d2a 100644 --- a/src/core/include/mp-units/framework/unit.h +++ b/src/core/include/mp-units/framework/unit.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -813,46 +814,22 @@ constexpr Out unit_symbol_to(Out out, U u, const unit_symbol_formatting& fmt = u return detail::unit_symbol_impl(out, u, fmt, false); } -namespace detail { - -template -[[nodiscard]] consteval std::array get_symbol_buffer(U) -{ - std::array buffer{}; - unit_symbol_to(buffer.begin(), U{}, fmt); - return buffer; -} - -} // namespace detail - - // TODO Refactor to `unit_symbol(U, fmt)` when P1045: constexpr Function Parameters is available MP_UNITS_EXPORT template -#if defined MP_UNITS_COMP_CLANG && MP_UNITS_COMP_CLANG <= 18 [[nodiscard]] constexpr auto unit_symbol(U) -#else -[[nodiscard]] consteval auto unit_symbol(U) -#endif { - auto get_size = []() consteval { -#if MP_UNITS_HOSTED - std::basic_string buffer; - unit_symbol_to(std::back_inserter(buffer), U{}, fmt); - return buffer.size(); -#else - std::array buffer; // TODO unsafe - auto end = unit_symbol_to(buffer.begin(), U{}, fmt); - return end - buffer.begin(); -#endif + auto get_symbol_text = []() consteval { + detail::inplace_vector text; // TODO can we improve here? + unit_symbol_to(std::back_inserter(text), U{}, fmt); + return text; }; + constexpr auto text = get_symbol_text(); #if MP_UNITS_API_STRING_VIEW_RET // Permitting static constexpr variables in constexpr functions - static constexpr std::size_t size = get_size(); - static constexpr auto buffer = detail::get_symbol_buffer(U{}); - return std::string_view(buffer.data(), size); + static constexpr basic_fixed_string buffer(std::from_range, text); + return buffer.view(); #else - constexpr std::size_t size = get_size(); - return basic_fixed_string(std::from_range, detail::get_symbol_buffer(U{})); + return basic_fixed_string(std::from_range, text); #endif }