23 #ifndef GUL14_STRING_UTIL_H_
24 #define GUL14_STRING_UTIL_H_
39 template <
typename T,
typename =
int>
40 struct HasEmplaceBack : std::false_type { };
42 struct HasEmplaceBack<T, typename std::enable_if_t<true,
43 decltype(std::declval<T>().emplace_back(), 0)>> : std::true_type { };
47 template <
typename Container,
typename Element>
48 auto emplace_back(Container& c, Element e)
49 -> std::enable_if_t<HasEmplaceBack<Container>::value>
51 c.emplace_back(std::move(e));
54 template <
typename Container,
typename Element>
55 auto emplace_back(Container& c, Element e)
56 -> std::enable_if_t<!HasEmplaceBack<Container>::value>
58 c.emplace(std::move(e));
104 template <
typename Integer,
105 std::enable_if_t<std::is_integral<Integer>::value,
bool> =
true>
108 auto u =
static_cast<typename std::make_unsigned<Integer>::type
>(v);
111 for (
int idx =
static_cast<int>(
sizeof(Integer)) - 1; idx >= 0; --idx)
113 auto byte = u >> (8 * idx);
114 unsigned int upper_nibble = (
byte >> 4) & 0xf;
115 unsigned int lower_nibble =
byte & 0xf;
143 template <
typename Iterator>
147 const std::size_t n = std::distance(begin, end);
153 result.reserve(2 *
sizeof(*begin) * n + separator.size() * (n - 1));
157 for (
auto it = std::next(begin); it != end; ++it)
186 template <
typename Integer,
size_t num_elements,
187 std::enable_if_t<std::is_integral<Integer>::value,
bool> =
true>
191 return hex_string(std::begin(array), std::end(array), separator);
211 template <
typename Container,
212 std::enable_if_t<IsContainerLike<Container>::value,
bool> =
true>
216 return hex_string(std::cbegin(container), std::cend(container), separator);
257 std::string
safe_string(
const char* char_ptr, std::size_t length);
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
GUL_EXPORT const string_view default_whitespace_characters
The default characters that are treated as whitespace by GUL.
Definition: string_util.cc:28
basic_string_view< char > string_view
A view to a contiguous sequence of chars.
Definition: string_view.h:623
GUL_EXPORT const std::array< char, 16 > hex_digits
The 16 digits for hexadecimal numbers ("0123456789abcdef").
Definition: string_util.cc:29
GUL_EXPORT std::string repeat(gul14::string_view str, std::size_t n)
Repeat a string N times.
Definition: string_util.cc:32
std::string hex_string(Integer v)
Return the hexadecimal ASCII representation of an integer value.
Definition: string_util.h:106
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:43
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.
Some metaprogramming traits for the General Utility Library.