forked from mpusz/mp-units
feat: std::format support for compliant compilers added
This commit is contained in:
@@ -51,14 +51,14 @@ over width, fill, and alignment::
|
||||
os << "|" << std::setw(10) << std::setfill('*') << 123 * m << "|"; // |*****123 m|
|
||||
|
||||
|
||||
fmt::format
|
||||
std::format
|
||||
-----------
|
||||
|
||||
.. tip::
|
||||
|
||||
The text formatting support is provided via the ``<units/format.h>`` header file.
|
||||
|
||||
The **mp-units** via ``fmt::format`` provides a fine-grained control over what
|
||||
The **mp-units** via ``std::format`` provides a fine-grained control over what
|
||||
and how is being printed on the text output.
|
||||
|
||||
|
||||
@@ -101,8 +101,8 @@ default formatting is also applied to the output streams. This is why the follow
|
||||
code lines produce the same output::
|
||||
|
||||
std::cout << "Distance: " << 123 * km << "\n";
|
||||
fmt::print("Distance: {}\n", 123 * km);
|
||||
fmt::print("Distance: {:%Q %q}\n", 123 * km);
|
||||
std::cout << std::format("Distance: {}\n", 123 * km);
|
||||
std::cout << std::format("Distance: {:%Q %q}\n", 123 * km);
|
||||
|
||||
|
||||
Quantity Value, Symbol, or Both?
|
||||
@@ -111,9 +111,9 @@ Quantity Value, Symbol, or Both?
|
||||
The user can easily decide to either print a whole quantity (value and symbol) or
|
||||
only its parts. Also a different quantity formatting might be applied::
|
||||
|
||||
fmt::print("{:%Q}", 123 * km); // 123
|
||||
fmt::print("{:%q}", 123 * km); // km
|
||||
fmt::print("{:%Q%q}", 123 * km); // 123km
|
||||
std::cout << std::format("{:%Q}", 123 * km); // 123
|
||||
std::cout << std::format("{:%q}", 123 * km); // km
|
||||
std::cout << std::format("{:%Q%q}", 123 * km); // 123km
|
||||
|
||||
|
||||
Controlling Width, Fill, and Alignment
|
||||
@@ -123,14 +123,14 @@ To control width, fill, and alignment the C++ standard grammar tokens ``fill-and
|
||||
and ``width`` are being used and they treat a quantity value and symbol as a contiguous
|
||||
text::
|
||||
|
||||
fmt::print("|{:0}|", 123 * m); // |123 m|
|
||||
fmt::print("|{:10}|", 123 * m); // | 123 m|
|
||||
fmt::print("|{:<10}|", 123 * m); // |123 m |
|
||||
fmt::print("|{:>10}|", 123 * m); // | 123 m|
|
||||
fmt::print("|{:^10}|", 123 * m); // | 123 m |
|
||||
fmt::print("|{:*<10}|", 123 * m); // |123 m*****|
|
||||
fmt::print("|{:*>10}|", 123 * m); // |*****123 m|
|
||||
fmt::print("|{:*^10}|", 123 * m); // |**123 m***|
|
||||
std::cout << std::format("|{:0}|", 123 * m); // |123 m|
|
||||
std::cout << std::format("|{:10}|", 123 * m); // | 123 m|
|
||||
std::cout << std::format("|{:<10}|", 123 * m); // |123 m |
|
||||
std::cout << std::format("|{:>10}|", 123 * m); // | 123 m|
|
||||
std::cout << std::format("|{:^10}|", 123 * m); // | 123 m |
|
||||
std::cout << std::format("|{:*<10}|", 123 * m); // |123 m*****|
|
||||
std::cout << std::format("|{:*>10}|", 123 * m); // |*****123 m|
|
||||
std::cout << std::format("|{:*^10}|", 123 * m); // |**123 m***|
|
||||
|
||||
|
||||
ASCII-only Quantity Symbols
|
||||
@@ -142,13 +142,13 @@ this by default. From the engineering point of view sometimes Unicode text migh
|
||||
not be a solution as terminals of many (especially embedded) devices are ASCII-only.
|
||||
In such a case the unit symbol can be forced to be printed using ASCII-only characters::
|
||||
|
||||
fmt::print("{}", 10 * R); // 10 Ω
|
||||
fmt::print("{:%Q %Aq}", 10 * R); // 10 ohm
|
||||
fmt::print("{}", 125 * us); // 125 µs
|
||||
fmt::print("{:%Q %Aq}", 125 * us); // 125 us
|
||||
std::cout << std::format("{}", 10 * R); // 10 Ω
|
||||
std::cout << std::format("{:%Q %Aq}", 10 * R); // 10 ohm
|
||||
std::cout << std::format("{}", 125 * us); // 125 µs
|
||||
std::cout << std::format("{:%Q %Aq}", 125 * us); // 125 us
|
||||
inline constexpr auto s2 = s * s;
|
||||
fmt::print("{}", 9.8 * (m / s2)); // 9.8 m/s²
|
||||
fmt::print("{:%Q %Aq}", 9.8 * (m / s2)); // 9.8 m/s^2
|
||||
std::cout << std::format("{}", 9.8 * (m / s2)); // 9.8 m/s²
|
||||
std::cout << std::format("{:%Q %Aq}", 9.8 * (m / s2)); // 9.8 m/s^2
|
||||
|
||||
|
||||
Controlling on How the Quantity Value Is Being Printed
|
||||
@@ -156,8 +156,8 @@ Controlling on How the Quantity Value Is Being Printed
|
||||
|
||||
``sign`` token allows us to specify on how the value's sign is being printed::
|
||||
|
||||
fmt::print("{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}", 1 * m); // 1 m,+1 m,1 m, 1 m
|
||||
fmt::print("{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}", -1 * m); // -1 m,-1 m,-1 m,-1 m
|
||||
std::cout << std::format("{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}", 1 * m); // 1 m,+1 m,1 m, 1 m
|
||||
std::cout << std::format("{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}", -1 * m); // -1 m,-1 m,-1 m,-1 m
|
||||
|
||||
where:
|
||||
|
||||
@@ -169,47 +169,47 @@ where:
|
||||
|
||||
``precision`` token is allowed only for floating-point representation types::
|
||||
|
||||
fmt::print("{:%.0Q %q}", 1.2345 * m); // 1 m
|
||||
fmt::print("{:%.1Q %q}", 1.2345 * m); // 1.2 m
|
||||
fmt::print("{:%.2Q %q}", 1.2345 * m); // 1.23 m
|
||||
std::cout << std::format("{:%.0Q %q}", 1.2345 * m); // 1 m
|
||||
std::cout << std::format("{:%.1Q %q}", 1.2345 * m); // 1.2 m
|
||||
std::cout << std::format("{:%.2Q %q}", 1.2345 * m); // 1.23 m
|
||||
|
||||
|
||||
:token:`units-rep-type` specifies how a value of the representation type is being
|
||||
printed. For integral types::
|
||||
|
||||
fmt::print("{:%bQ %q}", 42 * m); // 101010 m
|
||||
fmt::print("{:%BQ %q}", 42 * m); // 101010 m
|
||||
fmt::print("{:%dQ %q}", 42 * m); // 42 m
|
||||
fmt::print("{:%oQ %q}", 42 * m); // 52 m
|
||||
fmt::print("{:%xQ %q}", 42 * m); // 2a m
|
||||
fmt::print("{:%XQ %q}", 42 * m); // 2A m
|
||||
std::cout << std::format("{:%bQ %q}", 42 * m); // 101010 m
|
||||
std::cout << std::format("{:%BQ %q}", 42 * m); // 101010 m
|
||||
std::cout << std::format("{:%dQ %q}", 42 * m); // 42 m
|
||||
std::cout << std::format("{:%oQ %q}", 42 * m); // 52 m
|
||||
std::cout << std::format("{:%xQ %q}", 42 * m); // 2a m
|
||||
std::cout << std::format("{:%XQ %q}", 42 * m); // 2A m
|
||||
|
||||
The above can be printed in an alternate version thanks to the ``#`` token::
|
||||
|
||||
fmt::print("{:%#bQ %q}", 42 * m); // 0b101010 m
|
||||
fmt::print("{:%#BQ %q}", 42 * m); // 0B101010 m
|
||||
fmt::print("{:%#oQ %q}", 42 * m); // 052 m
|
||||
fmt::print("{:%#xQ %q}", 42 * m); // 0x2a m
|
||||
fmt::print("{:%#XQ %q}", 42 * m); // 0X2A m
|
||||
std::cout << std::format("{:%#bQ %q}", 42 * m); // 0b101010 m
|
||||
std::cout << std::format("{:%#BQ %q}", 42 * m); // 0B101010 m
|
||||
std::cout << std::format("{:%#oQ %q}", 42 * m); // 052 m
|
||||
std::cout << std::format("{:%#xQ %q}", 42 * m); // 0x2a m
|
||||
std::cout << std::format("{:%#XQ %q}", 42 * m); // 0X2A m
|
||||
|
||||
For floating-point values the :token:`units-rep-type` token works as follows::
|
||||
|
||||
fmt::print("{:%aQ %q}", 1.2345678 * m); // 0x9.e065152d8eae841p-3 m
|
||||
fmt::print("{:%.3aQ %q}", 1.2345678 * m); // 0x9.e06p-3 m
|
||||
fmt::print("{:%AQ %q}", 1.2345678 * m); // 0X9.E065152D8EAE841P-3 m
|
||||
fmt::print("{:%.3AQ %q}", 1.2345678 * m); // 0X9.E06P-3 m
|
||||
fmt::print("{:%eQ %q}", 1.2345678 * m); // 1.234568e+00 m
|
||||
fmt::print("{:%.3eQ %q}", 1.2345678 * m); // 1.235e+00 m
|
||||
fmt::print("{:%EQ %q}", 1.2345678 * m); // 1.234568E+00 m
|
||||
fmt::print("{:%.3EQ %q}", 1.2345678 * m); // 1.235E+00 m
|
||||
fmt::print("{:%gQ %q}", 1.2345678 * m); // 1.23457 m
|
||||
fmt::print("{:%gQ %q}", 1.2345678e8 * m); // 1.23457e+08 m
|
||||
fmt::print("{:%.3gQ %q}", 1.2345678 * m); // 1.23 m
|
||||
fmt::print("{:%.3gQ %q}", 1.2345678e8 * m); // 1.23e+08 m
|
||||
fmt::print("{:%GQ %q}", 1.2345678 * m); // 1.23457 m
|
||||
fmt::print("{:%GQ %q}", 1.2345678e8 * m); // 1.23457E+08 m
|
||||
fmt::print("{:%.3GQ %q}", 1.2345678 * m); // 1.23 m
|
||||
fmt::print("{:%.3GQ %q}", 1.2345678e8 * m); // 1.23E+08 m
|
||||
std::cout << std::format("{:%aQ %q}", 1.2345678 * m); // 0x9.e065152d8eae841p-3 m
|
||||
std::cout << std::format("{:%.3aQ %q}", 1.2345678 * m); // 0x9.e06p-3 m
|
||||
std::cout << std::format("{:%AQ %q}", 1.2345678 * m); // 0X9.E065152D8EAE841P-3 m
|
||||
std::cout << std::format("{:%.3AQ %q}", 1.2345678 * m); // 0X9.E06P-3 m
|
||||
std::cout << std::format("{:%eQ %q}", 1.2345678 * m); // 1.234568e+00 m
|
||||
std::cout << std::format("{:%.3eQ %q}", 1.2345678 * m); // 1.235e+00 m
|
||||
std::cout << std::format("{:%EQ %q}", 1.2345678 * m); // 1.234568E+00 m
|
||||
std::cout << std::format("{:%.3EQ %q}", 1.2345678 * m); // 1.235E+00 m
|
||||
std::cout << std::format("{:%gQ %q}", 1.2345678 * m); // 1.23457 m
|
||||
std::cout << std::format("{:%gQ %q}", 1.2345678e8 * m); // 1.23457e+08 m
|
||||
std::cout << std::format("{:%.3gQ %q}", 1.2345678 * m); // 1.23 m
|
||||
std::cout << std::format("{:%.3gQ %q}", 1.2345678e8 * m); // 1.23e+08 m
|
||||
std::cout << std::format("{:%GQ %q}", 1.2345678 * m); // 1.23457 m
|
||||
std::cout << std::format("{:%GQ %q}", 1.2345678e8 * m); // 1.23457E+08 m
|
||||
std::cout << std::format("{:%.3GQ %q}", 1.2345678 * m); // 1.23 m
|
||||
std::cout << std::format("{:%.3GQ %q}", 1.2345678e8 * m); // 1.23E+08 m
|
||||
|
||||
|
||||
Special Signs
|
||||
@@ -218,7 +218,7 @@ Special Signs
|
||||
Beside adding any list of regular characters as a separator between the value and the
|
||||
symbol, it is possible to type a few special signs there too::
|
||||
|
||||
fmt::print("{:%Q_%q}", 123 * km); // 123_km
|
||||
fmt::print("{:%Q%t%q}", 123 * km); // 123\tkm <tab>
|
||||
fmt::print("{:%Q%n%q}", 123 * km); // 123\nkm <new line>
|
||||
fmt::print("{:%Q%% %q}", 123 * km); // 123% km
|
||||
std::cout << std::format("{:%Q_%q}", 123 * km); // 123_km
|
||||
std::cout << std::format("{:%Q%t%q}", 123 * km); // 123\tkm <tab>
|
||||
std::cout << std::format("{:%Q%n%q}", 123 * km); // 123\nkm <new line>
|
||||
std::cout << std::format("{:%Q%% %q}", 123 * km); // 123% km
|
||||
|
||||
Reference in New Issue
Block a user