refactor: unit_symbol and dimension_symbol refactored for readability and consteval

This commit is contained in:
Mateusz Pusz
2024-07-04 14:16:19 +01:00
parent 79488ef17d
commit 246923d877
2 changed files with 20 additions and 12 deletions

View File

@@ -305,18 +305,22 @@ constexpr Out dimension_symbol_to(Out out, D d, const dimension_symbol_formattin
// TODO Refactor to `dimension_symbol(D, fmt)` when P1045: constexpr Function Parameters is available
MP_UNITS_EXPORT template<dimension_symbol_formatting fmt = dimension_symbol_formatting{}, typename CharT = char,
Dimension D>
#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_symbol_text = []() consteval {
detail::inplace_vector<CharT, 64> text; // TODO can we improve here?
constexpr auto oversized_symbol_text = []() consteval {
detail::inplace_vector<CharT, 128> text; // TODO can we improve here?
dimension_symbol_to<CharT>(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 basic_fixed_string<CharT, text.size()> buffer(std::from_range, text);
return buffer.view();
static constexpr basic_fixed_string<CharT, oversized_symbol_text.size()> storage(std::from_range,
oversized_symbol_text);
return storage.view();
#else
return basic_fixed_string<CharT, text.size()>(std::from_range, text);
#endif

View File

@@ -816,18 +816,22 @@ constexpr Out unit_symbol_to(Out out, U u, const unit_symbol_formatting& fmt = u
// TODO Refactor to `unit_symbol(U, fmt)` when P1045: constexpr Function Parameters is available
MP_UNITS_EXPORT template<unit_symbol_formatting fmt = unit_symbol_formatting{}, typename CharT = char, Unit U>
#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_symbol_text = []() consteval {
detail::inplace_vector<CharT, 64> text; // TODO can we improve here?
constexpr auto oversized_symbol_text = []() consteval {
detail::inplace_vector<CharT, 128> text; // TODO can we improve here?
unit_symbol_to<CharT>(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 basic_fixed_string<CharT, text.size()> buffer(std::from_range, text);
return buffer.view();
static constexpr basic_fixed_string<CharT, oversized_symbol_text.size()> storage(std::from_range,
oversized_symbol_text);
return storage.view();
#else
return basic_fixed_string<CharT, text.size()>(std::from_range, text);
#endif