Util--  1.1
stringtools.hh
Go to the documentation of this file.
1 #ifndef UTILMM_STRINGTOOLS_HH
2 #define UTILMM_STRINGTOOLS_HH
3 
4 #include <string>
5 #include <list>
6 #include <ctype.h>
7 
8 #include <boost/filesystem/path.hpp>
9 #include <boost/lexical_cast.hpp>
10 
11 namespace utilmm {
12  typedef std::list<std::string> stringlist;
13 
14  inline stringlist split(std::string const& s, std::string const& sep = " ", bool ignore_empty = true)
15  {
16  using std::string;
17 
18  stringlist l;
19  string::size_type sep_length = sep.length();
20 
21  string::size_type from = 0;
22  for(;;)
23  {
24  string::size_type to = s.find(sep, from);
25  if (to == string::npos)
26  {
27  if (from < s.length() || !ignore_empty)
28  l.push_back(string(s, from));
29  return l;
30  }
31 
32  if (to > from || !ignore_empty)
33  l.push_back(string(s, from, to - from));
34  from = to + sep_length;
35  }
36  }
37 
38  inline std::string join(stringlist const& l, std::string const& sep = " ")
39  {
40  using std::string;
41 
42  if (l.empty())
43  return "";
44 
45  string s = l.front();
46 
47  stringlist::const_iterator const end = l.end();
48  stringlist::const_iterator it = l.begin();
49  for (++it; it != l.end(); ++it)
50  s += sep + *it;
51  return s;
52  }
53 
54  template<typename T>
55  inline std::string join(std::list<T> const& l, std::string const& sep = " ")
56  {
57  stringlist strl;
58  for (typename std::template list<T>::const_iterator it = l.begin(); it != l.end(); ++it)
59  strl.push_back(boost::lexical_cast<std::string>(*it));
60  return join(strl, sep);
61  }
62 
63  inline std::string upcase(std::string const& s)
64  {
65  using std::string;
66  string ret(s);
67  string::iterator it, end = ret.end();
68  for (it = ret.begin(); it != ret.end(); ++it)
69  *it = toupper(*it);
70 
71  return ret;
72  }
73 
74  inline bool starts_with(std::string const& str, std::string const& start)
75  { return std::string(str, 0, start.length()) == start; }
76 
77  inline boost::filesystem::path clean_path(std::string str)
78  {
79  using std::string;
80  string::size_type i = str.find("//");
81  while (i < str.length())
82  {
83  str = str.erase(i, 1);
84  i = str.find("//");
85  }
86 
87  boost::filesystem::path p(str);
88  return p.normalize();
89  }
90 }
91 #endif
92 
bool starts_with(std::string const &str, std::string const &start)
Definition: stringtools.hh:74
std::list< std::string > stringlist
Definition: stringtools.hh:12
boost::filesystem::path clean_path(std::string str)
Definition: stringtools.hh:77
std::string upcase(std::string const &s)
Definition: stringtools.hh:63
std::string join(stringlist const &l, std::string const &sep=" ")
Definition: stringtools.hh:38
stringlist split(std::string const &s, std::string const &sep=" ", bool ignore_empty=true)
Definition: stringtools.hh:14
Definition: auto_flag.hh:6

Generated on Mon Oct 22 2018 18:39:28 for Util-- by doxygen 1.8.13
SourceForge.net Project Page