General Utility Library for C++14  2.12
string_util.h
Go to the documentation of this file.
1 
23 #ifndef GUL14_STRING_UTIL_H_
24 #define GUL14_STRING_UTIL_H_
25 
26 #include <array>
27 #include <iterator>
28 #include <string>
29 
30 #include "gul14/internal.h"
31 #include "gul14/string_view.h"
32 #include "gul14/traits.h"
33 
34 namespace gul14 {
35 
43 namespace detail {
44 
45 // Check if an object has a emplace_back() member function
46 template <typename T, typename = int>
47 struct HasEmplaceBack : std::false_type { };
48 template <typename T>
49 struct 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.
54 template <typename Container, typename Element>
55 auto emplace_back(Container& c, Element e)
56  -> std::enable_if_t<HasEmplaceBack<Container>::value>
57 {
58  c.emplace_back(std::move(e));
59 }
60 
61 template <typename Container, typename Element>
62 auto 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 
87 GUL_EXPORT
89 
91 GUL_EXPORT
92 extern const std::array<char, 16> hex_digits;
93 
111 template <typename Integer,
112  std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
113 inline 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;
123  result += hex_digits[upper_nibble];
124  result += hex_digits[lower_nibble];
125  }
126 
127  return result;
128 }
129 
150 template <typename Iterator>
151 inline std::string
152 hex_string(Iterator begin, Iterator end, 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 
193 template <typename Integer, size_t num_elements,
194  std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
195 inline std::string
196 hex_string(const Integer (&array)[num_elements], string_view separator = "")
197 {
198  return hex_string(std::begin(array), std::end(array), separator);
199 }
200 
218 template <typename Container,
219  std::enable_if_t<IsContainerLike<Container>::value, bool> = true>
220 inline std::string
221 hex_string(const Container& container, string_view separator = "")
222 {
223  return hex_string(std::cbegin(container), std::cend(container), separator);
224 }
225 
239 GUL_EXPORT
240 std::string repeat(gul14::string_view str, std::size_t n);
241 
263 GUL_EXPORT
264 std::string safe_string(const char* char_ptr, std::size_t length);
265 
267 
268 } // namespace gul14
269 
270 #endif
271 
272 // vi:ts=4:sw=4:sts=4:et
GUL_EXPORT const string_view default_whitespace_characters
The default characters that are treated as whitespace by GUL.
Definition: string_util.cc:29
GUL_EXPORT const std::array< char, 16 > hex_digits
The 16 digits for hexadecimal numbers ("0123456789abcdef").
Definition: string_util.cc:30
GUL_EXPORT std::string repeat(gul14::string_view str, std::size_t n)
Repeat a string N times.
Definition: string_util.cc:33
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:44
basic_string_view< char > string_view
A view to a contiguous sequence of chars.
Definition: string_view.h:624
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.
Some metaprogramming traits for the General Utility Library.