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 )
87 {
88  if (oldStr.size())
89  {
90  size_type newStart = start;
91 
92  while ((newStart = find(oldStr, newStart)) != npos)
93  {
94  std::string::replace(newStart, oldStr.size(), newStr);
95  newStart += newStr.size();
96  }
97  }
98 
99  return *this;
100 }
101 
102 
103 Foam::string& Foam::string::expand(const bool allowEmpty)
104 {
105  stringOps::inplaceExpand(*this, allowEmpty);
106  return *this;
107 }
108 
109 
110 bool Foam::string::removeRepeated(const char character)
111 {
112  bool changed = false;
113 
114  string::size_type n = 0;
115  iterator iter2 = begin();
116 
117  char cPrev = operator[](0) + 1;
118 
119  for
120  (
121  string::const_iterator iter1 = iter2;
122  iter1 != end();
123  ++ iter1
124  )
125  {
126  char c = *iter1;
127 
128  if (c == cPrev && c == character)
129  {
130  changed = true;
131  }
132  else
133  {
134  *iter2 = cPrev = c;
135  ++ iter2;
136  ++ n;
137  }
138  }
139 
140  resize(n);
141 
142  return changed;
143 }
144 
145 
146 Foam::string Foam::string::removeRepeated(const char character) const
147 {
148  string str(*this);
149  str.removeRepeated(character);
150  return str;
151 }
152 
153 
154 bool Foam::string::removeTrailing(const char character)
155 {
156  bool changed = false;
157 
158  string::size_type n = size();
159  if (n >= 1 && operator[](n - 1) == character)
160  {
161  resize(n - 1);
162  changed = true;
163  }
164 
165  return changed;
166 }
167 
168 
169 Foam::string Foam::string::removeTrailing(const char character) const
170 {
171  string result(*this);
172  result.removeTrailing(character);
173  return result;
174 }
175 
176 
177 bool Foam::string::removeTrailing(const string& str)
178 {
179  bool changed = false;
180 
181  string::size_type n = size(), nStr = str.size();
182  if (n >= str.size() && operator()(n - nStr, nStr) == str)
183  {
184  resize(n - nStr);
185  changed = true;
186  }
187 
188  return changed;
189 }
190 
191 
193 {
194  string result(*this);
195  result.removeTrailing(str);
196  return result;
197 }
198 
199 
200 void Foam::string::strip(const string& str)
201 {
202  // Find the first character to keep
203  string::size_type i0 = 0;
204  while (i0 < size() && str.count(operator[](i0)) > 0)
205  {
206  ++ i0;
207  }
208 
209  // Find one past the last character to keep
210  string::size_type i1 = size();
211  while (i1 > i0 && str.count(operator[](i1 - 1)) > 0)
212  {
213  -- i1;
214  }
215 
216  // Remove leading characters by shuffling the string up
217  if (i0 != 0)
218  {
219  for (string::size_type i = 0; i < size() - i0; ++ i)
220  {
221  operator[](i) = operator[](i + i0);
222  }
223  }
224 
225  // If removing any characters then resize the string
226  if (i0 != 0 || i1 != size())
227  {
228  resize(i1 - i0);
229  }
230 }
231 
232 
233 // ************************************************************************* //
bool removeRepeated(const char)
Remove repeated characters returning true if string changed.
Definition: string.C:110
void strip(const string &)
Strip characters from the start and end of the string.
Definition: string.C:200
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)
Replace all occurrences of sub-string oldStr with newStr.
Definition: string.C:82
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)
Replace first occurrence of sub-string oldStr with newStr.
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:103
label n
bool removeTrailing(const char)
Remove trailing character returning true if string changed.
Definition: string.C:154
A class for handling character strings derived from std::string.
Definition: string.H:76