41#ifndef _DWMSTRINGUTILS_HH_
42#define _DWMSTRINGUTILS_HH_
60 namespace StringUtils {
67 const std::string & suffix);
77 std::vector<std::string> & result,
78 bool ignoreEmpty =
true);
84 :
public std::runtime_error
91 : std::runtime_error(s)
99 bool StringToIntegral(
const std::string & s, T & target,
int base = 0)
103 if (std::numeric_limits<T>::is_signed) {
107 long long val = strtoll(s.c_str(), &endptr, base);
108 if ((! errno) && (endptr != s.c_str()) &&
109 (val <=
static_cast<long long>(std::numeric_limits<T>::max())) &&
110 (val >=
static_cast<long long>(std::numeric_limits<T>::min()))) {
111 target =
static_cast<T
>(val);
119 unsigned long long val = strtoull(s.c_str(), &endptr, base);
120 if ((! errno) && (endptr != s.c_str()) &&
121 (val <=
static_cast<T
>(std::numeric_limits<T>::max())) &&
122 (val >=
static_cast<T
>(std::numeric_limits<T>::min()))) {
123 target =
static_cast<T
>(val);
133 template <
typename T>
134 bool StringTo(
const std::string & s, T & target)
138 if (! std::numeric_limits<T>::digits10) {
140 std::istringstream is(s);
141 if ((is >> target)) {
151 bool StringTo(
const std::string & s, std::string & target);
156 bool StringTo(
const std::string & s,
char & target,
int base = 0);
161 bool StringTo(
const std::string & s,
signed char & target,
int base = 0);
166 bool StringTo(
const std::string & s,
unsigned char & target,
172 bool StringTo(
const std::string & s,
short & target,
int base = 0);
177 bool StringTo(
const std::string & s,
unsigned short & target,
183 bool StringTo(
const std::string & s,
int & target,
int base = 0);
188 bool StringTo(
const std::string & s,
unsigned int & target,
int base = 0);
193 bool StringTo(
const std::string & s,
long & target,
int base = 0);
198 bool StringTo(
const std::string & s,
unsigned long & target,
204 bool StringTo(
const std::string & s,
long long & target,
int base = 0);
209 bool StringTo(
const std::string & s,
unsigned long long & target,
215 bool StringTo(
const std::string & s,
float & target);
220 bool StringTo(
const std::string & s,
double & target);
225 bool StringTo(
const std::string & s,
bool & target);
230 template <
typename T>
231 static T StringTo(
const std::string & s)
234 if (! StringTo(s, x)) {
235 std::ostringstream os;
236 os <<
"\"" << s <<
"\" -> " <<
TypeName(x);
245 template <
typename T>
246 std::string ToString(
const T & x,
247 std::ios_base & (*iom)(std::ios_base &) = std::dec)
249 std::ostringstream os;
250 os << std::setprecision(500);
bool HasSuffix(const std::string &str, const std::string &suffix)
Returns true if str ends with suffix.
bool StringToVector(const std::string &s, char sep, std::vector< std::string > &result, bool ignoreEmpty=true)
Populates result with substrings from s, where each substring is separated by sep in s.
Hackish support for getting the name of a given object's type, since std::type_info::name() returns a...
std::string TypeName(const T &x)
Returns the name of type T.
Definition DwmTypeName.hh:83
Simple exception class for StringTo().
Definition DwmStringUtils.hh:85