2 #ifndef WIBBLE_STRING_H
3 #define WIBBLE_STRING_H
43 using namespace wibble::operators;
46 static int vasprintf (
char **,
const char *, va_list);
49 std::string
fmt(
const char* f, ... ) __attribute__ ((deprecated));
50 std::
string fmtf( const
char* f, ... );
51 template< typename T > inline std::
string fmt(const T& val);
55 template< typename X >
56 inline typename
TPair< std::ostream, typename X::Type >::First &operator<<(
57 std::ostream &o, X list )
63 while( !list.empty() ) {
64 o <<
fmt( list.head() );
65 if ( !list.tail().empty() )
73 template<
typename T >
74 inline std::string
fmt(
const T& val)
76 std::stringstream str;
81 template<>
inline std::string fmt<std::string>(
const std::string& val) {
84 template<>
inline std::string
fmt<char*>(
char *
const & val) {
return val; }
86 template<
typename C >
95 for (
typename C::const_iterator i = c.begin(); i != c.end(); ++i ) {
97 if ( i != c.end() && i + 1 != c.end() )
106 template<
typename X >
107 inline std::string
fmt(
const std::set< X >& val) {
112 template<
typename X >
113 inline std::string
fmt(
const std::vector< X > &val) {
118 inline std::string
basename(
const std::string& pathname)
120 size_t pos = pathname.rfind(
"/");
121 if (pos == std::string::npos)
124 return pathname.substr(pos+1);
128 inline std::string
dirname(
const std::string& pathname)
130 size_t pos = pathname.rfind(
"/");
131 if (pos == std::string::npos)
132 return std::string();
135 return std::string(
"/");
137 return pathname.substr(0, pos);
145 std::string
normpath(
const std::string& pathname);
148 inline bool startsWith(
const std::string& str,
const std::string& part)
150 if (str.size() < part.size())
152 return str.substr(0, part.size()) == part;
156 inline bool endsWith(
const std::string& str,
const std::string& part)
158 if (str.size() < part.size())
160 return str.substr(str.size() - part.size()) == part;
163 inline std::string
replace(
const std::string& str,
char from,
char to)
166 res.reserve(str.size());
167 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
175 #if !__xlC__ && (! __GNUC__ || __GNUC__ >= 4)
180 template<
typename FUN>
181 inline std::string
trim(
const std::string& str,
const FUN& classifier)
187 size_t end = str.size() - 1;
188 while (beg < end && classifier(str[beg]))
190 while (end >= beg && classifier(str[end]))
193 return str.substr(beg, end-beg+1);
199 inline std::string
trim(
const std::string& str)
201 return trim(str, ::isspace);
204 inline std::string
trim(
const std::string& str)
211 size_t end = str.size() - 1;
212 while (beg < end && ::isspace(str[beg]))
214 while (end >= beg && ::isspace(str[end]))
217 return str.substr(beg, end-beg+1);
222 inline std::string
toupper(
const std::string& str)
225 res.reserve(str.size());
226 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
232 inline std::string
tolower(
const std::string& str)
235 res.reserve(str.size());
236 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
242 inline std::string
ucfirst(
const std::string& str)
244 if (str.empty())
return str;
247 return res +
tolower(str.substr(1));
251 inline std::string
joinpath(
const std::string& path1,
const std::string& path2)
258 if (path1[path1.size() - 1] ==
'/')
260 return path1 + path2.substr(1);
262 return path1 + path2;
265 return path1 + path2;
267 return path1 +
'/' + path2;
271 std::string
urlencode(
const std::string& str);
274 std::string
urldecode(
const std::string& str);
303 const std::string& sep;
304 const std::string& str;
309 const_iterator(
const std::string& sep,
const std::string& str) : sep(sep), str(str), pos(0)
313 const_iterator(
const std::string& sep,
const std::string& str,
bool) : sep(sep), str(str), pos(std::string::npos) {}
317 if (pos == str.size())
318 pos = std::string::npos;
323 if (pos + 1 == str.size())
324 end = std::string::npos;
328 end = str.find(sep, pos);
329 if (end == std::string::npos)
331 cur = str.substr(pos);
336 cur = str.substr(pos, end-pos);
337 pos = end + sep.size();
345 if (pos == std::string::npos)
346 return std::string();
348 return str.substr(pos);
363 return pos == ti.pos;
369 return pos != ti.pos;
376 Split(
const std::string& sep,
const std::string& str) : sep(sep), str(str) {}
385 template<
typename ITER>
386 std::string
join(
const ITER&
begin,
const ITER& end,
const std::string& sep =
", ")
388 std::stringstream res;
390 for (ITER i = begin; i !=
end; ++i)
422 std::pair<std::string, std::string> value;
431 const std::pair<std::string, std::string>&
operator*()
const
435 const std::pair<std::string, std::string>*
operator->()
const