46 template<
typename T,
typename U>
54 }
catch (
const std::exception& e) {
55 throw bad_lexical_cast(e.what());
61 inline std::string
to_str(
const T& val) {
62 return lexical_cast<string>(val);
67 inline std::string
to_str(
const std::vector<T>& vec) {
69 for (size_t i = 0; i < vec.size(); ++i) {
70 rtn += to_str(vec[i]);
71 if (i < vec.size()-1) rtn +=
", ";
80 ss << setfill(
'0') << setw(
static_cast<
int>(nchars)) << val;
85 inline std::string
join(
const std::vector<std::string>& svec,
const std::string& sep) {
87 for (size_t i = 0; i < svec.size(); ++i) {
89 if (i < svec.size()-1) rtn += sep;
99 const size_t delim_pos = tmp.find(sep);
100 if (delim_pos == string::npos)
break;
101 const string stmp = tmp.substr(0, delim_pos);
102 if (!stmp.empty()) rtn.push_back(stmp);
103 tmp.replace(0, delim_pos+1,
"");
105 if (!tmp.empty()) rtn.push_back(tmp);
110 inline bool contains(
const std::string& s,
const std::string& sub) {
111 return s.find(sub) != string::npos;
115 inline bool startswith(
const std::string& s,
const std::string& sub) {
116 return s.find(sub) == 0;
120 inline bool endswith(
const std::string& s,
const std::string& sub) {
121 return s.find(sub) == s.length()-sub.length();
125 inline size_t
countchar(
const std::string& s,
const char c) {
126 return std::count(s.begin(), s.end(), c);
130 inline std::string
trim(
const std::string& s) {
131 const size_t firstnonspacepos = s.find_first_not_of(
" ");
132 const size_t lastnonspacepos = s.find_last_not_of(
" ");
133 if (firstnonspacepos == std::string::npos)
return "";
134 return s.substr(firstnonspacepos, lastnonspacepos-firstnonspacepos+1);
138 inline std::string
to_lower(
const std::string& s) {
140 transform(rtn.begin(), rtn.end(), rtn.begin(),
static_cast<
int(*)(
int)>(tolower));
145 inline std::string
to_upper(
const std::string& s) {
147 transform(rtn.begin(), rtn.end(), rtn.begin(),
static_cast<
int(*)(
int)>(toupper));
167 inline std::string
operator / (
const std::string& a,
const std::string& b) {
169 const string anorm = (a.find(
"/") != std::string::npos) ? a.substr(0, a.find_last_not_of(
"/")+1) : a;
170 const string bnorm = (b.find(
"/") != std::string::npos) ? b.substr(b.find_first_not_of(
"/")) : b;
171 return anorm +
"/" + bnorm;
175 inline std::string
basename(
const std::string& p) {
176 if (!contains(p,
"/"))
return p;
177 return p.substr(p.rfind(
"/")+1);
181 inline std::string
dirname(
const std::string& p) {
182 if (!contains(p,
"/"))
return "";
183 return p.substr(0, p.rfind(
"/"));
188 if (!contains(f,
"."))
return f;
189 return f.substr(0, f.rfind(
"."));
194 if (!contains(f,
"."))
return "";
195 return f.substr(f.rfind(
".")+1);
207 template <
typename N>
208 inline N
sqr(
const N& x) {
return x*x; }
211 template <
typename N>
212 inline int sgn(N val) {
return (N(0) < val) - (val < N(0)); }
215 inline int in_range(
double x,
double low,
double high) {
return x >= low && x < high; }
218 inline int in_closed_range(
double x,
double low,
double high) {
return x >= low && x <= high; }
221 inline int in_open_range(
double x,
double low,
double high) {
return x > low && x < high; }
238 template <
typename T>
239 inline bool contains(
const std::vector<T>& container,
const T& item) {
240 return find(container.begin(), container.end(), item) != container.end();
250 template <
typename K,
typename T>
251 inline bool has_key(
const std::map<K,T>& container,
const K& key) {
252 return container.find(key) != container.end();