General Utility Library for C++14  2.11
Classes | Functions
gul14/hexdump.h

Detailed Description

Hexadecimal dump of data.

Classes

class  gul14::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 gul14::hexdump (IteratorT begin, IteratorT end, string_view prompt="")
 Generate a hexdump of a data range and return it as a string. More...
 
template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
std::string gul14::hexdump (const ContainerT &cont, 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. More...
 
template<typename IteratorT , typename = std::enable_if_t<detail::IsHexDumpIterator<IteratorT>::value>>
HexdumpParameterForward< const IteratorT > gul14::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<<. More...
 
template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
HexdumpParameterForward< const decltype(std::declval< ContainerT >).cbegin())> gul14::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. More...
 
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 > gul14::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. More...
 

Function Documentation

◆ hexdump() [1/2]

template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
std::string gul14::hexdump ( const ContainerT &  cont,
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(optional) String that prefixes the dump text
Returns
a string containing the dump

References gul14::hexdump_stream().

◆ hexdump() [2/2]

template<typename IteratorT , typename = std::enable_if_t<detail::IsHexDumpIterator<IteratorT>::value>>
std::string gul14::hexdump ( IteratorT  begin,
IteratorT  end,
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 optional prompt can be added in front of the hexdump.

std::string x = "test\nthe Ä west!\t\r\n";
std::string str = gul14::hexdump(x.begin(), x.end(), "debug -> ");
std::cerr << str;
std::string hexdump(IteratorT begin, IteratorT end, 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 = gul14::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(optional) String that prefixes the dump text
Returns
a string containing the dump

References gul14::hexdump_stream().

◆ hexdump_stream() [1/3]

template<typename ContainerT , typename = std::enable_if_t<detail::IsHexDumpContainer<ContainerT>::value>>
HexdumpParameterForward<const decltype(std::declval<ContainerT>).cbegin())> gul14::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(optional) String that prefixes the dump text
Returns
a helper object to be used with operator<< on streams

◆ hexdump_stream() [2/3]

template<typename IteratorT , typename = std::enable_if_t<detail::IsHexDumpIterator<IteratorT>::value>>
HexdumpParameterForward<const IteratorT> gul14::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 << gul14::hexdump_stream(x.begin(), x.end()) << "\n"; // good
std::cout << gul14::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 optional prompt can be added in front of the hexdump.

std::string x = "test\nthe Ä west!\t\r\n";
std::cerr << gul14::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 << gul14::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(optional) String that prefixes the dump text
Returns
a helper object to be used with operator<< on streams

◆ 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> gul14::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(optional) String that prefixes the dump text
Returns
a helper object to be used with operator<< on streams

Referenced by gul14::hexdump().