diff --git a/include/shp/shp.h b/include/shp/shp.h index 4c7e9c1..27cc305 100644 --- a/include/shp/shp.h +++ b/include/shp/shp.h @@ -415,6 +415,11 @@ struct is_container : std::true_type { using element_type = std::string::value_type; }; +template +struct is_container> : std::true_type { + using element_type = T; +}; + //////////////////////////////////////////////////////////////////////////////// /// Helper functions for constructing a streamable object //////////////////////////////////////////////////////////////////////////////// @@ -570,6 +575,25 @@ hex_str(const ContainerT &cont, return os.str(); } +template , + typename WithASCII = PrintASCII, + typename InUpperCase = UpperCase> +inline typename std::enable_if::value, std::string>::type +hex_str(std::initializer_list cont, + const WithOffsets = WithOffsets{}, + const WithNibbleSeparation = WithNibbleSeparation{}, + const RowWidthValue = RowWidthValue{}, + const WithASCII = WithASCII{}, + const InUpperCase = InUpperCase{}) { + std::ostringstream os; + os << iterator_hex_writer{std::cbegin(cont), std::cend(cont)}; + return os.str(); +} + /** * Convert an single POD-object into a HEX-string. * diff --git a/test/src/iterator_hex_writer.cpp b/test/src/iterator_hex_writer.cpp index c2303da..96575fc 100644 --- a/test/src/iterator_hex_writer.cpp +++ b/test/src/iterator_hex_writer.cpp @@ -104,6 +104,31 @@ TEST_CASE("Full multiline printing", "[iterator_hex_writer]") { REQUIRE(result == expected); } } + + SECTION("initializer_list") { + std::initializer_list 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{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]") { @@ -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()); +}