42 #ifndef _DWMSTRINGUTILS_HH_ 43 #define _DWMSTRINGUTILS_HH_ 61 namespace StringUtils {
68 const std::string & suffix);
76 std::vector<std::string> & result);
82 :
public std::runtime_error
89 : std::runtime_error(s)
97 bool StringToIntegral(
const std::string & s, T & target,
int base = 0)
101 if (std::numeric_limits<T>::is_signed) {
105 long long val = strtoll(s.c_str(), &endptr, base);
106 if ((! errno) && (endptr != s.c_str()) &&
107 (val <= static_cast<long long>(std::numeric_limits<T>::max())) &&
108 (val >= static_cast<long long>(std::numeric_limits<T>::min()))) {
109 target =
static_cast<T
>(val);
117 unsigned long long val = strtoull(s.c_str(), &endptr, base);
118 if ((! errno) && (endptr != s.c_str()) &&
119 (val <= static_cast<T>(std::numeric_limits<T>::max())) &&
120 (val >= static_cast<T>(std::numeric_limits<T>::min()))) {
121 target =
static_cast<T
>(val);
131 template <
typename T>
132 bool StringTo(
const std::string & s, T & target)
136 if (! std::numeric_limits<T>::digits10) {
138 std::istringstream is(s);
139 if ((is >> target)) {
149 bool StringTo(
const std::string & s, std::string & target);
154 bool StringTo(
const std::string & s,
char & target,
int base = 0);
159 bool StringTo(
const std::string & s,
signed char & target,
int base = 0);
164 bool StringTo(
const std::string & s,
unsigned char & target,
170 bool StringTo(
const std::string & s,
short & target,
int base = 0);
175 bool StringTo(
const std::string & s,
unsigned short & target,
181 bool StringTo(
const std::string & s,
int & target,
int base = 0);
186 bool StringTo(
const std::string & s,
unsigned int & target,
int base = 0);
191 bool StringTo(
const std::string & s,
long & target,
int base = 0);
196 bool StringTo(
const std::string & s,
unsigned long & target,
202 bool StringTo(
const std::string & s,
long long & target,
int base = 0);
207 bool StringTo(
const std::string & s,
unsigned long long & target,
213 bool StringTo(
const std::string & s,
float & target);
218 bool StringTo(
const std::string & s,
double & target);
223 bool StringTo(
const std::string & s,
bool & target);
228 template <
typename T>
229 static T StringTo(
const std::string & s)
232 if (! StringTo(s, x)) {
233 std::ostringstream os;
234 os <<
"\"" << s <<
"\" -> " <<
TypeName(x);
243 template <
typename T>
244 std::string ToString(
const T & x,
245 std::ios_base & (*iom)(std::ios_base &) = std::dec)
247 std::ostringstream os;
248 os << std::setprecision(500);
260 #endif // _DWMSTRINGUTILS_HH_ bool HasSuffix(const std::string &str, const std::string &suffix)
Returns true if str ends with suffix.
Hackish support for getting the name of a given object's type, since std::type_info::name() returns a...
Simple exception class for StringTo().
Definition: DwmStringUtils.hh:81
bool StringToVector(const std::string &s, char sep, std::vector< std::string > &result)
Populates result with substrings from s, where each substring is separated by sep in s...
std::string TypeName(const T &x)
Returns the name of type T.
Definition: DwmTypeName.hh:84
Definition: DwmBZ2IO.hh:67