TF++, Miscellaneous classes and modules in C++:
stringfunc.cc
Go to the documentation of this file.
1 
36 #define TF_STRINGFUNC_CC_VERSION \
37  "TF_STRINGFUNC_CC V1.1"
38 
39 #include <tfxx/stringfunc.h>
40 
41 #include<algorithm>
42 #include<iterator>
43 #include<functional>
44 #include<ctype.h>
45 
46 namespace tfxx {
47 
48  namespace string {
49 
50  // remove leading and trailing whitespace
51  std::string trimws(std::string s)
52  {
53  if (s.length()>0)
54  {
55  std::string::size_type ib=s.find_first_not_of(" ", 0);
56  if (ib==std::string::npos)
57  {
58  s="";
59  }
60  else
61  {
62  std::string::size_type il=s.find_last_not_of(" \r", s.length());
63  std::string::size_type n=il>=ib ? il-ib+1 : 0;
64  if (n==0) { ib = 0; }
65  if ((ib!=0) || (n!=s.length())) { s=s.substr(ib,n); }
66  }
67  }
68  return(s);
69  } // std::string trimws(std::string s)
70 
71  /*----------------------------------------------------------------------*/
72 
73  // remove leading whitespace
74  std::string trimws_begin(std::string s)
75  {
76  if (s.length()>0)
77  {
78  std::string::size_type ib=s.find_first_not_of(" ", 0);
79  if (ib==std::string::npos)
80  {
81  s="";
82  }
83  else if (ib!=0)
84  {
85  std::string::size_type n=s.length()-ib;
86  s=s.substr(ib,n);
87  }
88  }
89  return(s);
90  } // std::string trimws_begin(std::string s)
91 
92  /*----------------------------------------------------------------------*/
93 
94  // remove trailing whitespace
95  // This is not as straight forward as trimws_begin as I did not manage to
96  // get string::erase compiled with reverse iteraters. On the other hand
97  // the only appropriate method to start a search at the end of the string
98  // is to use a reverse iterator. Doing reverse twice IS ugly as it means
99  // copying the string twice. Maybe I'll write some better code some
100  // time...
101  std::string trimws_end(std::string s)
102  {
103  if (s.length()>0)
104  {
105  std::string::size_type il=s.find_last_not_of(" \r", s.length());
106  std::string::size_type n=il>=0 ? il+1 : 0;
107  if (n!=s.length()) { s=s.substr(0,n); }
108  }
109  return(s);
110  } // std::string trimws_end(std::string s)
111 
112  /*----------------------------------------------------------------------*/
113 
120  std::string strip_string(std::string& s, const char& delim)
121  {
122  std::string::size_type i=s.find(delim);
123  std::string result;
124  if ((i>=0) && (i<s.size())) {
125  result=s.substr(0,i);
126  s.erase(0,i+1);
127  } else {
128  result=s;
129  s.erase();
130  }
131  return(result);
132  }
133 
134  /*----------------------------------------------------------------------*/
135 
142  int count_char(const std::string& s, const char& c)
143  {
144  int result=0;
145  for (std::string::const_iterator i=s.begin(); i!=s.end(); i++)
146  { if (*i == c) { result++; } }
147  return(result);
148  }
149 
150  /*----------------------------------------------------------------------*/
151 
160  std::string patsubst(const std::string& s,
161  const std::string& p,
162  const std::string& r)
163  {
164  std::string retval=s;
165  std::string::size_type n=retval.find(p);
166  while (n !=std::string::npos)
167  {
168  retval.replace(n,p.length(),r);
169  n=retval.find(p,n);
170  }
171  return(retval);
172  }
173 
174  } // namespace string
175 
176 } // namespace tfxx
177 
178 /* ----- END OF stringfunc.cc ----- */
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
std::string trimws_begin(std::string s)
Definition: stringfunc.cc:74
std::string trimws_end(std::string s)
Definition: stringfunc.cc:101
int count_char(const std::string &s, const char &c)
Definition: stringfunc.cc:142
Namespace containing all code of library libtfxx.