TF++, Miscellaneous classes and modules in C++:
stringfunc.h
Go to the documentation of this file.
1 
40 // include guard
41 #ifndef TF_STRINGFUNC_H_VERSION
42 
43 #define TF_STRINGFUNC_H_VERSION \
44  "TF_STRINGFUNC_H V1.2"
45 
46 #include<string>
47 
48  namespace tfxx {
49 
50  namespace string {
51 
70  template<template<class> class C>
71  std::string join(const C<std::string>& v,
72  const std::string& delimiter="")
73  {
74  std::string retval;
75  for (typename C<std::string>::const_iterator i=v.begin();
76  i!=v.end(); i++)
77  { retval.append(*i); retval.append(delimiter); }
78  return(retval);
79  }
80 
107  /*
108  template<template<class> class C>
109  void gen_split(C<std::string>& v, const std::string& s,
110  */
111  template<class C>
112  void gen_split(C& v, const std::string& s,
113  const std::string& delimiter=" ",
114  const bool& dropdelimiter=false)
115  {
116  v.clear();
117  if (!s.empty()) {
118  std::string::size_type posend=0, pos=0;
119  std::string::size_type len=0;
120  while (posend!=std::string::npos) {
121  posend=s.find(delimiter, pos);
122  len=posend-pos;
123  if (dropdelimiter){
124  v.push_back(s.substr(pos, len));
125  if (posend!=std::string::npos) { len+=delimiter.length(); }
126  } else {
127  if (posend!=std::string::npos) { len+=delimiter.length(); }
128  v.push_back(s.substr(pos, len));
129  }
130  pos+=len;
131  }
132  }
133  }
134 
158  template<template<class> class C>
159  C<std::string> split(const std::string& s,
160  const std::string& delimiter=" ",
161  const bool& dropdelimiter=false)
162  {
163  C<std::string> v;
164  gen_split(v, s, delimiter, dropdelimiter);
165  return(v);
166  }
167 
179  std::string trimws_begin(std::string s);
180 
192  std::string trimws_end(std::string s);
193 
206  std::string trimws(std::string s);
207 
214  std::string strip_string(std::string& s, const char& delim=',');
215 
222  int count_char(const std::string& s, const char& c);
223 
232  std::string patsubst(const std::string& s,
233  const std::string& p,
234  const std::string& r);
235 
236  } // namespace string
237 
238  } // namesapce tfxx
239 
240 #endif // TF_STRINGFUNC_H_VERSION (includeguard)
241 
242 /* ----- END OF stringfunc.h ----- */
std::string trimws(std::string s)
Definition: stringfunc.cc:51
std::string strip_string(std::string &s, const char &delim)
Definition: stringfunc.cc:120
std::string patsubst(const std::string &s, const std::string &p, const std::string &r)
Definition: stringfunc.cc:160
void gen_split(C &v, const std::string &s, const std::string &delimiter=" ", const bool &dropdelimiter=false)
Definition: stringfunc.h:112
std::string trimws_begin(std::string s)
Definition: stringfunc.cc:74
std::string trimws_end(std::string s)
Definition: stringfunc.cc:101
std::string join(const C< std::string > &v, const std::string &delimiter="")
Definition: stringfunc.h:71
int count_char(const std::string &s, const char &c)
Definition: stringfunc.cc:142
C< std::string > split(const std::string &s, const std::string &delimiter=" ", const bool &dropdelimiter=false)
Definition: stringfunc.h:159
Namespace containing all code of library libtfxx.