General Utility Library for C++17 25.4.1
Classes | Functions
gul17/hexdump.h

Detailed Description

Hexadecimal dump of data.

Collaboration diagram for gul17/hexdump.h:

Classes

class  gul17::HexdumpParameterForward< IteratorT, ContainerT >
 Helper object used to enable a convenient syntax to dump things to a stream. More...
 

Functions

template<typename IteratorT , typename = std::enable_if_t<detail::IsHexDumpIterator<IteratorT>::value>>
std::string gul17::hexdump (IteratorT begin, IteratorT end, std::string_view prompt="")
 Generate a hexdump of a data range and return it as a string.
 
template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
std::string gul17::hexdump (const ContainerT &cont, std::string_view prompt="")
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename IteratorT , typename = std::enable_if_t<detail::IsHexDumpIterator<IteratorT>::value>>
HexdumpParameterForward< const IteratorTgul17::hexdump_stream (const IteratorT &begin, const IteratorT &end, std::string prompt="")
 Generate a hexdump of a data range that can be efficiently written to a stream using operator<<.
 
template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
HexdumpParameterForward< const decltype(std::declval< ContainerT >().cbegin())> gul17::hexdump_stream (const ContainerT &cont, std::string prompt="")
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value, decltype(HexdumpParameterForward<decltype(std::declval<ContainerT>().cbegin()), ContainerT> {}, 0)>>
HexdumpParameterForward< decltype(std::declval< ContainerT >().cbegin()), ContainerTgul17::hexdump_stream (ContainerT &&cont, std::string prompt="")
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 

Function Documentation

◆ hexdump() [1/2]

template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
std::string gul17::hexdump ( const ContainerT cont,
std::string_view  prompt = "" 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
contReference to the container whose contents should be dumped; the container must provide ForwardIterators for .cbegin() and .cend()
prompt(std::optional) String that prefixes the dump text
Returns
a string containing the dump

References gul17::bit_set().

◆ hexdump() [2/2]

template<typename IteratorT , typename = std::enable_if_t<detail::IsHexDumpIterator<IteratorT>::value>>
std::string gul17::hexdump ( IteratorT  begin,
IteratorT  end,
std::string_view  prompt = "" 
)

Generate a hexdump of a data range and return it as a string.

The elements of the data range must be of integral type. They are dumped as unsigned integer values with their native width: Chars as "00" to "ff", 16-bit integers as "0000" to "ffff", and so on. If the elements are of type char, also a textual representation of the printable characters is dumped. An std::optional prompt can be added in front of the hexdump.

std::string x = "test\nthe Ä west!\t\r\n";
std::string str = gul17::hexdump(x.begin(), x.end(), "debug -> ");
std::cerr << str;
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:121
std::string hexdump(IteratorT begin, IteratorT end, std::string_view prompt="")
Generate a hexdump of a data range and return it as a string.
Definition hexdump.h:220
deBug -> 000000: 74 65 73 74 0a 74 68 65 20 c3 84 20 77 65 73 74  test.the .. west
         000010: 21 09 0d 0a                                      !...
std::array<int, 8> ar = {{ 0, 1, 5, 2, -0x300fffff, 2, 5, 1999 }};
std::string str = gul17::hexdump(begin(ar), end(ar));
std::cout << str;
000000: 00000000 00000001 00000005 00000002 cff00001 00000002 00000005 000007cf
Parameters
beginForwardIterator to the first data element to be dumped
endForwardIterator past the last data element to be dumped
prompt(std::optional) String that prefixes the dump text
Returns
a string containing the dump

References gul17::bit_set().

◆ hexdump_stream() [1/3]

template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
HexdumpParameterForward< const decltype(std::declval< ContainerT >().cbegin())> gul17::hexdump_stream ( const ContainerT cont,
std::string  prompt = "" 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
contReference to the container to dump
prompt(std::optional) String that prefixes the dump text
Returns
a helper object to be used with operator<< on streams

References gul17::bit_set().

◆ hexdump_stream() [2/3]

template<typename IteratorT , typename = std::enable_if_t<detail::IsHexDumpIterator<IteratorT>::value>>
HexdumpParameterForward< const IteratorT > gul17::hexdump_stream ( const IteratorT begin,
const IteratorT end,
std::string  prompt = "" 
)

Generate a hexdump of a data range that can be efficiently written to a stream using operator<<.

Where hexdump() writes all of its output into one monolithic string, hexdump_stream() returns a tiny helper object that can efficiently send its output to an output stream via operator<<. This means that the following two lines produce the exact same output, but the stream version uses less resources:

std::cout << gul17::hexdump_stream(x.begin(), x.end()) << "\n"; // good
std::cout << gul17::hexdump(x.begin(), x.end()) << "\n"; // also good, but allocates a temporary string
HexdumpParameterForward< const IteratorT > hexdump_stream(const IteratorT &begin, const IteratorT &end, std::string prompt="")
Generate a hexdump of a data range that can be efficiently written to a stream using operator<<.
Definition hexdump.h:423

The elements of the data range must be of integral type. They are dumped as unsigned integer values with their native width: Chars as "00" to "ff", 16-bit integers as "0000" to "ffff", and so on. If the elements are of type char, also a textual representation of the printable characters is dumped. An std::optional prompt can be added in front of the hexdump.

std::string x = "test\nthe Ä west!\t\r\n";
std::cerr << gul17::hexdump_stream(x.begin(), x.end(), "debug -> ");
debug -> 000000: 74 65 73 74 0a 74 68 65 20 c3 84 20 77 65 73 74  test.the .. west
         000010: 21 09 0d 0a                                      !...
std::array<int, 8> ar = {{ 0, 1, 5, 2, -0x300fffff, 2, 5, 1999 }};
std::cout << gul17::hexdump_stream(begin(ar), end(ar));
000000: 00000000 00000001 00000005 00000002 cff00001 00000002 00000005 000007cf
Parameters
beginForwardIterator to the first data to be dumped
endForwardIterator past the last data element to be dumped
prompt(std::optional) String that prefixes the dump text
Returns
a helper object to be used with operator<< on streams

References gul17::bit_set().

◆ hexdump_stream() [3/3]

template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value, decltype(HexdumpParameterForward<decltype(std::declval<ContainerT>().cbegin()), ContainerT> {}, 0)>>
HexdumpParameterForward< decltype(std::declval< ContainerT >().cbegin()), ContainerT > gul17::hexdump_stream ( ContainerT &&  cont,
std::string  prompt = "" 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
contReference to the container to dump if is a temporary
prompt(std::optional) String that prefixes the dump text
Returns
a helper object to be used with operator<< on streams

References gul17::bit_set().