feat: added an option to disable space before unit symbol in the text output

Resolves #387
This commit is contained in:
Mateusz Pusz committed 2023-09-04 11:09:40 +02:00
1 parent 75482733d1
commit f46b3dad74
6 files changed
+87 -3

No files matched your search

+1 -1
View File
@@ -398,7 +398,7 @@ private:
// default format should print value followed by the unit separated with 1 space
out = mp_units::detail::format_units_quantity_value<CharT>(out, q.numerical_value(), specs.rep, ctx.locale());
if constexpr (mp_units::detail::has_unit_symbol(get_unit(Reference))) {
*out++ = CharT(' ');
if constexpr (mp_units::space_before_unit_symbol<get_unit(Reference)>) *out++ = CharT(' ');
out = unit_symbol_to<CharT>(out, get_unit(Reference));
}
} else {
+1 -1
View File
@@ -40,7 +40,7 @@ void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
else
os << q.numerical_value();
if constexpr (has_unit_symbol(get_unit(R))) {
os << " ";
if constexpr (space_before_unit_symbol<get_unit(R)>) os << " ";
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), get_unit(R));
}
}
+9
View File
@@ -807,6 +807,15 @@ constexpr Out unit_symbol_impl(Out out, const derived_unit<Expr...>&, unit_symbo
} // namespace detail
/**
* @brief Puts a space ' ' sign before a unit symbol
*
* Quantities of some units (e.g. degree, arcminute, arcsecond) should not be printed with the
* space between a number and a unit. For those a partial specialization with the value `false` should
* be provided.
*/
template<Unit auto U>
inline constexpr bool space_before_unit_symbol = true;
template<typename CharT = char, std::output_iterator<CharT> Out, Unit U>
constexpr Out unit_symbol_to(Out out, U u, unit_symbol_formatting fmt = unit_symbol_formatting{})
@@ -123,4 +123,11 @@ inline constexpr bool unit_can_be_prefixed<non_si::hour> = false;
template<>
inline constexpr bool unit_can_be_prefixed<non_si::day> = false;
template<>
inline constexpr bool space_before_unit_symbol<non_si::degree> = false;
template<>
inline constexpr bool space_before_unit_symbol<non_si::arcminute> = false;
template<>
inline constexpr bool space_before_unit_symbol<non_si::arcsecond> = false;
} // namespace mp_units