Skip to content

Commit

Permalink
generating right sized constexpr string_view of unit_symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
jbbjarnason committed Oct 13, 2023
1 parent 4bcd5e4 commit 5ac1dd4
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/core/include/mp-units/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,19 +819,17 @@ template<typename CharT = char, Unit U>

template<typename CharT, Unit U, unit_symbol_formatting fmt>
struct const_unit_symbol {
static constexpr std::size_t size{255};
static consteval auto unit_symbol_len() -> std::size_t { return unit_symbol(U{}, fmt).size(); }
static constexpr auto impl() noexcept
{
std::array<CharT, size> array{};
auto out_it{mp_units::unit_symbol_to(array.begin(), U{}, fmt)};
auto len = std::distance(array.begin(), out_it);
return std::make_pair(array, len);
std::array<CharT, unit_symbol_len() + 1> buffer{};
mp_units::unit_symbol_to(std::begin(buffer), U{}, fmt);
return buffer;
}
// Give the joined string static storage
static constexpr auto arr = impl();
static_assert(arr.second < size);
// View as a std::string_view
static constexpr std::basic_string_view<CharT> value{arr.first.data(), arr.second};
static constexpr std::basic_string_view<CharT> value{arr.data(), arr.size() - 1};
};

template<typename CharT = char, Unit U, unit_symbol_formatting fmt = unit_symbol_formatting{}>
Expand Down

0 comments on commit 5ac1dd4

Please sign in to comment.