Skip to content

Commit

Permalink
Add support for initializer lists
Browse files Browse the repository at this point in the history
  • Loading branch information
yowidin committed Feb 17, 2024
1 parent d6a5ad3 commit ae28d3d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions include/shp/shp.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ struct is_container<std::string> : std::true_type {
using element_type = std::string::value_type;
};

template <typename T>
struct is_container<std::initializer_list<T>> : std::true_type {
using element_type = T;
};

////////////////////////////////////////////////////////////////////////////////
/// Helper functions for constructing a streamable object
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -570,6 +575,25 @@ hex_str(const ContainerT &cont,
return os.str();
}

template <typename ValueT,
typename WithOffsets = PrintOffsets,
typename WithNibbleSeparation = SeparateNibbles,
typename RowWidthValue = RowWidth<16>,
typename WithASCII = PrintASCII,
typename InUpperCase = UpperCase>
inline typename std::enable_if<std::is_standard_layout<ValueT>::value, std::string>::type
hex_str(std::initializer_list<ValueT> cont,
const WithOffsets = WithOffsets{},
const WithNibbleSeparation = WithNibbleSeparation{},
const RowWidthValue = RowWidthValue{},
const WithASCII = WithASCII{},
const InUpperCase = InUpperCase{}) {
std::ostringstream os;
os << iterator_hex_writer<decltype(std::cbegin(cont)), WithOffsets, WithNibbleSeparation, RowWidthValue, WithASCII,
InUpperCase>{std::cbegin(cont), std::cend(cont)};
return os.str();
}

/**
* Convert an single POD-object into a HEX-string.
*
Expand Down
30 changes: 30 additions & 0 deletions test/src/iterator_hex_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ TEST_CASE("Full multiline printing", "[iterator_hex_writer]") {
REQUIRE(result == expected);
}
}

SECTION("initializer_list") {
std::initializer_list<std::uint8_t> value = {0xDE, 0xAD, 0xBE, 0xEF};
const std::string expected{"DEADBEEF"};

SECTION("explicit") {
auto start = std::cbegin(value);
auto end = std::cend(value);
os << shp::iterator_hex_writer<decltype(start), shp::NoOffsets, shp::NoNibbleSeparation, shp::SingleRow,
shp::NoASCII, shp::UpperCase>{start, end};
REQUIRE(os.str() == expected);
}

SECTION("with helper") {
os << shp::hex(value, shp::NoOffsets{}, shp::NoNibbleSeparation{}, shp::SingleRow{}, shp::NoASCII{},
shp::UpperCase{});
REQUIRE(os.str() == expected);
}

SECTION("Directly to string") {
auto result = shp::hex_str(value, shp::NoOffsets{}, shp::NoNibbleSeparation{}, shp::SingleRow{},
shp::NoASCII{}, shp::UpperCase{});
REQUIRE(result == expected);
}
}
}

TEST_CASE("Check ASCII Alignment", "[iterator_hex_writer]") {
Expand All @@ -127,3 +152,8 @@ TEST_CASE("Check non ambiguous", "[iterator_hex_writer]") {
auto result = shp::hex_str(v);
REQUIRE(!result.empty());
}

TEST_CASE("Initializer lists should work", "[iterator_hex_writer]") {
auto result = shp::hex_str({0xDE, 0xAD, 0xBE, 0xEF});
REQUIRE(!result.empty());
}

0 comments on commit ae28d3d

Please sign in to comment.