General Utility Library for C++17 25.4.1
string_util.h
Go to the documentation of this file.
1
23#ifndef GUL17_STRING_UTIL_H_
24#define GUL17_STRING_UTIL_H_
25
26#include <array>
27#include <iterator>
28#include <string_view>
29#include <string>
30
31#include "gul17/internal.h"
32#include "gul17/traits.h"
33
34namespace gul17 {
35
43namespace detail {
44
45// Check if an object has a emplace_back() member function
46template <typename T, typename = int>
47struct HasEmplaceBack : std::false_type { };
48template <typename T>
49struct HasEmplaceBack<T, typename std::enable_if_t<true,
50 decltype(std::declval<T>().emplace_back(), 0)>> : std::true_type { };
51
52// Add an element to (the back of) a container using emplace_back()
53// if availabe or emplace() if not.
54template <typename Container, typename Element>
55auto emplace_back(Container& c, Element e)
56 -> std::enable_if_t<HasEmplaceBack<Container>::value>
57{
58 c.emplace_back(std::move(e));
59}
60
61template <typename Container, typename Element>
62auto emplace_back(Container& c, Element e)
63 -> std::enable_if_t<!HasEmplaceBack<Container>::value>
64{
65 c.emplace(std::move(e));
66}
67
68} // namespace detail
70
87GUL_EXPORT
88extern const std::string_view default_whitespace_characters;
89
91GUL_EXPORT
92extern const std::array<char, 16> hex_digits;
93
111template <typename Integer,
112 std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
113inline std::string hex_string(Integer v)
114{
115 auto u = static_cast<typename std::make_unsigned<Integer>::type>(v);
116 std::string result;
117
118 for (int idx = static_cast<int>(sizeof(Integer)) - 1; idx >= 0; --idx)
119 {
120 auto byte = u >> (8 * idx);
121 unsigned int upper_nibble = (byte >> 4) & 0xf;
122 unsigned int lower_nibble = byte & 0xf;
125 }
126
127 return result;
128}
129
150template <typename Iterator>
151inline std::string
152hex_string(Iterator begin, Iterator end, std::string_view separator = "")
153{
154 const std::size_t n = std::distance(begin, end);
155
156 std::string result;
157
158 if (n > 0)
159 {
160 result.reserve(2 * sizeof(*begin) * n + separator.size() * (n - 1));
161
162 result += hex_string(*begin);
163
164 for (auto it = std::next(begin); it != end; ++it)
165 {
166 result += separator;
167 result += hex_string(*it);
168 }
169 }
170
171 return result;
172}
173
193template <typename Integer, size_t num_elements,
194 std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
195inline std::string
196hex_string(const Integer (&array)[num_elements], std::string_view separator = "")
197{
198 return hex_string(std::begin(array), std::end(array), separator);
199}
200
218template <typename Container,
219 std::enable_if_t<IsContainerLike<Container>::value, bool> = true>
220inline std::string
221hex_string(const Container& container, std::string_view separator = "")
222{
223 return hex_string(std::cbegin(container), std::cend(container), separator);
224}
225
244GUL_EXPORT
245std::string null_safe_string(const char* char_ptr);
246
265GUL_EXPORT
266std::string_view null_safe_string_view(const char* char_ptr);
267
281GUL_EXPORT
282std::string repeat(std::string_view str, std::size_t n);
283
307GUL_EXPORT
308std::string safe_string(const char* char_ptr, std::size_t length);
309
333GUL_EXPORT
334std::string_view safe_string_view(const char* char_ptr, std::size_t length);
335
337
338} // namespace gul17
339
340#endif
341
342// vi:ts=4:sw=4:sts=4:et
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:121
GUL_EXPORT const std::array< char, 16 > hex_digits
The 16 digits for hexadecimal numbers ("0123456789abcdef").
Definition string_util.cc:30
GUL_EXPORT const std::string_view default_whitespace_characters
The default characters that are treated as whitespace by GUL.
Definition string_util.cc:29
std::string hex_string(Integer v)
Return the hexadecimal ASCII representation of an integer value.
Definition string_util.h:113
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:64
GUL_EXPORT std::string_view null_safe_string_view(const char *char_ptr)
Safely construct a string_view from a char pointer.
Definition string_util.cc:43
GUL_EXPORT std::string_view safe_string_view(const char *char_ptr, std::size_t length)
Safely construct a string_view from a char pointer and a length.
Definition string_util.cc:74
GUL_EXPORT std::string repeat(std::string_view str, std::size_t n)
Repeat a string N times.
Definition string_util.cc:53
GUL_EXPORT std::string null_safe_string(const char *char_ptr)
Safely construct a std::string from a C string or a null pointer.
Definition string_util.cc:33
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:26
Some metaprogramming traits for the General Utility Library.