24 const char* delimiters,
25 bool tokenCompression =
true)
27 std::stringstream stringStream(str);
29 std::vector<std::string> tokenVector;
30 while (std::getline(stringStream, line))
34 while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos)
39 tokenVector.push_back(line.substr(prev, pos - prev));
42 else if (!tokenCompression)
44 tokenVector.push_back(line.substr(prev, pos - prev));
48 if (prev < line.length())
50 tokenVector.push_back(line.substr(prev, std::string::npos));
62 inline std::string&
StringStartTrim(std::string& str,
const std::string& chars =
"\t\n\v\f\r ")
64 str.erase(0, str.find_first_not_of(chars));
71 inline std::string&
StringEndTrim(std::string& str,
const std::string& chars =
"\t\n\v\f\r ")
73 str.erase(str.find_last_not_of(chars) + 1);
80 inline std::string&
StringTrim(std::string& str,
const std::string& chars =
"\t\n\v\f\r ")
88 inline std::string
StringTrimCopy(
const std::string& str,
const std::string& chars =
"\t\n\v\f\r ")
90 std::string strCopy = str;
96 inline std::string
StringConcat(
const std::vector<std::string>& strings, std::string seperator =
"")
99 for (
auto string : strings)
101 ss <<
string << seperator;
110 const std::string& oldStr,
111 const std::string& newStr)
113 std::string::size_type pos = 0u;
114 while ((pos = str.find(oldStr, pos)) != std::string::npos)
116 str.replace(pos, oldStr.length(), newStr);
117 pos += newStr.length();
129 inline bool StringToBool(
const std::string& s,
bool throw_on_error =
true)
135 std::istringstream is(s);
143 std::string s_lower = s;
144 std::transform(s_lower.begin(),
147 [](
unsigned char c){ return std::tolower(c); });
151 is >> std::boolalpha >> result;
154 if (is.fail() && throw_on_error)
std::vector< std::string > StringTokenizer(const std::string &str, const char *delimiters, bool tokenCompression=true)
Function to take a string and a list of delimiters and split the string into tokens based on those de...
void StringReplaceAll(std::string &str, const std::string &oldStr, const std::string &newStr)
Iterates over a given str and replaces all instance of substring oldStr with newStr.
std::string & StringStartTrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trim from the start of a string.
std::string & StringTrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trim from both the start and the end of a string.
bool StringToBool(const std::string &s, bool throw_on_error=true)
Converts a string to bool.
std::string StringConcat(const std::vector< std::string > &strings, std::string seperator="")
Takes a vector of strings and concatenates them together into one long std::string with an optional s...
std::string StringTrimCopy(const std::string &str, const std::string &chars="\t\n\v\f\r ")
Trim from both the start and the end of a string, returns a trimmed copy of the string.
std::string & StringEndTrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trim for the end of a string.
Copyright (c) 2021 ARM Limited and Contributors.