24 static std::string
concat(
const std::string& a,
const std::string& b)
noexcept;
30 static bool contains(
const std::string& str,
const std::string& substr)
noexcept;
41 static bool ends_with(
const std::string& str,
const std::string& suffix)
noexcept;
47 static bool find(std::string_view sv,
char c)
noexcept;
53 static std::string_view
find_first_of(
const std::string_view& s,
const std::string_view& chars)
noexcept;
57 template <
typename T,
typename =
void>
58 struct has_push_back : std::false_type
63 struct has_push_back<T, std::void_t<decltype(std::declval<T>().push_back(std::declval<typename T::value_type>())
69 static constexpr auto eq = []<
typename T0,
typename T1>(
const T0& el,
const T1& sep) ->
bool
71 using std::is_convertible_v;
74 using ElType = std::decay_t<T0>;
75 using SepType = std::decay_t<T1>;
77 if constexpr (is_same_v<ElType, SepType>)
81 else if constexpr (is_convertible_v<ElType, SepType>)
83 return static_cast<SepType
>(el) == sep;
85 else if constexpr (is_convertible_v<SepType, ElType>)
87 return el ==
static_cast<ElType
>(sep);
107 template <
typename It,
typename Oc,
typename V,
typename Pred>
108 static It
split(It it,
const It end_it, Oc& dest,
const V& sep, Pred f)
110 using std::is_same_v;
113 using SliceContainer =
typename Oc::value_type;
117 SliceContainer dest_elm{};
121 while (slice != end_it)
127 if constexpr (is_same_v<SliceContainer, string>)
131 else if constexpr (has_push_back<SliceContainer>::value)
133 dest_elm.push_back(*slice);
138 static_assert(is_same_v<SliceContainer, string> || has_push_back<SliceContainer>::value,
139 "SliceContainer must be std::string or have a push_back method");
144 dest.push_back(dest_elm);
165 template <
typename It,
typename Oc,
typename V>
166 static It
split(It it,
const It end_it, Oc& dest,
const V& sep)
168 return split(it, end_it, dest, sep, eq);
179 template <
typename Cin,
typename Cout,
typename V>
180 static Cout&
strsplit(
const Cin& str, Cout& dest,
const V& sep)
182 split(str.begin(), str.end(), dest, sep, eq);
190 template <
typename T>
193 using std::is_same_v;
194 using std::string_view;
197 constexpr string_view whitespace_chars =
" \t\r\n\v\f";
200 if constexpr (is_same_v<T, char>)
202 return whitespace_chars.find(c) != string_view::npos;
207 return c ==
static_cast<T
>(
' ') ||
208 c ==
static_cast<T
>(
'\t') ||
209 c ==
static_cast<T
>(
'\r') ||
210 c ==
static_cast<T
>(
'\n') ||
211 c ==
static_cast<T
>(
'\v') ||
212 c ==
static_cast<T
>(
'\f');
224 string outputString{s};
226 const auto its = unique(outputString.begin(), outputString.end(),
227 [](
const auto& a,
const auto& b)
229 return is_whitespace(a) && is_whitespace(b);
232 outputString.erase(its, outputString.end());
234 outputString.shrink_to_fit();
244 template <
typename... Args>
245 static std::string
format(std::string_view format_str,
const Args&...
args)
noexcept;
Command-line argument handler with JSON support.
Definition args.h:19
String helper class.
Definition string_utils.h:18
static std::string get_file_extension(const std::string &filename) noexcept
Extract file extension from a filename.
static bool find(std::string_view sv, char c) noexcept
Find a character in a string_view.
static bool contains(const std::string &str, const std::string &substr) noexcept
Check if a string contains a substring.
static std::string_view find_first_of(const std::string_view &s, const std::string_view &chars) noexcept
Find the first occurrence of any character from a set in a string view.
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:191
static std::string format(std::string_view format_str, const Args &... args) noexcept
Simple wrapper for fmt::format using runtime format strings (string_view)
static Cout & strsplit(const Cin &str, Cout &dest, const V &sep)
High-level string split function using containers.
Definition string_utils.h:180
static bool ends_with(const std::string &str, const std::string &suffix) noexcept
Check if a string ends with a specific suffix.
static std::string strip_whitespace(const std::string &s)
Removes consecutive whitespace characters from a string, leaving only single whitespace between non-w...
Definition string_utils.h:219
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:108
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:166