From 23524d2cd2fdab4bb143e97542ec59deac599696 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Sun, 24 Sep 2023 09:18:26 +0000 Subject: [PATCH 1/4] fix: awful wrong indentation in stringops --- include/dpp/stringops.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/include/dpp/stringops.h b/include/dpp/stringops.h index 77de2e1718..2445f005d4 100644 --- a/include/dpp/stringops.h +++ b/include/dpp/stringops.h @@ -40,9 +40,9 @@ namespace dpp { */ template std::basic_string lowercase(const std::basic_string& s) { - std::basic_string s2 = s; - std::transform(s2.begin(), s2.end(), s2.begin(), tolower); - return s2; + std::basic_string s2 = s; + std::transform(s2.begin(), s2.end(), s2.begin(), tolower); + return s2; } /** @@ -54,9 +54,9 @@ template std::basic_string lowercase(const std::basic_string& */ template std::basic_string uppercase(const std::basic_string& s) { - std::basic_string s2 = s; - std::transform(s2.begin(), s2.end(), s2.begin(), toupper); - return s2; + std::basic_string s2 = s; + std::transform(s2.begin(), s2.end(), s2.begin(), toupper); + return s2; } /** @@ -189,10 +189,10 @@ template int from_string(const std::string &s) */ template std::string to_hex(T i) { - std::stringstream stream; + std::stringstream stream; stream.imbue(std::locale::classic()); - stream << std::setfill('0') << std::setw(sizeof(T)*2) << std::hex << i; - return stream.str(); + stream << std::setfill('0') << std::setw(sizeof(T)*2) << std::hex << i; + return stream.str(); } /** @@ -205,10 +205,10 @@ template std::string to_hex(T i) */ template std::string leading_zeroes(T i, size_t width) { - std::stringstream stream; + std::stringstream stream; stream.imbue(std::locale::classic()); - stream << std::setfill('0') << std::setw((int)width) << std::dec << i; - return stream.str(); + stream << std::setfill('0') << std::setw((int)width) << std::dec << i; + return stream.str(); } } // namespace dpp From 9b4a7d6b61aee178807b1cb14bb228dbd55605fc Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Sun, 24 Sep 2023 09:27:36 +0000 Subject: [PATCH 2/4] refactor: improved to_hex --- include/dpp/stringops.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/dpp/stringops.h b/include/dpp/stringops.h index 2445f005d4..de7b8ba152 100644 --- a/include/dpp/stringops.h +++ b/include/dpp/stringops.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace dpp { /** @@ -189,10 +190,9 @@ template int from_string(const std::string &s) */ template std::string to_hex(T i) { - std::stringstream stream; - stream.imbue(std::locale::classic()); - stream << std::setfill('0') << std::setw(sizeof(T)*2) << std::hex << i; - return stream.str(); + char str[26] = { 0 }; + std::to_chars(std::begin(str), std::end(str), i, 16); + return std::string{str}; } /** From 8d62cf93e3756bd99a7d6366950adbd0e44514ad Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Sun, 24 Sep 2023 09:38:49 +0000 Subject: [PATCH 3/4] fix: add support for leading zeroes to to_hex --- include/dpp/stringops.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/dpp/stringops.h b/include/dpp/stringops.h index de7b8ba152..527b015d1e 100644 --- a/include/dpp/stringops.h +++ b/include/dpp/stringops.h @@ -191,8 +191,13 @@ template int from_string(const std::string &s) template std::string to_hex(T i) { char str[26] = { 0 }; + size_t size = sizeof(T) * 2; std::to_chars(std::begin(str), std::end(str), i, 16); - return std::string{str}; + std::string out{str}; + if (out.length() < size) { + out.insert(out.begin(), size - out.length(), '0'); + } + return out; } /** From 2fda856215c755cd0fe743f4e2d89d291fd919a9 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Sun, 24 Sep 2023 09:41:59 +0000 Subject: [PATCH 4/4] improve by adding support for optional leading zero --- include/dpp/stringops.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/dpp/stringops.h b/include/dpp/stringops.h index 527b015d1e..279d84c6c9 100644 --- a/include/dpp/stringops.h +++ b/include/dpp/stringops.h @@ -186,15 +186,16 @@ template int from_string(const std::string &s) * * @tparam T numeric type * @param i numeric value + * @param leading_zeroes set to false if you don't want the leading zeroes in the output * @return std::string value in hex, the length will be 2* the raw size of the type */ -template std::string to_hex(T i) +template std::string to_hex(T i, bool leading_zeroes = true) { char str[26] = { 0 }; size_t size = sizeof(T) * 2; std::to_chars(std::begin(str), std::end(str), i, 16); std::string out{str}; - if (out.length() < size) { + if (leading_zeroes && out.length() < size) { out.insert(out.begin(), size - out.length(), '0'); } return out;