Maze Builder Docs 8.10.23
Loading...
Searching...
No Matches
string_utils.h
Go to the documentation of this file.
1#ifndef STRING_UTILS_H
2#define STRING_UTILS_H
3
4#include <algorithm>
5#include <string>
6#include <string_view>
7#include <type_traits>
8
11namespace mazes
12{
18 {
19 private:
20 // Helper trait to detect if a type has push_back method - local to this function
21 template <typename T, typename = void>
22 struct has_push_back : std::false_type
23 {
24 };
25
26 template <typename T>
27 struct has_push_back<T, std::void_t<decltype(std::declval<T>().push_back(std::declval<typename T::value_type>()))>> : std::true_type
28 {
29 };
30
32 static constexpr auto eq = [](const auto &el, const auto &sep) -> bool
33 {
34 using std::is_convertible_v;
35 using std::is_same_v;
36
37 using ElType = decltype(el);
38 using SepType = decltype(sep);
39
40 if constexpr (is_same_v<ElType, SepType>)
41 {
42 return el == sep;
43 }
44 else if constexpr (is_convertible_v<ElType, SepType>)
45 {
46 return static_cast<SepType>(el) == sep;
47 }
48 else if constexpr (is_convertible_v<SepType, ElType>)
49 {
50 return el == static_cast<ElType>(sep);
51 }
52 else
53 {
54 return false;
55 }
56 };
57
58 public:
63 static std::string concat(const std::string &a, const std::string &b) noexcept;
64
68 static std::string make_mask_source(const std::string &pattern) noexcept;
69
81 template <typename It, typename Oc, typename V, typename Pred>
82 static It split(It it, const It end_it, Oc &dest, const V &sep, Pred f)
83 {
84 using std::is_same_v;
85 using std::string;
86
87 using SliceContainer = typename Oc::value_type;
88
89 while (it != end_it)
90 {
91 SliceContainer dest_elm{};
92
93 auto slice{it};
94
95 while (slice != end_it)
96 {
97 if (f(*slice, sep))
98 break;
99
100 // Handle string vs other containers
101 if constexpr (is_same_v<SliceContainer, string>)
102 {
103 dest_elm += *slice;
104 }
105 else if constexpr (has_push_back<SliceContainer>::value)
106 {
107 dest_elm.push_back(*slice);
108 }
109 else
110 {
111 // For types without push_back, provide a helpful error
112 static_assert(is_same_v<SliceContainer, string> || has_push_back<SliceContainer>::value,
113 "SliceContainer must be std::string or have a push_back method");
114 }
115 ++slice;
116 }
117
118 dest.push_back(dest_elm);
119
120 if (slice == end_it)
121 {
122 return end_it;
123 }
124
125 it = ++slice;
126 }
127 return it;
128 }
129
139 template <typename It, typename Oc, typename V>
140 static It split(It it, const It end_it, Oc &dest, const V &sep)
141 {
142 return split(it, end_it, dest, sep, eq);
143 }
144
153 template <typename Cin, typename Cout, typename V>
154 static Cout &strsplit(const Cin &str, Cout &dest, const V &sep)
155 {
156 split(str.begin(), str.end(), dest, sep, eq);
157 return dest;
158 }
159
164 template <typename T>
165 static bool is_whitespace(const T &c)
166 {
167 using std::is_same_v;
168 using std::string_view;
169
170 // Use std::string_view for safer character comparison
171 constexpr string_view whitespace_chars = " \t\r\n\v\f";
172
173 // For character types, convert to char for comparison
174 if constexpr (is_same_v<T, char>)
175 {
176 return whitespace_chars.find(c) != string_view::npos;
177 }
178 else
179 {
180 // For other types, do individual comparisons
181 return c == static_cast<T>(' ') ||
182 c == static_cast<T>('\t') ||
183 c == static_cast<T>('\r') ||
184 c == static_cast<T>('\n') ||
185 c == static_cast<T>('\v') ||
186 c == static_cast<T>('\f');
187 }
188 }
189
193 static std::string_view strip_whitespace(std::string_view sv)
194 {
195 std::string_view output_str{sv};
196 output_str.remove_prefix(std::min(output_str.find_first_not_of(" \t\r\n\v\f"), output_str.size()));
197 return output_str;
198 }
199
200 static std::string_view strip_backticks(std::string_view sv)
201 {
202 std::string_view output_str{sv};
203 output_str.remove_prefix(std::min(output_str.find_first_not_of("\\`"), output_str.size()));
204 output_str.remove_suffix(std::min(output_str.size() - output_str.find_last_not_of("\\`") - 1, output_str.size()));
205 return output_str;
206 }
207
208 static std::string_view strip_quotes(std::string_view sv)
209 {
210 std::string_view output_str{sv};
211 if (!output_str.empty() && ((output_str.front() == '"' && output_str.back() == '"') ||
212 (output_str.front() == '\'' && output_str.back() == '\'')))
213 {
214 output_str.remove_prefix(1);
215 output_str.remove_suffix(1);
216 }
217 return output_str;
218 }
219
220 static std::string replace_all(const std::string &s, const std::string &from, const std::string &to)
221 {
222 if (from.empty())
223 {
224 return s;
225 }
226
227 std::string result{s};
228 size_t start_pos = 0;
229 while ((start_pos = result.find(from, start_pos)) != std::string::npos)
230 {
231 result.replace(start_pos, from.length(), to);
232 start_pos += to.length(); // Move past the replaced part
233 }
234 return result;
235 }
236 }; // class
237} // namespace
238
239#endif // STRING_UTILS_H
String helper class.
Definition string_utils.h:18
static std::string make_mask_source(const std::string &pattern) noexcept
Generate a mask string based on the given pattern.
static std::string concat(const std::string &a, const std::string &b) noexcept
Combine and return two strings.
static bool is_whitespace(const T &c)
Checks if a character is a whitespace character.
Definition string_utils.h:165
static Cout & strsplit(const Cin &str, Cout &dest, const V &sep)
High-level string split function using containers.
Definition string_utils.h:154
static std::string_view strip_whitespace(std::string_view sv)
Removes consecutive whitespace characters from a string, leaving only single whitespace between non-w...
Definition string_utils.h:193
static It split(It it, const It end_it, Oc &dest, const V &sep, Pred f)
Splits a range into slices based on a separator and stores the results in a destination container.
Definition string_utils.h:82
static It split(It it, const It end_it, Oc &dest, const V &sep)
Generic split function with default equality predicate.
Definition string_utils.h:140