General Utility Library for C++14  2.12
Functions | Variables
gul14/string_util.h

Detailed Description

Various string utility functions.

Functions

template<typename Integer , std::enable_if_t< std::is_integral< Integer >::value, bool > = true>
std::string gul14::hex_string (Integer v)
 Return the hexadecimal ASCII representation of an integer value. More...
 
template<typename Iterator >
std::string gul14::hex_string (Iterator begin, Iterator end, string_view separator="")
 Return the hexadecimal ASCII representation of a range of integer values. More...
 
template<typename Integer , size_t num_elements, std::enable_if_t< std::is_integral< Integer >::value, bool > = true>
std::string gul14::hex_string (const Integer(&array)[num_elements], string_view separator="")
 Return the hexadecimal ASCII representation of an array with integer values. More...
 
template<typename Container , std::enable_if_t< IsContainerLike< Container >::value, bool > = true>
std::string gul14::hex_string (const Container &container, string_view separator="")
 Return the hexadecimal ASCII representation of a container with integer values. More...
 
GUL_EXPORT std::string gul14::repeat (gul14::string_view str, std::size_t n)
 Repeat a string N times. More...
 
GUL_EXPORT std::string gul14::safe_string (const char *char_ptr, std::size_t length)
 Safely construct a std::string from a char pointer and a length. More...
 

Variables

GUL_EXPORT const string_view gul14::default_whitespace_characters { " \t\r\n\a\b\f\v" }
 The default characters that are treated as whitespace by GUL. More...
 
GUL_EXPORT const std::array< char, 16 > gul14::hex_digits
 The 16 digits for hexadecimal numbers ("0123456789abcdef"). More...
 

Function Documentation

◆ hex_string() [1/4]

template<typename Container , std::enable_if_t< IsContainerLike< Container >::value, bool > = true>
std::string gul14::hex_string ( const Container &  container,
string_view  separator = "" 
)
inline

Return the hexadecimal ASCII representation of a container with integer values.

The letters 'a' to 'f' are used in lowercase, and no separators are inserted between individual values.

std::array<unsigned char, 4> values = { 0, 15, 16, 255 };
assert(hex_string(values) == "000f10ff");
std::string hex_string(Integer v)
Return the hexadecimal ASCII representation of an integer value.
Definition: string_util.h:113
Parameters
containerInput container
separatorA string that is inserted between the elements to separate them visually (empty by default)
Template Parameters
Containermust be a gul14::IsContainerLike class
Since
GUL version 2.6

References gul14::hex_string().

◆ hex_string() [2/4]

template<typename Integer , size_t num_elements, std::enable_if_t< std::is_integral< Integer >::value, bool > = true>
std::string gul14::hex_string ( const Integer(&)  array[num_elements],
string_view  separator = "" 
)
inline

Return the hexadecimal ASCII representation of an array with integer values.

The letters 'a' to 'f' are used in lowercase, and a user-defined separator can be inserted between individual values.

uint16_t values[] = { 256, 255 };
assert(hex_string(values) == "010000ff");
assert(hex_string(values, "-") == "0100-00ff");
Parameters
arrayInput array
separatorA string that is inserted between the elements to separate them visually (empty by default)
Template Parameters
Integermust be an integral type.
num_elementsis the number of array elements.
Since
GUL version 2.6

References gul14::hex_string().

◆ hex_string() [3/4]

template<typename Integer , std::enable_if_t< std::is_integral< Integer >::value, bool > = true>
std::string gul14::hex_string ( Integer  v)
inline

Return the hexadecimal ASCII representation of an integer value.

The letters 'a' to 'f' are used in lowercase, and the number of hex digits is twice the number of bytes in the input integer type.

hex_string(static_cast<unsigned char>(1)) == "01"
hex_string(static_cast<unsigned char>(255)) == "ff";
hex_string(uint32_t{ 256 }) == "00000100";
Parameters
vInput value
Template Parameters
Integermust be an integral type.
Since
GUL version 2.6

References gul14::hex_digits.

Referenced by gul14::hex_string().

◆ hex_string() [4/4]

template<typename Iterator >
std::string gul14::hex_string ( Iterator  begin,
Iterator  end,
string_view  separator = "" 
)
inline

Return the hexadecimal ASCII representation of a range of integer values.

The letters 'a' to 'f' are used in lowercase, and a user-defined separator can be inserted between individual values.

std::array<unsigned char, 4> values = { 0, 15, 16, 255 };
assert(hex_string(values.begin(), values.end()) == "000f10ff");
assert(hex_string(values.begin(), values.end(), "-") == "00-0f-10-ff");
Parameters
beginIterator to the first element of the range
endIterator past the last element of the range
separatorA string that is inserted between the elements to separate them visually (empty by default)
Template Parameters
Iteratormust be a forward iterator.
Since
GUL version 2.6

References gul14::hex_string().

◆ repeat()

std::string gul14::repeat ( gul14::string_view  str,
std::size_t  n 
)

Repeat a string N times.

std::string str = repeat("du", 3); // str == "dududu"
GUL_EXPORT std::string repeat(gul14::string_view str, std::size_t n)
Repeat a string N times.
Definition: string_util.cc:33
Parameters
strString to be repeated
nNumber of repetitions
Returns
the N-fold concatenation of the input string.
Since
GUL version 2.7

◆ safe_string()

std::string gul14::safe_string ( const char *  char_ptr,
std::size_t  length 
)

Safely construct a std::string from a char pointer and a length.

If the pointer is null, an empty string is constructed. If there are no zero bytes in the input range, a string of length length is constructed. Otherwise, the input string is treated as a C string and the first zero byte is treated as the end of the string.

auto a = safe_string(nullptr, 5); // a == ""s
auto b = safe_string("ABC", 2); // b == "AB"s
auto c = safe_string("ABC", 4); // c == "ABC"s, trailing zero byte ends the string
auto d = safe_string("AB\0CD", 5); // d == "AB"s, intermediate zero byte ends the string
GUL_EXPORT std::string safe_string(const char *char_ptr, std::size_t length)
Safely construct a std::string from a char pointer and a length.
Definition: string_util.cc:44
Parameters
char_ptrPointer to a C string, an unterminated string of at least the specified length, or null.
lengthMaximum length of the generated string.
Since
GUL version 2.6

Variable Documentation

◆ default_whitespace_characters

const string_view gul14::default_whitespace_characters { " \t\r\n\a\b\f\v" }

The default characters that are treated as whitespace by GUL.

This is a string view that contains the space and the most common control characters, namely (with their ASCII codes):

  • Bell/alert (7)
  • Backspace (8)
  • Horizontal tabulator (9)
  • Newline/line feed (10)
  • Vertical Tab (11)
  • Form feed (12)
  • Carriage return (13)
  • Space (32)
Note
The null character is not treated as whitespace by default.

◆ hex_digits

const std::array< char, 16 > gul14::hex_digits
Initial value:
{
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'} }

The 16 digits for hexadecimal numbers ("0123456789abcdef").

Referenced by gul14::hex_string().