27static inline void ltrim(std::string& s)
29 if (!s.empty() && s[0] ==
'\n')
33 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
int ch) {
34 return !std::isspace(ch);
40static inline void rtrim(std::string& s)
42 if (!s.empty() && s[s.length() - 1] ==
'\n')
44 s.erase(s.length() - 1);
46 s.erase(std::find_if(s.rbegin(), s.rend(), [](
int ch) {
47 return !std::isspace(ch);
54static inline void trim(std::string& s)
62static inline void ltrim(std::string_view& sv)
64 sv.remove_prefix(std::min(sv.find_first_not_of(
' '), sv.size()));
69static inline void rtrim(std::string_view& sv)
71 sv.remove_suffix(std::min(sv.size() - sv.find_last_not_of(
' ') - 1, sv.size()));
76static inline void trim(std::string_view& sv)
112static inline std::string_view
ltrim_copy(std::string_view sv)
121static inline std::string_view
rtrim_copy(std::string_view sv)
130static inline std::string_view
trim_copy(std::string_view sv)
149static inline bool replace(std::string& str,
const std::string& from,
const std::string& to,
CaseSensitivity cs = RespectCase)
155 auto it = std::search(str.begin(), str.end(),
156 from.begin(), from.end(),
157 [cs](
char ch1,
char ch2) { return cs == RespectCase
159 : std::toupper(ch1) == std::toupper(ch2); });
165 auto start_pos =
static_cast<size_t>(it - str.begin());
166 str.replace(start_pos, from.length(), to);
177 while (
replace(str, from, to, cs)) {}
196static inline std::vector<std::string>
split(
const std::string& str,
const std::string& delimiter)
198 size_t pos_start = 0;
200 size_t delim_len = delimiter.length();
201 std::vector<std::string> res;
203 while ((pos_end = str.find(delimiter, pos_start)) != std::string::npos)
205 res.push_back(str.substr(pos_start, pos_end - pos_start));
206 pos_start = pos_end + delim_len;
208 res.push_back(str.substr(pos_start));
216static inline std::vector<std::string>
split(
const std::string& str,
char delimiter)
218 return split(str, std::string(1, delimiter));
225static inline std::vector<std::string>
split_wo_empty(
const std::string& str,
const std::string& delimiter)
227 size_t pos_start = 0;
229 size_t delim_len = delimiter.length();
230 std::vector<std::string> res;
232 while ((pos_end = str.find(delimiter, pos_start)) != std::string::npos)
234 if (pos_start != pos_end)
236 res.push_back(str.substr(pos_start, pos_end - pos_start));
238 pos_start = pos_end + delim_len;
239 while (pos_start < str.size() && str.find(delimiter, pos_start) == pos_start)
241 pos_start += delim_len;
244 if (pos_start != str.size())
246 res.push_back(str.substr(pos_start));
255static inline std::vector<std::string>
split_wo_empty(
const std::string& str,
char delimiter)
262concept StdString = std::convertible_to<T, std::string> || std::convertible_to<T, std::wstring>;
271template<StdString String>
272int stoi(
const String& str,
int default_value, std::size_t* pos =
nullptr,
int base = 10) noexcept
276 return std::stoi(str, pos, base);
281 return default_value;
291template<StdString String>
292int64_t
stol(
const String& str, int64_t default_value, std::size_t* pos =
nullptr,
int base = 10) noexcept
296 return std::stol(str, pos, base);
301 return default_value;
311template<StdString String>
312int64_t
stoll(
const String& str, int64_t default_value, std::size_t* pos =
nullptr,
int base = 10) noexcept
316 return std::stoll(str, pos, base);
321 return default_value;
330template<StdString String>
331float stof(
const String& str,
float default_value, std::size_t* pos =
nullptr) noexcept
335 return std::stof(str, pos);
340 return default_value;
349template<StdString String>
350double stod(
const String& str,
double default_value, std::size_t* pos =
nullptr) noexcept
354 return std::stod(str, pos);
359 return default_value;
368template<StdString String>
369long double stold(
const String& str,
long double default_value, std::size_t* pos =
nullptr) noexcept
373 return std::stold(str, pos);
378 return default_value;
int stoi(const String &str, int default_value, std::size_t *pos=nullptr, int base=10) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:272
double stod(const String &str, double default_value, std::size_t *pos=nullptr) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:350
static void replaceAll(std::string &str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
Replaces all occurrence of a search pattern with another sequence.
Definition StringUtil.hpp:175
long double stold(const String &str, long double default_value, std::size_t *pos=nullptr) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:369
CaseSensitivity
Enum for case sensitive tasks.
Definition StringUtil.hpp:138
@ RespectCase
Respect the case.
Definition StringUtil.hpp:139
@ IgnoreCase
Ignore case.
Definition StringUtil.hpp:140
static std::string trim_copy(std::string s)
Trim from both ends (copying)
Definition StringUtil.hpp:103
static std::string ltrim_copy(std::string s)
Trim from start (copying)
Definition StringUtil.hpp:85
static void rtrim(std::string &s)
Trim from end (in place)
Definition StringUtil.hpp:40
static void ltrim(std::string &s)
Trim from start (in place)
Definition StringUtil.hpp:27
static void trim(std::string &s)
Trim from both ends (in place)
Definition StringUtil.hpp:54
int64_t stoll(const String &str, int64_t default_value, std::size_t *pos=nullptr, int base=10) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:312
float stof(const String &str, float default_value, std::size_t *pos=nullptr) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:331
static std::string rtrim_copy(std::string s)
Trim from end (copying)
Definition StringUtil.hpp:94
static std::vector< std::string > split_wo_empty(const std::string &str, const std::string &delimiter)
Splits a string into parts at a delimiter and removes empty entries.
Definition StringUtil.hpp:225
static std::string replaceAll_copy(std::string str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
Replaces all occurrence of a search pattern with another sequence.
Definition StringUtil.hpp:186
static std::vector< std::string > split(const std::string &str, const std::string &delimiter)
Splits a string into parts at a delimiter.
Definition StringUtil.hpp:196
int64_t stol(const String &str, int64_t default_value, std::size_t *pos=nullptr, int base=10) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:292
static bool replace(std::string &str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
Replaces the first occurrence of a search pattern with another sequence.
Definition StringUtil.hpp:149
Concept limiting the type to std::string and std::wstring, but also allowing convertible types like c...
Definition StringUtil.hpp:262