Skip to content

Commit

Permalink
cleanup and refactor to_hex helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush committed Aug 27, 2023
1 parent af44b38 commit 2f3a78e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/libs/conduit/conduit_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*>(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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/conduit/conduit_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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))
{
Expand Down
15 changes: 14 additions & 1 deletion src/libs/conduit/conduit_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <iomanip>
#include <sstream>
#include <chrono>
#include <cstdlib>

//-----------------------------------------------------------------------------
// -- conduit includes --
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/tests/conduit/t_conduit_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64>(hstring);
EXPECT_EQ(val,val_check);
}

//-----------------------------------------------------------------------------
TEST(conduit_utils, timer)
{
Expand Down

0 comments on commit 2f3a78e

Please sign in to comment.