string.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "string.H"
27 #include "stringOps.H"
28 #include "UList.H"
29 
30 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
31 
32 const char* const Foam::string::typeName = "string";
33 int Foam::string::debug(Foam::debug::debugSwitch(string::typeName, 0));
35 
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
40 :
41  std::string(str.begin(), str.end())
42 {}
43 
44 
45 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
46 
48 {
49  size_type cCount = 0;
50 
51  for (const_iterator iter = begin(); iter != end(); ++iter)
52  {
53  if (*iter == c)
54  {
55  ++cCount;
56  }
57  }
58 
59  return cCount;
60 }
61 
62 
64 (
65  const string& oldStr,
66  const string& newStr,
67  size_type start
68 )
69 {
70  size_type newStart = start;
71 
72  if ((newStart = find(oldStr, newStart)) != npos)
73  {
74  std::string::replace(newStart, oldStr.size(), newStr);
75  }
76 
77  return *this;
78 }
79 
80 
82 (
83  const string& oldStr,
84  const string& newStr,
85  size_type start
86 ) const
87 {
88  return string(*this).replace(oldStr, newStr, start);
89 }
90 
91 
93 (
94  const string& oldStr,
95  const string& newStr,
96  size_type start
97 )
98 {
99  if (oldStr.size())
100  {
101  size_type newStart = start;
102 
103  while ((newStart = find(oldStr, newStart)) != npos)
104  {
105  std::string::replace(newStart, oldStr.size(), newStr);
106  newStart += newStr.size();
107  }
108  }
109 
110  return *this;
111 }
112 
113 
115 (
116  const string& oldStr,
117  const string& newStr,
118  size_type start
119 ) const
120 {
121  return string(*this).replaceAll(oldStr, newStr, start);
122 }
123 
124 
125 Foam::string& Foam::string::expand(const bool allowEmpty)
126 {
127  stringOps::inplaceExpandEnvVar(*this, allowEmpty);
128  return *this;
129 }
130 
131 
132 bool Foam::string::remove(const char character)
133 {
134  bool changed = false;
135 
136  string::size_type n = 0;
137  iterator iter2 = begin();
138 
139  for
140  (
141  string::const_iterator iter1 = iter2;
142  iter1 != end();
143  ++iter1
144  )
145  {
146  char c = *iter1;
147 
148  if (c == character)
149  {
150  changed = true;
151  }
152  else
153  {
154  *iter2 = c;
155  ++iter2;
156  ++n;
157  }
158  }
159 
160  resize(n);
161 
162  return changed;
163 }
164 
165 
166 Foam::string Foam::string::remove(const char character) const
167 {
168  string str(*this);
169  str.remove(character);
170  return str;
171 }
172 
173 
174 
175 bool Foam::string::removeRepeated(const char character)
176 {
177  bool changed = false;
178 
179  string::size_type n = 0;
180  iterator iter2 = begin();
181 
182  char cPrev = operator[](0) + 1;
183 
184  for
185  (
186  string::const_iterator iter1 = iter2;
187  iter1 != end();
188  ++iter1
189  )
190  {
191  char c = *iter1;
192 
193  if (c == cPrev && c == character)
194  {
195  changed = true;
196  }
197  else
198  {
199  *iter2 = cPrev = c;
200  ++iter2;
201  ++n;
202  }
203  }
204 
205  resize(n);
206 
207  return changed;
208 }
209 
210 
211 Foam::string Foam::string::removeRepeated(const char character) const
212 {
213  string str(*this);
214  str.removeRepeated(character);
215  return str;
216 }
217 
218 
219 bool Foam::string::removeTrailing(const char character)
220 {
221  bool changed = false;
222 
223  string::size_type n = size();
224  if (n >= 1 && operator[](n - 1) == character)
225  {
226  resize(n - 1);
227  changed = true;
228  }
229 
230  return changed;
231 }
232 
233 
234 Foam::string Foam::string::removeTrailing(const char character) const
235 {
236  string result(*this);
237  result.removeTrailing(character);
238  return result;
239 }
240 
241 
242 bool Foam::string::removeTrailing(const string& str)
243 {
244  bool changed = false;
245 
246  string::size_type n = size(), nStr = str.size();
247  if (n >= str.size() && operator()(n - nStr, nStr) == str)
248  {
249  resize(n - nStr);
250  changed = true;
251  }
252 
253  return changed;
254 }
255 
256 
258 {
259  string result(*this);
260  result.removeTrailing(str);
261  return result;
262 }
263 
264 
265 void Foam::string::strip(const string& str)
266 {
267  // Find the first character to keep
268  string::size_type i0 = 0;
269  while (i0 < size() && str.count(operator[](i0)) > 0)
270  {
271  ++ i0;
272  }
273 
274  // Find one past the last character to keep
275  string::size_type i1 = size();
276  while (i1 > i0 && str.count(operator[](i1 - 1)) > 0)
277  {
278  -- i1;
279  }
280 
281  // Remove leading characters by shuffling the string up
282  if (i0 != 0)
283  {
284  for (string::size_type i = 0; i < size() - i0; ++ i)
285  {
286  operator[](i) = operator[](i + i0);
287  }
288  }
289 
290  // If removing any characters then resize the string
291  if (i0 != 0 || i1 != size())
292  {
293  resize(i1 - i0);
294  }
295 }
296 
297 
299 (
300  const char c,
301  string::size_type i0 = 0
302 ) const
303 {
304  size_t level = 1;
305 
306  string::size_type i = i0 + 1;
307 
308  while (level > 0 && i < size())
309  {
310  if (operator[](i) == operator[](i0)) ++level;
311  if (operator[](i) == c) --level;
312 
313  ++i;
314  }
315 
316  return level == 0 ? i - 1 : string::npos;
317 }
318 
319 
320 // ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
label n
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
A class for handling character strings derived from std::string.
Definition: string.H:79
string & replaceAll(const string &oldStr, const string &newStr, size_type start=0)
In this string replace all occurrences of sub-string oldStr.
Definition: string.C:93
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:47
static const string null
An empty string.
Definition: string.H:88
bool removeTrailing(const char)
Remove trailing character returning true if string changed.
Definition: string.C:219
size_type findClosing(const char, const size_type) const
Find the closing character. Brackets counting algorithm. The.
Definition: string.C:299
void strip(const string &)
Strip characters from the start and end of the string.
Definition: string.C:265
string & replace(const string &oldStr, const string &newStr, size_type start=0)
In this string replace first occurrence of sub-string oldStr.
Definition: string.C:64
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurrences of environment variables.
Definition: string.C:125
bool remove(const char)
Remove all occurrences of character returning true if string changed.
Definition: string.C:132
static int debug
Definition: string.H:85
string()
Construct null.
Definition: stringI.H:30
bool removeRepeated(const char)
Remove repeated character returning true if string changed.
Definition: string.C:175
static const char *const typeName
Definition: string.H:84
const dimensionedScalar c
Speed of light in a vacuum.
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:211
string & inplaceExpandEnvVar(string &, const bool allowEmpty=false)
Expand all occurrences of environment variables and paths.
Definition: stringOps.C:252
triSurfaceToAgglom resize(surfacesMesh.size())