diff --git a/ext/tm/include/tm/string.hpp b/ext/tm/include/tm/string.hpp index 0df4f274d9..16212cb823 100644 --- a/ext/tm/include/tm/string.hpp +++ b/ext/tm/include/tm/string.hpp @@ -1577,6 +1577,20 @@ class String final { * auto str = String::format("{} {}orld {}", cstr, c, num); * assert_str_eq("hello world 999", str); * ``` + * + * Using `{h}`, an integral value or a pointer can be displayed as hex + * ``` + * void *ptr = nullptr; + * int i = 255; + * auto str = String::format("Pointer = {h}, Integer = {h}", ptr, i); + * assert_str_eq("Pointer = 0x0, Integer = 0xff", str); + * ``` + * + * A floating point in hex mode is floored to the integer value. + * ```should_abort + * float f = 10.5; + * String::format("{h}", f); + * ``` */ template static String format(const char *const fmt, Args... args) { @@ -1595,7 +1609,7 @@ class String final { static void format(String &out, const char *fmt, T first, Args... rest) { for (const char *c = fmt; *c != 0; c++) { if (*c == '{' && *(c + 1) == 'h' && *(c + 2) == '}') { - if constexpr (std::is_arithmetic::value || std::is_pointer::value) { + if constexpr (std::is_integral_v || std::is_pointer_v) { out += hex(first, HexFormat::LowercaseAndPrefixed); format(out, c + 3, rest...); return; @@ -1604,7 +1618,7 @@ class String final { abort(); } } else if (*c == '{' && *(c + 1) == '}') { - if constexpr (std::is_pointer::value && !std::is_same::value && !std::is_same::value) { + if constexpr (std::is_pointer_v && !std::is_same_v && !std::is_same_v) { fprintf(stderr, "String::format: T is a general pointer type but you didn't specify {h}\n"); abort(); } else {