/usr/include/wibble/string.h is in libwibble-dev 0.1.28-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | // -*- C++ -*-
#ifndef WIBBLE_STRING_H
#define WIBBLE_STRING_H
/*
* Various string functions
*
* Copyright (C) 2007,2008 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <wibble/operators.h>
#include <wibble/sfinae.h>
#include <cstdarg>
#include <cstdio>
#include <string>
#include <set>
#include <vector>
#include <sstream>
#include <cctype>
#ifdef _WIN32
#include <cstring>
#include <cstdlib>
#endif
namespace wibble {
namespace str {
using namespace wibble::operators;
#ifdef _WIN32
static int vasprintf (char **, const char *, va_list);
#endif
std::string fmt( const char* f, ... ) __attribute__ ((deprecated));
std::string fmtf( const char* f, ... );
template< typename T > inline std::string fmt(const T& val);
// Formatting lists -- actually, we need to move list handling into wibble,
// really.
template< typename X >
inline typename TPair< std::ostream, typename X::Type >::First &operator<<(
std::ostream &o, X list )
{
if ( list.empty() )
return o << "[]";
o << "[ ";
while( !list.empty() ) {
o << fmt( list.head() );
if ( !list.tail().empty() )
o << ", ";
list = list.tail();
}
return o << " ]";
}
/// Format any value into a string using a std::stringstream
template< typename T >
inline std::string fmt(const T& val)
{
std::stringstream str;
str << val;
return str.str();
}
template<> inline std::string fmt<std::string>(const std::string& val) {
return val;
}
template<> inline std::string fmt<char*>(char * const & val) { return val; }
template< typename C >
inline std::string fmt_container( const C &c, char f, char l )
{
std::string s;
s += f;
if ( c.empty() )
return s + l;
s += ' ';
for ( typename C::const_iterator i = c.begin(); i != c.end(); ++i ) {
s += fmt( *i );
if ( i != c.end() && i + 1 != c.end() )
s += ", ";
}
s += ' ';
s += l;
return s;
}
// formatting sets using { ... } notation
template< typename X >
inline std::string fmt(const std::set< X >& val) {
return fmt_container( val, '{', '}' );
}
// formatting vectors using [ ... ] notation
template< typename X >
inline std::string fmt(const std::vector< X > &val) {
return fmt_container( val, '[', ']' );
}
/// Given a pathname, return the file name without its path
inline std::string basename(const std::string& pathname)
{
size_t pos = pathname.rfind("/");
if (pos == std::string::npos)
return pathname;
else
return pathname.substr(pos+1);
}
/// Given a pathname, return the directory name without the file name
inline std::string dirname(const std::string& pathname)
{
size_t pos = pathname.rfind("/");
if (pos == std::string::npos)
return std::string();
else if (pos == 0)
// Handle the case of '/foo'
return std::string("/");
else
return pathname.substr(0, pos);
}
/**
* Normalise a pathname.
*
* For example, A//B, A/./B and A/foo/../B all become A/B.
*/
std::string normpath(const std::string& pathname);
/// Check if a string starts with the given substring
inline bool startsWith(const std::string& str, const std::string& part)
{
if (str.size() < part.size())
return false;
return str.substr(0, part.size()) == part;
}
/// Check if a string ends with the given substring
inline bool endsWith(const std::string& str, const std::string& part)
{
if (str.size() < part.size())
return false;
return str.substr(str.size() - part.size()) == part;
}
inline std::string replace(const std::string& str, char from, char to)
{
std::string res;
res.reserve(str.size());
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
if (*i == from)
res.append(1, to);
else
res.append(1, *i);
return res;
}
#if !__xlC__ && (! __GNUC__ || __GNUC__ >= 4)
/**
* Return the substring of 'str' without all leading and trailing characters
* for which 'classifier' returns true.
*/
template<typename FUN>
inline std::string trim(const std::string& str, const FUN& classifier)
{
if (str.empty())
return str;
size_t beg = 0;
size_t end = str.size() - 1;
while (beg < end && classifier(str[beg]))
++beg;
while (end >= beg && classifier(str[end]))
--end;
return str.substr(beg, end-beg+1);
}
/**
* Return the substring of 'str' without all leading and trailing spaces.
*/
inline std::string trim(const std::string& str)
{
return trim(str, ::isspace);
}
#else
/// Workaround version for older gcc
inline std::string trim(const std::string& str)
{
if (str.empty())
return str;
size_t beg = 0;
size_t end = str.size() - 1;
while (beg < end && ::isspace(str[beg]))
++beg;
while (end >= beg && ::isspace(str[end]))
--end;
return str.substr(beg, end-beg+1);
}
#endif
/// Convert a string to uppercase
inline std::string toupper(const std::string& str)
{
std::string res;
res.reserve(str.size());
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
res += ::toupper(*i);
return res;
}
/// Convert a string to lowercase
inline std::string tolower(const std::string& str)
{
std::string res;
res.reserve(str.size());
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
res += ::tolower(*i);
return res;
}
/// Return the same string, with the first character uppercased
inline std::string ucfirst(const std::string& str)
{
if (str.empty()) return str;
std::string res;
res += ::toupper(str[0]);
return res + tolower(str.substr(1));
}
/// Join two paths, adding slashes when appropriate
inline std::string joinpath(const std::string& path1, const std::string& path2)
{
if (path1.empty())
return path2;
if (path2.empty())
return path1;
if (path1[path1.size() - 1] == '/')
if (path2[0] == '/')
return path1 + path2.substr(1);
else
return path1 + path2;
else
if (path2[0] == '/')
return path1 + path2;
else
return path1 + '/' + path2;
}
/// Urlencode a string
std::string urlencode(const std::string& str);
/// Decode an urlencoded string
std::string urldecode(const std::string& str);
/// Encode a string in Base64
std::string encodeBase64(const std::string& str);
/// Decode a string encoded in Base64
std::string decodeBase64(const std::string& str);
/**
* Split a string where a given substring is found
*
* This does a similar work to the split functions of perl, python and ruby.
*
* Example code:
* \code
* str::Split splitter("/", myString);
* vector<string> split;
* std::copy(splitter.begin(), splitter.end(), back_inserter(split));
* \endcode
*/
class Split
{
std::string sep;
std::string str;
public:
// TODO: add iterator_traits
class const_iterator
{
const std::string& sep;
const std::string& str;
std::string cur;
size_t pos;
public:
const_iterator(const std::string& sep, const std::string& str) : sep(sep), str(str), pos(0)
{
++*this;
}
const_iterator(const std::string& sep, const std::string& str, bool) : sep(sep), str(str), pos(std::string::npos) {}
const_iterator& operator++()
{
if (pos == str.size())
pos = std::string::npos;
else
{
size_t end;
if (sep.empty())
if (pos + 1 == str.size())
end = std::string::npos;
else
end = pos + 1;
else
end = str.find(sep, pos);
if (end == std::string::npos)
{
cur = str.substr(pos);
pos = str.size();
}
else
{
cur = str.substr(pos, end-pos);
pos = end + sep.size();
}
}
return *this;
}
std::string remainder() const
{
if (pos == std::string::npos)
return std::string();
else
return str.substr(pos);
}
const std::string& operator*() const
{
return cur;
}
const std::string* operator->() const
{
return &cur;
}
bool operator==(const const_iterator& ti) const
{
// Comparing iterators on different strings is not supported for
// performance reasons
return pos == ti.pos;
}
bool operator!=(const const_iterator& ti) const
{
// Comparing iterators on different strings is not supported for
// performance reasons
return pos != ti.pos;
}
};
/**
* Create a splitter that uses the given regular expression to find tokens.
*/
Split(const std::string& sep, const std::string& str) : sep(sep), str(str) {}
/**
* Split the string and iterate the resulting tokens
*/
const_iterator begin() const { return const_iterator(sep, str); }
const_iterator end() const { return const_iterator(sep, str, false); }
};
template<typename ITER>
std::string join(const ITER& begin, const ITER& end, const std::string& sep = ", ")
{
std::stringstream res;
bool first = true;
for (ITER i = begin; i != end; ++i)
{
if (first)
first = false;
else
res << sep;
res << *i;
}
return res.str();
}
/**
* Parse a record of Yaml-style field: value couples.
*
* Parsing stops either at end of record (one empty line) or at end of file.
*
* The value is deindented properly.
*
* Example code:
* \code
* utils::YamlStream stream;
* map<string, string> record;
* std::copy(stream.begin(inputstream), stream.end(), inserter(record));
* \endcode
*/
class YamlStream
{
public:
// TODO: add iterator_traits
class const_iterator
{
std::istream* in;
std::pair<std::string, std::string> value;
std::string line;
public:
const_iterator(std::istream& in);
const_iterator() : in(0) {}
const_iterator& operator++();
const std::pair<std::string, std::string>& operator*() const
{
return value;
}
const std::pair<std::string, std::string>* operator->() const
{
return &value;
}
bool operator==(const const_iterator& ti) const
{
return in == ti.in;
}
bool operator!=(const const_iterator& ti) const
{
return in != ti.in;
}
};
const_iterator begin(std::istream& in) { return const_iterator(in); }
const_iterator end() { return const_iterator(); }
};
}
}
// vim:set ts=4 sw=4:
#endif
|