General Utility Library for C++17 26.5.0
string_util.h
Go to the documentation of this file.
1
26#ifndef GUL17_STRING_UTIL_H_
27#define GUL17_STRING_UTIL_H_
28
29#include <array>
30#include <iterator>
31#include <string_view>
32#include <string>
33
34#include "gul17/internal.h"
35#include "gul17/traits.h"
36
37namespace gul17 {
38
46namespace detail {
47
48// Check if an object has a emplace_back() member function
49template <typename T, typename = int>
50struct HasEmplaceBack : std::false_type { };
51template <typename T>
52struct HasEmplaceBack<T, typename std::enable_if_t<true,
53 decltype(std::declval<T>().emplace_back(), 0)>> : std::true_type { };
54
55// Add an element to (the back of) a container using emplace_back()
56// if availabe or emplace() if not.
57template <typename Container, typename Element>
58auto emplace_back(Container& c, Element e)
59 -> std::enable_if_t<HasEmplaceBack<Container>::value>
60{
61 c.emplace_back(std::move(e));
62}
63
64template <typename Container, typename Element>
65auto emplace_back(Container& c, Element e)
66 -> std::enable_if_t<!HasEmplaceBack<Container>::value>
67{
68 c.emplace(std::move(e));
69}
70
71} // namespace detail
73
90GUL_EXPORT
91extern const std::string_view default_whitespace_characters;
92
94GUL_EXPORT
95extern const std::array<char, 16> hex_digits;
96
114template <typename Integer,
115 std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
116inline std::string hex_string(Integer v)
117{
118 auto u = static_cast<typename std::make_unsigned<Integer>::type>(v);
119 std::string result;
120
121 for (int idx = static_cast<int>(sizeof(Integer)) - 1; idx >= 0; --idx)
122 {
123 auto byte = u >> (8 * idx);
124 unsigned int upper_nibble = (byte >> 4) & 0xf;
125 unsigned int lower_nibble = byte & 0xf;
128 }
129
130 return result;
131}
132
153template <typename Iterator>
154inline std::string
155hex_string(Iterator begin, Iterator end, std::string_view separator = "")
156{
157 const auto range_length = std::distance(begin, end);
158
159 std::string result;
160
161 if (range_length > 0)
162 {
163 const auto n = static_cast<std::size_t>(range_length);
164
165 result.reserve(2 * sizeof(*begin) * n + separator.size() * (n - 1));
166
167 result += hex_string(*begin);
168
169 for (auto it = std::next(begin); it != end; ++it)
170 {
171 result += separator;
172 result += hex_string(*it);
173 }
174 }
175
176 return result;
177}
178
198template <typename Integer, size_t num_elements,
199 std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
200inline std::string
201hex_string(const Integer (&array)[num_elements], std::string_view separator = "")
202{
203 return hex_string(std::begin(array), std::end(array), separator);
204}
205
223template <typename Container,
224 std::enable_if_t<IsContainerLike<Container>::value, bool> = true>
225inline std::string
226hex_string(const Container& container, std::string_view separator = "")
227{
228 return hex_string(std::cbegin(container), std::cend(container), separator);
229}
230
249GUL_EXPORT
250std::string null_safe_string(const char* char_ptr);
251
270GUL_EXPORT
271std::string_view null_safe_string_view(const char* char_ptr);
272
295GUL_EXPORT
296std::string_view null_safe_string_view(const char* char_ptr, std::size_t length);
297
311GUL_EXPORT
312std::string repeat(std::string_view str, std::size_t n);
313
338GUL_EXPORT
339std::string safe_string(const char* char_ptr, std::size_t length);
340
365GUL_EXPORT
366std::string_view safe_string_view(const char* char_ptr, std::size_t length);
367
369
370} // namespace gul17
371
372#endif
373
374// 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:124
GUL_EXPORT const std::array< char, 16 > hex_digits
The 16 digits for hexadecimal numbers ("0123456789abcdef").
Definition string_util.cc:33
GUL_EXPORT const std::string_view default_whitespace_characters
The default characters that are treated as whitespace by GUL.
Definition string_util.cc:32
std::string hex_string(Integer v)
Return the hexadecimal ASCII representation of an integer value.
Definition string_util.h:116
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, respecting null termination in the s...
Definition string_util.cc:77
GUL_EXPORT std::string_view null_safe_string_view(const char *char_ptr)
Safely construct a string_view from a C string or a null pointer.
Definition string_util.cc:46
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, respecting null termination in the s...
Definition string_util.cc:87
GUL_EXPORT std::string repeat(std::string_view str, std::size_t n)
Repeat a string N times.
Definition string_util.cc:66
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:36
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29
Some metaprogramming traits for the General Utility Library.