18 for (
size_t i = 0; str[i] != 0; ++i)
40 auto keys =
Split(patterns,
';');
41 for (
const std::string& key : keys)
44 std::string pattern = negative ? key.substr(1) : key;
49 std::regex expression(pattern);
50 bool matched = std::regex_match(str, expression);
54 catch (
const std::regex_error&) {}
64 return std::string(std::find_if(str.begin(), str.end(), [](
int c) { return !std::isspace(c); }), str.end());
69 return std::string(str.begin(), std::find_if(str.rbegin(), str.rend(), [](
int c) { return !std::isspace(c); }).base());
74 auto start = std::find_if(str.begin(), str.end(), [](
int c) { return !std::isspace(c); });
75 auto end = std::find_if(str.rbegin(), str.rend(), [](
int c) { return !std::isspace(c); }).base();
77 return (start != str.end()) ? std::string(start, end) : std::string();
82 str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](
int c) { return !std::isspace(c); }));
88 str.erase(std::find_if(str.rbegin(), str.rend(), [](
int c) { return !std::isspace(c); }).base(), str.end());
94 return (str1 == str2);
99 if (str1.size() != str2.size())
101 return std::equal(str1.cbegin(), str1.cend(), str2.cbegin(), [](std::string::value_type l, std::string::value_type r) { return std::tolower(l) == std::tolower(r); });
109 while ((pos = str.find(substr, pos)) != std::string::npos)
111 pos += substr.size();
120 size_t pos = str.find(substr);
121 if (pos == std::string::npos)
124 str.replace(pos, substr.size(), with);
130 size_t pos = str.rfind(substr);
131 if (pos == std::string::npos)
134 str.replace(pos, substr.size(), with);
143 while ((pos = str.find(substr, pos)) != std::string::npos)
145 str.replace(pos, substr.size(), with);
155 std::vector<std::string> tokens;
163 pos_current = str.find(delimiter, pos_last);
164 if (pos_current == std::string::npos)
165 pos_current = str.size();
167 length = pos_current - pos_last;
168 if (!skip_empty || (length != 0))
169 tokens.emplace_back(str.substr(pos_last, length));
171 if (pos_current == str.size())
174 pos_last = pos_current + 1;
180 std::vector<std::string>
StringUtils::Split(std::string_view str, std::string_view delimiter,
bool skip_empty)
182 std::vector<std::string> tokens;
190 pos_current = str.find(delimiter, pos_last);
191 if (pos_current == std::string::npos)
192 pos_current = str.size();
194 length = pos_current - pos_last;
195 if (!skip_empty || (length != 0))
196 tokens.emplace_back(str.substr(pos_last, length));
198 if (pos_current == str.size())
201 pos_last = pos_current + delimiter.size();
209 std::vector<std::string> tokens;
217 pos_current = str.find_first_of(delimiters, pos_last);
218 if (pos_current == std::string::npos)
219 pos_current = str.size();
221 length = pos_current - pos_last;
222 if (!skip_empty || (length != 0))
223 tokens.emplace_back(str.substr(pos_last, length));
225 if (pos_current == str.size())
228 pos_last = pos_current + 1;
234 std::string
StringUtils::Join(
const std::vector<std::string>& tokens,
bool skip_empty,
bool skip_blank)
239 std::ostringstream result;
241 for (
size_t i = 0; i < tokens.size(); ++i)
242 if (!((skip_empty && tokens[i].empty()) || (skip_blank &&
IsBlank(tokens[i]))))
248 std::string
StringUtils::Join(
const std::vector<std::string>& tokens,
char delimiter,
bool skip_empty,
bool skip_blank)
253 std::ostringstream result;
255 for (
size_t i = 0; i < tokens.size() - 1; ++i)
256 if (!((skip_empty && tokens[i].empty()) || (skip_blank &&
IsBlank(tokens[i]))))
257 result << tokens[i] << delimiter;
259 if (!((skip_empty && tokens[tokens.size() - 1].empty()) || (skip_blank &&
IsBlank(tokens[tokens.size() - 1]))))
260 result << tokens[tokens.size() - 1];
265 std::string
StringUtils::Join(
const std::vector<std::string>& tokens,
const char* delimiter,
bool skip_empty,
bool skip_blank)
270 std::ostringstream result;
272 for (
size_t i = 0; i < tokens.size() - 1; ++i)
273 if (!((skip_empty && tokens[i].empty()) || (skip_blank &&
IsBlank(tokens[i]))))
274 result << tokens[i] << delimiter;
276 if (!((skip_empty && tokens[tokens.size() - 1].empty()) || (skip_blank &&
IsBlank(tokens[tokens.size() - 1]))))
277 result << tokens[tokens.size() - 1];
282 std::string
StringUtils::Join(
const std::vector<std::string>& tokens, std::string_view delimiter,
bool skip_empty,
bool skip_blank)
287 std::ostringstream result;
289 for (
size_t i = 0; i < tokens.size() - 1; ++i)
290 if (!((skip_empty && tokens[i].empty()) || (skip_blank &&
IsBlank(tokens[i]))))
291 result << tokens[i] << delimiter;
293 if (!((skip_empty && tokens[tokens.size() - 1].empty()) || (skip_blank &&
IsBlank(tokens[tokens.size() - 1]))))
294 result << tokens[tokens.size() - 1];
302 std::string value =
ToLower(str);
303 if ((value ==
"true") || (value ==
"yes") || (value ==
"on") || (value ==
"1"))
305 if ((value ==
"false") || (value ==
"no") || (value ==
"off") || (value ==
"0"))
308 assert(
"Invalid boolean value represented in string!");
static bool Compare(std::string_view str1, std::string_view str2)
Compare two strings case sensitive version.
static std::vector< std::string > SplitByAny(std::string_view str, std::string_view delimiters, bool skip_empty=false)
Split the string into tokens by the any character in the given delimiter string.
static size_t CountAll(std::string_view str, std::string_view substr)
Count all occurrences of substring.
static std::vector< std::string > Split(std::string_view str, char delimiter, bool skip_empty=false)
Split the string into tokens by the given delimiter character.
static std::string ToRTrim(std::string_view str)
Trims space characters from the end of the given constant string.
static bool StartsWith(std::string_view str, std::string_view prefix)
Checks the given string for specific prefix.
static std::string & LTrim(std::string &str)
Trims space characters from the start of the given string.
static T FromString(std::string_view str)
Converts strings to arbitrary datatypes using std::istringstream.
static bool ReplaceAll(std::string &str, std::string_view substr, std::string_view with)
Replace all occurrences of substring with another substring.
static std::string ToLTrim(std::string_view str)
Trims space characters from the start of the given constant string.
static std::string Join(const std::vector< std::string > &tokens, bool skip_empty=false, bool skip_blank=false)
Join tokens into the string.
static char ToLower(char ch)
Convert the given character to lower case.
static bool ReplaceFirst(std::string &str, std::string_view substr, std::string_view with)
Replace the first occurrence of substring with another substring.
static std::string & RTrim(std::string &str)
Trims space characters from the end of the given string.
static bool CompareNoCase(std::string_view str1, std::string_view str2)
Compare two strings case insensitive version.
static std::string ToTrim(std::string_view str)
Trims space characters from the both sides of the given constant string.
static bool IsBlank(char ch)
Is the given character blank (empty or contains only space characters)?
static bool ReplaceLast(std::string &str, std::string_view substr, std::string_view with)
Replace the last occurrence of substring with another substring.
static bool IsPatternMatch(const std::string &patterns, const std::string &str)
Is the given string match to the given patterns?
C++ Common project definitions.
String utilities definition.