Use std::format for hex conversions
Deprecate intoToHex handler now that we can do everything using
std::format.
Tested: RSV passes
Redfish protocol validator passes
Change-Id: I71000506573314d6c9326c4677f5fbca1ca02b46
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/http/http_response.hpp b/http/http_response.hpp
index 81c664a..cbffdfc 100644
--- a/http/http_response.hpp
+++ b/http/http_response.hpp
@@ -259,7 +259,7 @@
}
size_t hashval = std::hash<nlohmann::json>{}(jsonValue);
- return "\"" + intToHexString(hashval, 8) + "\"";
+ return std::format("\"{:08X}\"", hashval);
}
void write(std::string&& bodyPart)
diff --git a/redfish-core/include/utils/etag_utils.hpp b/redfish-core/include/utils/etag_utils.hpp
index 8759cc7..1c11c07 100644
--- a/redfish-core/include/utils/etag_utils.hpp
+++ b/redfish-core/include/utils/etag_utils.hpp
@@ -26,7 +26,8 @@
crow::Response& res)
{
size_t hash = json_util::hashJsonWithoutKey(res.jsonValue, "DateTime");
- res.setCurrentOverrideEtag("\"" + intToHexString(hash, 8) + "\"");
+ std::string etag = std::format("\"{:08X}\"", hash);
+ res.setCurrentOverrideEtag(etag);
oldCompleteRequestHandler(res);
}
diff --git a/redfish-core/include/utils/hex_utils.hpp b/redfish-core/include/utils/hex_utils.hpp
index 73f7ad2..2f2c40c 100644
--- a/redfish-core/include/utils/hex_utils.hpp
+++ b/redfish-core/include/utils/hex_utils.hpp
@@ -5,32 +5,18 @@
#include <array>
#include <cstddef>
#include <cstdint>
+#include <format>
+#include <span>
#include <string>
#include <vector>
-static constexpr std::array<char, 16> digitsArray = {
- '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
-
-inline std::string intToHexString(uint64_t value, size_t digits)
+inline std::string bytesToHexString(const std::span<const uint8_t>& bytes)
{
- std::string rc(digits, '0');
- size_t bitIndex = (digits - 1) * 4;
- for (size_t digitIndex = 0; digitIndex < digits; digitIndex++)
+ std::string rc;
+ rc.reserve(bytes.size() * 2);
+ for (uint8_t byte : bytes)
{
- rc[digitIndex] = digitsArray[(value >> bitIndex) & 0x0f];
- bitIndex -= 4;
- }
- return rc;
-}
-
-inline std::string bytesToHexString(const std::vector<uint8_t>& bytes)
-{
- std::string rc(bytes.size() * 2, '0');
- for (size_t i = 0; i < bytes.size(); ++i)
- {
- rc[i * 2] = digitsArray[(bytes[i] & 0xf0) >> 4];
- rc[i * 2 + 1] = digitsArray[bytes[i] & 0x0f];
+ rc += std::format("{:02X}", byte);
}
return rc;
}
diff --git a/redfish-core/lib/memory.hpp b/redfish-core/lib/memory.hpp
index bdd31a0..d411d48 100644
--- a/redfish-core/lib/memory.hpp
+++ b/redfish-core/lib/memory.hpp
@@ -165,7 +165,7 @@
{
return;
}
- asyncResp->res.jsonValue[jsonPtr][key] = "0x" + intToHexString(*value, 4);
+ asyncResp->res.jsonValue[jsonPtr][key] = std::format("{:#04X}", *value);
}
inline void getPersistentMemoryProperties(
diff --git a/redfish-core/lib/processor.hpp b/redfish-core/lib/processor.hpp
index 3479909..882be4b 100644
--- a/redfish-core/lib/processor.hpp
+++ b/redfish-core/lib/processor.hpp
@@ -166,7 +166,8 @@
if (value != nullptr && *value != 2)
{
asyncResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
- "0x" + intToHexString(*value, 4);
+
+ std::format("{:#04X}", *value);
}
}
else if (property.first == "EffectiveModel")
@@ -180,7 +181,7 @@
if (*value != 0)
{
asyncResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
- "0x" + intToHexString(*value, 4);
+ std::format("{:#04X}", *value);
}
}
else if (property.first == "Id")
@@ -190,7 +191,7 @@
{
asyncResp->res
.jsonValue["ProcessorId"]["IdentificationRegisters"] =
- "0x" + intToHexString(*value, 16);
+ std::format("{:#016X}", *value);
}
}
else if (property.first == "Microcode")
@@ -204,7 +205,7 @@
if (*value != 0)
{
asyncResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
- "0x" + intToHexString(*value, 8);
+ std::format("{:#08X}", *value);
}
}
else if (property.first == "Step")
@@ -218,7 +219,7 @@
if (*value != std::numeric_limits<uint16_t>::max())
{
asyncResp->res.jsonValue["ProcessorId"]["Step"] =
- "0x" + intToHexString(*value, 4);
+ std::format("{:#04X}", *value);
}
}
}
diff --git a/test/redfish-core/include/utils/hex_utils_test.cpp b/test/redfish-core/include/utils/hex_utils_test.cpp
index fb8100e..b3722b2 100644
--- a/test/redfish-core/include/utils/hex_utils_test.cpp
+++ b/test/redfish-core/include/utils/hex_utils_test.cpp
@@ -15,31 +15,9 @@
using ::testing::IsEmpty;
-TEST(IntToHexString, ReturnsCorrectHexForUint64)
-{
- EXPECT_EQ(intToHexString(0xFFFFFFFFFFFFFFFFULL, 16), "FFFFFFFFFFFFFFFF");
-
- EXPECT_EQ(intToHexString(0, 4), "0000");
- EXPECT_EQ(intToHexString(0, 8), "00000000");
- EXPECT_EQ(intToHexString(0, 12), "000000000000");
- EXPECT_EQ(intToHexString(0, 16), "0000000000000000");
-
- // uint64_t sized ints
- EXPECT_EQ(intToHexString(0xDEADBEEFBAD4F00DULL, 4), "F00D");
- EXPECT_EQ(intToHexString(0xDEADBEEFBAD4F00DULL, 8), "BAD4F00D");
- EXPECT_EQ(intToHexString(0xDEADBEEFBAD4F00DULL, 12), "BEEFBAD4F00D");
- EXPECT_EQ(intToHexString(0xDEADBEEFBAD4F00DULL, 16), "DEADBEEFBAD4F00D");
-
- // uint16_t sized ints
- EXPECT_EQ(intToHexString(0xBEEF, 1), "F");
- EXPECT_EQ(intToHexString(0xBEEF, 2), "EF");
- EXPECT_EQ(intToHexString(0xBEEF, 3), "EEF");
- EXPECT_EQ(intToHexString(0xBEEF, 4), "BEEF");
-}
-
TEST(BytesToHexString, OnSuccess)
{
- EXPECT_EQ(bytesToHexString({0x1a, 0x2b}), "1A2B");
+ EXPECT_EQ(bytesToHexString({{0x1a, 0x2b}}), "1A2B");
}
TEST(HexCharToNibble, ReturnsCorrectNibbleForEveryHexChar)