23 #include <boost/algorithm/string.hpp> 39 for (
const auto& e : s) {
40 status = (status && isdigit(e));
51 std::stringstream ss(s);
58 inline std::vector<std::string>
Split(
const std::string& s,
const std::string& delimiters)
60 std::vector<std::string> tokens;
61 boost::algorithm::split(tokens, s, boost::is_any_of(delimiters));
67 inline std::vector<std::string>
Tokenize(
const std::string& str,
const std::string& delimiters)
69 std::vector<std::string> tokens;
72 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
74 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
76 while (std::string::npos != pos || std::string::npos != lastPos) {
78 tokens.push_back(str.substr(lastPos, pos - lastPos));
80 lastPos = str.find_first_not_of(delimiters, pos);
82 pos = str.find_first_of(delimiters, lastPos);
98 inline std::string ToString<std::string>(
const std::string& value)
104 template <
typename It>
106 typename std::enable_if<!std::is_same<typename It::value_type, std::string>::value, It>::type first, It last)
108 std::vector<std::string> v;
109 for (It it = first; it < last; ++it) {
116 template <
typename It>
118 typename std::enable_if<std::is_same<typename It::value_type, std::string>::value, It>::type first, It last)
120 std::vector<std::string> v;
121 std::copy(first, last, back_inserter(v));
126 template <
typename T>
127 inline std::string
ToString(
const T& value,
int width,
char fill =
' ')
129 std::stringstream ss;
130 ss << std::setw(width) << std::setfill(fill) << value;
135 inline std::string
ToLower(
const std::string& source)
137 auto lower = [](
int c) ->
int {
return std::tolower(c); };
139 std::transform(source.begin(), source.end(), std::back_inserter(copy), lower);
144 inline std::string
ToUpper(
const std::string& source)
146 auto upper = [](
int c) ->
int {
return std::toupper(c); };
148 std::transform(source.begin(), source.end(), std::back_inserter(copy), upper);
153 inline std::string
TrimRight(
const std::string& source,
const std::string& t =
" ")
155 std::string str = source;
156 return str.erase(str.find_last_not_of(t) + 1);
160 inline std::string
TrimLeft(
const std::string& source,
const std::string& t =
" ")
162 std::string str = source;
163 return str.erase(0, source.find_first_not_of(t));
167 inline std::string
Trim(
const std::string& source,
const std::string& t =
" ")
172 template <
typename T>
175 std::string valueStr = std::to_string(value);
178 std::size_t rest = valueStr.length() % 3;
180 res += valueStr.substr(0, rest);
182 for (
size_t i = rest; i < valueStr.length(); i += 3) {
183 res +=
"." + valueStr.substr(i, 3);
187 return res.substr(1);
std::string ToString(const T &value)
Builds a string representation of a value of type T.
T FromString(const std::string &s)
Builds a value of type T representation from a string.
std::string ToUpper(const std::string &source)
Builds a string with upper case characters only.
std::string intToDottedString(const T &value)
std::vector< std::string > Split(const std::string &s, const std::string &delimiters)
Split a string (in order of occurence) by splitting it on the given delimiters.
std::vector< std::string > Tokenize(const std::string &str, const std::string &delimiters)
Tokenize a string (in order of occurence) with the given delimiters.
std::string TrimLeft(const std::string &source, const std::string &t=" ")
Trim characters at left end of string.
std::string TrimRight(const std::string &source, const std::string &t=" ")
Trim characters at right end of string.
std::string Trim(const std::string &source, const std::string &t=" ")
Trim characters at both ends of string.
Namespace for the simulator and related classes.
std::string ToLower(const std::string &source)
Builds a string with lower case characters only.
bool CheckAllDigits(const std::string &s)
All characters in string are digits (or string is empty)