General Utility Library for C++14  2.8
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 #include "gul14/internal.h"
30 #include "gul14/string_view.h"
31 #include "gul14/traits.h"
32 
33 namespace gul14 {
34 
36 namespace detail {
37 
38 // Check if an object has a emplace_back() member function
39 template <typename T, typename = int>
40 struct HasEmplaceBack : std::false_type { };
41 template <typename T>
42 struct HasEmplaceBack<T, typename std::enable_if_t<true,
43  decltype(std::declval<T>().emplace_back(), 0)>> : std::true_type { };
44 
45 // Add an element to (the back of) a container using emplace_back()
46 // if availabe or emplace() if not.
47 template <typename Container, typename Element>
48 auto emplace_back(Container& c, Element e)
49  -> std::enable_if_t<HasEmplaceBack<Container>::value>
50 {
51  c.emplace_back(std::move(e));
52 }
53 
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(std::move(e));
59 }
60 
61 } // namespace detail
63 
80 GUL_EXPORT
82 
84 GUL_EXPORT
85 extern const std::array<char, 16> hex_digits;
86 
104 template <typename Integer,
105  std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
106 inline std::string hex_string(Integer v)
107 {
108  auto u = static_cast<typename std::make_unsigned<Integer>::type>(v);
109  std::string result;
110 
111  for (int idx = static_cast<int>(sizeof(Integer)) - 1; idx >= 0; --idx)
112  {
113  auto byte = u >> (8 * idx);
114  unsigned int upper_nibble = (byte >> 4) & 0xf;
115  unsigned int lower_nibble = byte & 0xf;
116  result += hex_digits[upper_nibble];
117  result += hex_digits[lower_nibble];
118  }
119 
120  return result;
121 }
122 
143 template <typename Iterator>
144 inline std::string
145 hex_string(Iterator begin, Iterator end, string_view separator = "")
146 {
147  const std::size_t n = std::distance(begin, end);
148 
149  std::string result;
150 
151  if (n > 0)
152  {
153  result.reserve(2 * sizeof(*begin) * n + separator.size() * (n - 1));
154 
155  result += hex_string(*begin);
156 
157  for (auto it = std::next(begin); it != end; ++it)
158  {
159  result += separator;
160  result += hex_string(*it);
161  }
162  }
163 
164  return result;
165 }
166 
186 template <typename Integer, size_t num_elements,
187  std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
188 inline std::string
189 hex_string(const Integer (&array)[num_elements], string_view separator = "")
190 {
191  return hex_string(std::begin(array), std::end(array), separator);
192 }
193 
211 template <typename Container,
212  std::enable_if_t<IsContainerLike<Container>::value, bool> = true>
213 inline std::string
214 hex_string(const Container& container, string_view separator = "")
215 {
216  return hex_string(std::cbegin(container), std::cend(container), separator);
217 }
218 
232 GUL_EXPORT
233 std::string repeat(gul14::string_view str, std::size_t n);
234 
256 GUL_EXPORT
257 std::string safe_string(const char* char_ptr, std::size_t length);
258 
259 } // namespace gul14
260 
261 #endif
262 
263 // vi:ts=4:sw=4:sts=4:et
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.