diff --git a/libc/test/UnitTest/LibcTest.cpp b/libc/test/UnitTest/LibcTest.cpp index ac67b997a9f2f..edf543b33b166 100644 --- a/libc/test/UnitTest/LibcTest.cpp +++ b/libc/test/UnitTest/LibcTest.cpp @@ -18,38 +18,25 @@ namespace testing { namespace internal { -// When the value is UInt128 or __uint128_t, show its hexadecimal digits. -// We cannot just use a UInt128 specialization as that resolves to only -// one type, UInt<128> or __uint128_t. We want both overloads as we want to -// be able to unittest UInt<128> on platforms where UInt128 resolves to -// UInt128. +// When the value is UInt128, __uint128_t or wider, show its hexadecimal digits. template -cpp::enable_if_t && cpp::is_unsigned_v, cpp::string> -describeValueUInt(T Value) { +cpp::enable_if_t && cpp::is_unsigned_v && + (sizeof(T) > sizeof(uint64_t)), + cpp::string> +describeValue(T Value) { static_assert(sizeof(T) % 8 == 0, "Unsupported size of UInt"); - cpp::string S(sizeof(T) * 2, '0'); - - constexpr char HEXADECIMALS[16] = {'0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - const size_t Size = S.size(); - for (size_t I = 0; I < Size; I += 2, Value >>= 8) { - unsigned char Mod = static_cast(Value) & 0xFF; - S[Size - I] = HEXADECIMALS[Mod & 0x0F]; - S[Size - (I + 1)] = HEXADECIMALS[Mod & 0x0F]; - } - - return "0x" + S; + char buf[IntegerToString::hex_bufsize()]; + IntegerToString::hex(Value, buf, false); + return "0x" + cpp::string(buf, sizeof(buf)); } -// When the value is of integral type, just display it as normal. +// When the value is of a standard integral type, just display it as normal. template -cpp::enable_if_t, cpp::string> +cpp::enable_if_t && + sizeof(ValType) <= sizeof(uint64_t), + cpp::string> describeValue(ValType Value) { - if constexpr (sizeof(ValType) <= sizeof(uint64_t)) { - return cpp::to_string(Value); - } else { - return describeValueUInt(Value); - } + return cpp::to_string(Value); } cpp::string describeValue(cpp::string Value) { return Value; }