21 template <
typename T,
typename =
void>
22 struct has_push_back : std::false_type
27 struct has_push_back<T, std::void_t<decltype(std::declval<T>().push_back(std::declval<typename T::value_type>()))>> : std::true_type
32 static constexpr auto eq = [](
const auto &el,
const auto &sep) ->
bool
34 using std::is_convertible_v;
37 using ElType =
decltype(el);
38 using SepType =
decltype(sep);
40 if constexpr (is_same_v<ElType, SepType>)
44 else if constexpr (is_convertible_v<ElType, SepType>)
46 return static_cast<SepType
>(el) == sep;
48 else if constexpr (is_convertible_v<SepType, ElType>)
50 return el ==
static_cast<ElType
>(sep);
63 static std::string
concat(
const std::string &a,
const std::string &b)
noexcept;
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)
87 using SliceContainer =
typename Oc::value_type;
91 SliceContainer dest_elm{};
95 while (slice != end_it)
101 if constexpr (is_same_v<SliceContainer, string>)
105 else if constexpr (has_push_back<SliceContainer>::value)
107 dest_elm.push_back(*slice);
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");
118 dest.push_back(dest_elm);
139 template <
typename It,
typename Oc,
typename V>
140 static It
split(It it,
const It end_it, Oc &dest,
const V &sep)
142 return split(it, end_it, dest, sep, eq);
153 template <
typename Cin,
typename Cout,
typename V>
154 static Cout &
strsplit(
const Cin &str, Cout &dest,
const V &sep)
156 split(str.begin(), str.end(), dest, sep, eq);
164 template <
typename T>
167 using std::is_same_v;
168 using std::string_view;
171 constexpr string_view whitespace_chars =
" \t\r\n\v\f";
174 if constexpr (is_same_v<T, char>)
176 return whitespace_chars.find(c) != string_view::npos;
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');
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()));
200 static std::string_view strip_backticks(std::string_view sv)
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()));
208 static std::string_view strip_quotes(std::string_view sv)
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() ==
'\'')))
214 output_str.remove_prefix(1);
215 output_str.remove_suffix(1);
220 static std::string replace_all(
const std::string &s,
const std::string &from,
const std::string &to)
227 std::string result{s};
228 size_t start_pos = 0;
229 while ((start_pos = result.find(from, start_pos)) != std::string::npos)
231 result.replace(start_pos, from.length(), to);
232 start_pos += to.length();
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