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-2021 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::inplaceExpand(*this, allowEmpty);
128  return *this;
129 }
130 
131 
132 bool Foam::string::removeRepeated(const char character)
133 {
134  bool changed = false;
135 
136  string::size_type n = 0;
137  iterator iter2 = begin();
138 
139  char cPrev = operator[](0) + 1;
140 
141  for
142  (
143  string::const_iterator iter1 = iter2;
144  iter1 != end();
145  ++ iter1
146  )
147  {
148  char c = *iter1;
149 
150  if (c == cPrev && c == character)
151  {
152  changed = true;
153  }
154  else
155  {
156  *iter2 = cPrev = c;
157  ++ iter2;
158  ++ n;
159  }
160  }
161 
162  resize(n);
163 
164  return changed;
165 }
166 
167 
168 Foam::string Foam::string::removeRepeated(const char character) const
169 {
170  string str(*this);
171  str.removeRepeated(character);
172  return str;
173 }
174 
175 
176 bool Foam::string::removeTrailing(const char character)
177 {
178  bool changed = false;
179 
180  string::size_type n = size();
181  if (n >= 1 && operator[](n - 1) == character)
182  {
183  resize(n - 1);
184  changed = true;
185  }
186 
187  return changed;
188 }
189 
190 
191 Foam::string Foam::string::removeTrailing(const char character) const
192 {
193  string result(*this);
194  result.removeTrailing(character);
195  return result;
196 }
197 
198 
199 bool Foam::string::removeTrailing(const string& str)
200 {
201  bool changed = false;
202 
203  string::size_type n = size(), nStr = str.size();
204  if (n >= str.size() && operator()(n - nStr, nStr) == str)
205  {
206  resize(n - nStr);
207  changed = true;
208  }
209 
210  return changed;
211 }
212 
213 
215 {
216  string result(*this);
217  result.removeTrailing(str);
218  return result;
219 }
220 
221 
222 void Foam::string::strip(const string& str)
223 {
224  // Find the first character to keep
225  string::size_type i0 = 0;
226  while (i0 < size() && str.count(operator[](i0)) > 0)
227  {
228  ++ i0;
229  }
230 
231  // Find one past the last character to keep
232  string::size_type i1 = size();
233  while (i1 > i0 && str.count(operator[](i1 - 1)) > 0)
234  {
235  -- i1;
236  }
237 
238  // Remove leading characters by shuffling the string up
239  if (i0 != 0)
240  {
241  for (string::size_type i = 0; i < size() - i0; ++ i)
242  {
243  operator[](i) = operator[](i + i0);
244  }
245  }
246 
247  // If removing any characters then resize the string
248  if (i0 != 0 || i1 != size())
249  {
250  resize(i1 - i0);
251  }
252 }
253 
254 
255 // ************************************************************************* //
bool removeRepeated(const char)
Remove repeated characters returning true if string changed.
Definition: string.C:132
void strip(const string &)
Strip characters from the start and end of the string.
Definition: string.C:222
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:47
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
string operator()(const size_type i, const size_type n) const
Return the sub-string from the i-th character for n characters.
Definition: stringI.H:204
const dimensionedScalar c
Speed of light in a vacuum.
static const char *const typeName
Definition: string.H:84
static int debug
Definition: string.H:85
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:211
triSurfaceToAgglom resize(surfacesMesh.size())
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
static const string null
An empty string.
Definition: string.H:88
string()
Construct null.
Definition: stringI.H:30
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 & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
Definition: stringOps.C:81
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurrences of environment variables.
Definition: string.C:125
label n
bool removeTrailing(const char)
Remove trailing character returning true if string changed.
Definition: string.C:176
A class for handling character strings derived from std::string.
Definition: string.H:76