28 #ifndef __RADAR_STRING_HPP__
29 #define __RADAR_STRING_HPP__
42 #define snprintf _snprintf
49 namespace stringutils {
62 RADAR_API
void split (
const std::string& str, std::string& first, std::string& second,
const char separator =
',');
71 RADAR_API
void split (
const std::string& str, std::vector<std::string>& tokens,
const std::string& delimiters =
" ");
84 RADAR_API std::string&
trimleft (std::string& str);
92 RADAR_API std::string
trimleft (
const std::string& str);
100 RADAR_API std::string&
trimright (std::string& str);
108 RADAR_API std::string
trimright (
const std::string& str);
116 RADAR_API std::string&
trim (std::string& str);
124 RADAR_API std::string
trim (
const std::string& str);
139 template<
class T> std::string
toString(
const T& value)
141 std::ostringstream stream; stream << std::fixed << value;
return stream.str();
151 RADAR_API std::string
toString(
bool val);
159 RADAR_API std::string
toString(
int val);
167 RADAR_API std::string
toString(
long val);
175 RADAR_API std::string
toString(
float val);
183 RADAR_API std::string
toString(
double val);
191 RADAR_API std::string
toString(
size_t val);
204 template<
class T> std::string
toString(
const std::vector<T>& val,
const char* sep =
",")
206 std::ostringstream ss;
208 for (
size_t i=0; i<val.size(); i++)
227 RADAR_API std::string
toString(
const std::vector<bool>& val,
const char* sep =
",");
238 RADAR_API std::string
toString(
const std::vector<int>& val,
const char* sep =
",");
249 RADAR_API std::string
toString(
const std::vector<long>& val,
const char* sep =
",");
260 RADAR_API std::string
toString(
const std::vector<float>& val,
const char* sep =
",");
271 RADAR_API std::string
toString(
const std::vector<double>& val,
const char* sep =
",");
282 template <
class T> std::string
toString(
const std::vector<std::pair<T,T> > value,
const char* sep =
",")
284 std::ostringstream ss;
285 for (
size_t i=0; i<value.size(); i++)
289 ss << toString<T>(value[i].first) <<
":" << toString<T>(value[i].second);
308 template <
class T>
static T
parse(
const std::string& str,
const std::string& typestr)
313 std::istringstream ss(str);
317 throw std::invalid_argument(
"'" + str +
"' is not a valid " + typestr +
" value");
327 RADAR_API
bool parseBool (
const std::string& str);
335 RADAR_API
int parseInt (
const std::string& str);
343 RADAR_API
float parseFloat (
const std::string& str);
351 RADAR_API
double parseDouble (
const std::string& str);
364 RADAR_API
void parseSeq (
const std::string& str, std::vector<bool>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
376 RADAR_API
void parseSeq (
const std::string& str, std::vector<int>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
388 RADAR_API
void parseSeq (
const std::string& str, std::vector<double>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
400 RADAR_API
void parseSeq (
const std::string& str, std::vector<std::string>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
410 RADAR_API
bool isInt (
const std::string& str);
void split(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)
Splits a string into substrings using the chacatars of a given string as tokens separators.
Definition: string.cpp:34
float parseFloat(const std::string &s)
Parse a std::string to a float value.
Definition: string.cpp:155
void parseSeq(const std::string &s, std::vector< bool > &val, const char *sep, const bool allowEmptyStr)
Parse a string sequence of boolean values to a std::vector.
Definition: string.cpp:158
static T parse(const std::string &str, const std::string &typestr)
Parse a std::string to a given type value.
Definition: string.hpp:308
std::string & trim(std::string &s)
remove all space to the left and the right of a string
Definition: string.cpp:91
int parseInt(const std::string &s)
Parse a std::string to an int value.
Definition: string.cpp:154
std::string & trimleft(std::string &s)
removes all spaces to the left of a string
Definition: string.cpp:55
bool parseBool(const std::string &s)
Parse a std::string to a boolean value.
Definition: string.cpp:135
bool isInt(const std::string &s)
Check if the string is a number.
Definition: string.cpp:128
double parseDouble(const std::string &s)
Parse a std::string to a double value.
Definition: string.cpp:156
std::string toString(bool value)
Convert a boolean value to its string rapresentation (0/1).
Definition: string.cpp:109
std::string & trimright(std::string &s)
removes all spaces to the right of a string
Definition: string.cpp:72