mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 20:54:28 +02:00
Initial support for printing multiple negative exponents added
This commit is contained in:
@@ -28,11 +28,11 @@
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
template<bool Divide, std::size_t Idx>
|
||||
template<bool Divide, std::size_t NegativeExpCount, std::size_t Idx>
|
||||
constexpr auto operator_text()
|
||||
{
|
||||
if constexpr(Idx == 0) {
|
||||
if constexpr(Divide) {
|
||||
if constexpr(Divide && NegativeExpCount == 1) {
|
||||
return basic_fixed_string("1/");
|
||||
}
|
||||
else {
|
||||
@@ -40,7 +40,7 @@ constexpr auto operator_text()
|
||||
}
|
||||
}
|
||||
else {
|
||||
if constexpr(Divide) {
|
||||
if constexpr(Divide && NegativeExpCount == 1) {
|
||||
return basic_fixed_string("/");
|
||||
}
|
||||
else {
|
||||
@@ -49,28 +49,40 @@ constexpr auto operator_text()
|
||||
}
|
||||
}
|
||||
|
||||
template<typename E, basic_fixed_string Symbol, std::size_t Idx>
|
||||
template<typename E, basic_fixed_string Symbol, std::size_t NegativeExpCount, std::size_t Idx>
|
||||
constexpr auto exp_text()
|
||||
{
|
||||
// get calculation operator + symbol
|
||||
const auto txt = operator_text<E::num < 0, Idx>() + Symbol;
|
||||
const auto txt = operator_text<E::num < 0, NegativeExpCount, Idx>() + Symbol;
|
||||
if constexpr(E::den != 1) {
|
||||
// add root part
|
||||
return txt + basic_fixed_string("^(") + regular<abs(E::num)>() + basic_fixed_string("/") + regular<E::den>() + basic_fixed_string(")");
|
||||
}
|
||||
else if constexpr(abs(E::num) != 1) {
|
||||
else if constexpr(E::num != 1) {
|
||||
// add exponent part
|
||||
return txt + superscript<abs(E::num)>();
|
||||
if constexpr(NegativeExpCount > 1) { // no '/' sign here (only negative exponents)
|
||||
return txt + superscript<E::num>();
|
||||
}
|
||||
else if constexpr(E::num != -1) { // -1 is replaced with '/' sign here
|
||||
return txt + superscript<abs(E::num)>();
|
||||
}
|
||||
else {
|
||||
return txt;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return txt;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Es>
|
||||
inline constexpr int negative_exp_count = ((Es::num < 0 ? 1 : 0) + ...);
|
||||
|
||||
template<typename... Us, typename... Es, std::size_t... Idxs>
|
||||
constexpr auto deduced_symbol_text(exp_list<Es...>, std::index_sequence<Idxs...>)
|
||||
{
|
||||
return (exp_text<Es, Us::symbol, Idxs>() + ...);
|
||||
constexpr auto neg_exp = negative_exp_count<Es...>;
|
||||
return (exp_text<Es, Us::symbol, neg_exp, Idxs>() + ...);
|
||||
}
|
||||
|
||||
template<DerivedDimension Dim, Unit... Us>
|
||||
|
12
src/include/units/bits/external/text_tools.h
vendored
12
src/include/units/bits/external/text_tools.h
vendored
@@ -41,21 +41,25 @@ template<> inline constexpr basic_fixed_string superscript_number<7> = "\u2077";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<8> = "\u2078";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<9> = "\u2079";
|
||||
|
||||
inline constexpr basic_fixed_string superscript_minus = "\u207b";
|
||||
|
||||
template<int Value>
|
||||
requires (Value >= 0)
|
||||
constexpr auto superscript()
|
||||
{
|
||||
if constexpr(Value < 10)
|
||||
if constexpr(Value < 0)
|
||||
return superscript_minus + superscript<-Value>();
|
||||
else if constexpr(Value < 10)
|
||||
return superscript_number<Value>;
|
||||
else
|
||||
return superscript<Value / 10>() + superscript<Value % 10>();
|
||||
}
|
||||
|
||||
template<int Value>
|
||||
requires (Value >= 0)
|
||||
constexpr auto regular()
|
||||
{
|
||||
if constexpr(Value < 10)
|
||||
if constexpr(Value < 0)
|
||||
return basic_fixed_string("-") + superscript<-Value>();
|
||||
else if constexpr(Value < 10)
|
||||
return basic_fixed_string(static_cast<char>('0' + Value));
|
||||
else
|
||||
return regular<Value / 10>() + regular<Value % 10>();
|
||||
|
@@ -74,7 +74,8 @@ constexpr auto prefix_or_ratio_text()
|
||||
template<typename... Es, std::size_t... Idxs>
|
||||
constexpr auto derived_dimension_unit_text(exp_list<Es...>, std::index_sequence<Idxs...>)
|
||||
{
|
||||
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, Idxs>() + ...);
|
||||
constexpr auto neg_exp = negative_exp_count<Es...>;
|
||||
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, neg_exp, Idxs>() + ...);
|
||||
}
|
||||
|
||||
template<typename... Es>
|
||||
|
Reference in New Issue
Block a user