Skip to content

Commit

Permalink
renamed value_to_hex_string to to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
Cooolrik committed Mar 3, 2024
1 parent 628dc94 commit af08f27
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
8 changes: 4 additions & 4 deletions ctle/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ std::ostream &operator<<( std::ostream &os, const ctle::hash<512> &_hash );
namespace ctle
{

template <> std::string value_to_hex_string<hash<256>>( const hash<256> &value )
template <> std::string to_string<hash<256>>( const hash<256> &value )
{
static_assert( sizeof( value ) == 32, "Error: hash<256> is assumed to be of size 32." );
return bytes_to_hex_string( &value, 32 );
}

template <> std::string value_to_hex_string<hash<512>>( const hash<512> &value )
template <> std::string to_string<hash<512>>( const hash<512> &value )
{
static_assert( sizeof( value ) == 64, "Error: hash<512> is assumed to be of size 64." );
return bytes_to_hex_string( &value, 64 );
Expand Down Expand Up @@ -235,13 +235,13 @@ namespace std

std::ostream &operator<<( std::ostream &os, const ctle::hash<256> &_hash )
{
os << ctle::value_to_hex_string( _hash );
os << ctle::to_string( _hash );
return os;
}

std::ostream &operator<<( std::ostream &os, const ctle::hash<512> &_hash )
{
os << ctle::value_to_hex_string( _hash );
os << ctle::to_string( _hash );
return os;
}

Expand Down
7 changes: 6 additions & 1 deletion ctle/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace ctle
{
const uuid uuid::nil;

template <> std::string value_to_hex_string( const uuid &value )
template <> std::string to_string( const uuid &value )
{
std::string ret;

Expand All @@ -121,6 +121,11 @@ template <> std::string value_to_hex_string( const uuid &value )
return ret;
}

template <> std::string value_to_hex_string( const uuid &value )
{
return to_string( value );
}

template <> uuid ctle::from_string<uuid>( const string_span<char> &str, bool &success ) noexcept
{
if( (str.end - str.start) != 36
Expand Down
14 changes: 7 additions & 7 deletions unit_tests/test_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ TEST( hash, basic_test )
{
using hash = ctle::hash<256>;

const hash hsh0 = hex_string_to_value<hash>( "0000000000000000000000000000000000000000000000000000000000000000" ); // lowest
const hash hsh1 = hex_string_to_value<hash>( "0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff" );
const hash hsh2 = hex_string_to_value<hash>( "00000200ffffffffffffffffffffffffffffffffffffffffffffffffffffffff" );
const hash hsh3 = hex_string_to_value<hash>( "ffffffffffffffffffffffffffffffffffff0000000000000000000000000000" ); // highest
const hash hsh0 = from_string<hash>( "0000000000000000000000000000000000000000000000000000000000000000" ); // lowest
const hash hsh1 = from_string<hash>( "0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff" );
const hash hsh2 = from_string<hash>( "00000200ffffffffffffffffffffffffffffffffffffffffffffffffffffffff" );
const hash hsh3 = from_string<hash>( "ffffffffffffffffffffffffffffffffffff0000000000000000000000000000" ); // highest

std::string hshstr = value_to_hex_string( hsh1 );
auto hshval2 = hex_string_to_value<hash>( hshstr.c_str() );
std::string hshstr = to_string( hsh1 );
auto hshval2 = from_string<hash>( hshstr.c_str() );
EXPECT_TRUE( hsh1 == hshval2 );

std::stringstream str;
str << hsh2;
auto hshval3 = hex_string_to_value<hash>( str.str().c_str() );
auto hshval3 = from_string<hash>( str.str().c_str() );
EXPECT_TRUE( hsh2 == hshval3 );

auto h2 = hsh3;
Expand Down

0 comments on commit af08f27

Please sign in to comment.