19#ifndef __STRING_UTILS__
20#define __STRING_UTILS__
32 inline bool isInt(
const std::string& val) {
34 std::regex(
"[\\+-]?((\\d+(\\.(0*))?)(e[\\+]?\\d+)?)", std::regex::icase);
36 return std::regex_match(val, r_int);
42 inline bool isFloat(
const std::string& val) {
43 std::regex r_float = std::regex(
44 "[\\+-]?(((\\d*\\.\\d+)|(\\d+(\\.(0*))?))(e[\\+-]?((\\d*\\.\\d+)|(\\d+(\\.(0*))?)))?)",
47 return std::regex_match(val, r_float);
54 inline T getValueFromString(std::string
const& s)
57 std::istringstream iss;
68 inline std::string getValueFromString<std::string>(std::string
const& s)
77 inline T getValueFromString(
const char* s)
83 return getValueFromString<T>(str);
90 std::string toString(T
const& var)
92 std::ostringstream oss;
104 template<
typename T,
typename U>
105 std::vector<U> split(T
const& value,
const char* key,
bool keepEmptyPart =
false)
107 std::vector<U> result;
111 std::string copy(toString<T>(value));
116 while ((pos = copy.find(key)) != std::string::npos)
118 sub = copy.substr(0, pos);
119 copy.erase(0, pos + size);
120 if (!sub.empty() || keepEmptyPart)
121 result.push_back(getValueFromString<U>(sub));
124 result.push_back(getValueFromString<U>(copy));
128 template<
typename T,
typename U>
129 std::vector<U> split(T
const& value, std::string
const& key,
bool keepEmptyPart =
false)
131 return split<T, U>(value, key.c_str(), keepEmptyPart);
139 std::string join(std::vector<T>
const& c,
const char* separator)
141 std::ostringstream os;
151 if (i < c.size() && separator)
158 std::string join(std::vector<T>
const& c, std::string
const& separator)
160 return join<T>(c, separator.c_str());
166 T getBetween(std::string
const& c,
const char* key1,
const char* key2)
170 if (!key1 || !key2 ||
171 (pos1 = c.find(key1)) == std::string::npos ||
172 (pos2 = c.find(key2)) == std::string::npos)
173 return getValueFromString<T>(
"");
174 pos1 += strlen(key1);
176 return getValueFromString<T>(
"");
177 return getValueFromString<T>(c.substr(pos1, pos2 - pos1).c_str());
181 T getBetween(
const char* st,
const char* key1,
const char* key2)
187 return getBetween<T>(str, key1, key2);
194 std::vector<T> getListOfItem(std::string st,
const char* key1,
const char* key2)
199 int length(key2 ? strlen(key2) : 0);
201 str = getBetween<T>(st, key1, key2);
202 for (; !str.empty(); str = getBetween<T>(st, key1, key2))
205 if ((find = st.find(key1)) == std::string::npos)
207 st.erase(find, str.size() + length);
213 std::vector<T> getListOfItem(
const char* st,
const char* key1,
const char* key2)
219 return getListOfItem<T>(s, key1, key2);
226 std::string replace(T
const& s,
const char* toReplace,
const char* replacement)
230 if (!toReplace || !replacement)
231 return toString<T>(s);
232 vec = split<T, T>(s, toReplace);
233 return join<T>(vec, replacement);
237 std::string replace(T
const& s, std::string
const& toReplace,
238 std::string
const& replacement)
240 return replace<T>(s, toReplace.c_str(), replacement.c_str());
244 std::string replace(T
const& s,
const char* toReplace,
245 std::string
const& replacement)
247 return replace<T>(s, toReplace, replacement.c_str());
251 std::string replace(T
const& s, std::string
const& toReplace,
252 const char* replacement)
254 return replace<T>(s, toReplace.c_str(), replacement);
260 unsigned int numberOfOccurence(std::string
const& s, T toFind)
262 std::string tmp(toString<T>(toFind)), tmp2(s);
263 unsigned int found(0);
266 if (s ==
"" || toFind ==
"")
268 while ((pos = tmp2.find(tmp, 0)) != std::string::npos) {
270 tmp2.erase(0, pos + 1);
276 unsigned int numberOfOccurence(
char const* s, T toFind)
278 std::string tmp(s ? s :
"");
282 return numberOfOccurence<T>(tmp, toFind);
287 T truncate(T& s,
unsigned int position)
289 std::string tmp = toString<T>(s);
291 if (position < tmp.length())
292 tmp.resize(position);
293 return getValueFromString<T>(tmp);