28#ifndef __RADAR_STRING_HPP__
29#define __RADAR_STRING_HPP__
42#define snprintf _snprintf
49namespace stringutils {
62RADAR_API
void split (
const std::string& str, std::string& first, std::string& second,
const char separator =
',');
71RADAR_API
void split (
const std::string& str, std::vector<std::string>& tokens,
const std::string& delimiters =
" ");
84RADAR_API std::string&
trimleft (std::string& str);
92RADAR_API std::string
trimleft (
const std::string& str);
100RADAR_API std::string&
trimright (std::string& str);
108RADAR_API std::string
trimright (
const std::string& str);
116RADAR_API std::string&
trim (std::string& str);
124RADAR_API std::string
trim (
const std::string& str);
139template<
class T> std::string
toString(
const T& value)
141 std::ostringstream stream; stream << std::fixed << value;
return stream.str();
151RADAR_API std::string
toString(
bool val);
159RADAR_API std::string
toString(
int val);
167RADAR_API std::string
toString(
long val);
175RADAR_API std::string
toString(
float val);
183RADAR_API std::string
toString(
double val);
191RADAR_API std::string
toString(
size_t val);
204template<
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++)
227RADAR_API std::string
toString(
const std::vector<bool>& val,
const char* sep =
",");
238RADAR_API std::string
toString(
const std::vector<int>& val,
const char* sep =
",");
249RADAR_API std::string
toString(
const std::vector<long>& val,
const char* sep =
",");
260RADAR_API std::string
toString(
const std::vector<float>& val,
const char* sep =
",");
271RADAR_API std::string
toString(
const std::vector<double>& val,
const char* sep =
",");
282template <
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);
308template <
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");
327RADAR_API
bool parseBool (
const std::string& str);
335RADAR_API
int parseInt (
const std::string& str);
343RADAR_API
float parseFloat (
const std::string& str);
351RADAR_API
double parseDouble (
const std::string& str);
364RADAR_API
void parseSeq (
const std::string& str, std::vector<bool>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
376RADAR_API
void parseSeq (
const std::string& str, std::vector<int>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
388RADAR_API
void parseSeq (
const std::string& str, std::vector<double>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
400RADAR_API
void parseSeq (
const std::string& str, std::vector<std::string>& val,
const char* sep =
",",
const bool allowEmptyStr =
true);
410RADAR_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