From 2f3a78ecfa0dbf6fd0811e08c1e6ddee94dcded1 Mon Sep 17 00:00:00 2001 From: Cyrus Harrison Date: Sun, 27 Aug 2023 10:28:08 -0700 Subject: [PATCH] cleanup and refactor to_hex helper --- src/libs/conduit/conduit_generator.cpp | 10 ++++------ src/libs/conduit/conduit_node.cpp | 4 ++-- src/libs/conduit/conduit_utils.hpp | 15 ++++++++++++++- src/tests/conduit/t_conduit_utils.cpp | 11 +++++++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/libs/conduit/conduit_generator.cpp b/src/libs/conduit/conduit_generator.cpp index 673757024..29ecd6ac8 100644 --- a/src/libs/conduit/conduit_generator.cpp +++ b/src/libs/conduit/conduit_generator.cpp @@ -1003,20 +1003,18 @@ Generator::Parser::JSON::parse_inline_address(const conduit_rapidjson::Value &jv { void * res = nullptr; if(jvalue.IsString()) - { - char *str_end = nullptr; + { std::string sval(jvalue.GetString()); - unsigned long long ull_addy = strtoull(sval.c_str(),&str_end,0); - res = (void*)ull_addy; + res = utils::hex_string_to_value(sval); } // else if(jvalue.IsNumber()) // { - // // TODO ... + // // TODO: FUTURE? ... // } else { CONDUIT_ERROR("JSON Generator error:\n" - << "inline address should be a string or integer"); + << "inline address should be a string"); } return res; } diff --git a/src/libs/conduit/conduit_node.cpp b/src/libs/conduit/conduit_node.cpp index 65d72f45a..9bf97ba8f 100644 --- a/src/libs/conduit/conduit_node.cpp +++ b/src/libs/conduit/conduit_node.cpp @@ -13583,7 +13583,7 @@ Node::to_json_generic(std::ostream &os, if(address) { - os << "\"address\": \"0x" << utils::to_hex_string(m_data) << "\""; + os << "\"address\": \"" << utils::to_hex_string(m_data) << "\""; } else { @@ -18209,7 +18209,7 @@ Node::info(Node &res, const std::string &curr_path) const if(m_data != NULL) { - std::string ptr_key = std::string("0x") + utils::to_hex_string(m_data); + std::string ptr_key = utils::to_hex_string(m_data); if(!res["mem_spaces"].has_path(ptr_key)) { diff --git a/src/libs/conduit/conduit_utils.hpp b/src/libs/conduit/conduit_utils.hpp index 453030d5a..e372a1856 100644 --- a/src/libs/conduit/conduit_utils.hpp +++ b/src/libs/conduit/conduit_utils.hpp @@ -20,6 +20,7 @@ #include #include #include +#include //----------------------------------------------------------------------------- // -- conduit includes -- @@ -522,7 +523,7 @@ namespace utils std::string to_hex_string(T value) { std::stringstream oss; - oss << std::hex << value; + oss << "0x" << std::hex << value; return oss.str(); } @@ -568,6 +569,18 @@ namespace utils return res; } + // declare then define to avoid icc warnings + template< typename T > + T hex_string_to_value(const std::string &s); + + template< typename T > + T hex_string_to_value(const std::string &s) + { + char *str_end = nullptr; + unsigned long long ull_value = strtoull(s.c_str(),&str_end,0); + return (T)(ull_value); + } + //----------------------------------------------------------------------------- // floating point to string helper, strikes a balance of what we want diff --git a/src/tests/conduit/t_conduit_utils.cpp b/src/tests/conduit/t_conduit_utils.cpp index 38b85d17f..4d07c6249 100644 --- a/src/tests/conduit/t_conduit_utils.cpp +++ b/src/tests/conduit/t_conduit_utils.cpp @@ -897,6 +897,17 @@ TEST(conduit_utils, value_fits) } +//----------------------------------------------------------------------------- +TEST(conduit_utils, to_and_from_hex_string) +{ + int64 val = 1024; + std::string hstring = utils::to_hex_string(val); + std::cout << hstring << std::endl; + EXPECT_EQ(hstring,"0x400"); + int64 val_check = conduit::utils::hex_string_to_value(hstring); + EXPECT_EQ(val,val_check); +} + //----------------------------------------------------------------------------- TEST(conduit_utils, timer) {