string.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "string.H"
30 #include "stringOps.H"
31 #include "word.H"
32 #include "wordRe.H"
33 
34 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
35 
36 const char* const Foam::string::typeName = "string";
37 
39 
41 
42 
43 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
44 
46 {
47  const auto i = find_ext();
48 
49  if (i == npos)
50  {
51  return word::null;
52  }
53 
54  return substr(i+1);
55 }
56 
57 
58 bool Foam::string::ext(const word& ending)
59 {
60  if (ending.empty() || empty() || back() == '/')
61  {
62  return false;
63  }
64  else if (ending[0] == '.')
65  {
66  if (ending.size() == 1)
67  {
68  return false;
69  }
70  }
71  else
72  {
73  append(1u, '.');
74  }
75  append(ending);
76 
77  return true;
78 }
79 
80 
81 bool Foam::string::hasExt(const word& ending) const
82 {
83  auto i = find_ext();
84  if (i == npos)
85  {
86  return false;
87  }
88 
89  ++i; // Compare *after* the dot
90  return
91  (
92  // Lengths must match
93  ((size() - i) == ending.size())
94  && !compare(i, npos, ending)
95  );
96 }
97 
98 
99 bool Foam::string::hasExt(const wordRe& ending) const
100 {
101  const auto i = find_ext();
102  if (i == npos)
103  {
104  return false;
105  }
106 
107  const std::string end = substr(i+1); // Compare *after* the dot
108  return ending.match(end);
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
115 {
116  return stringOps::count(*this, c);
117 }
118 
119 
121 (
122  const std::string& s1,
123  const std::string& s2,
124  size_type pos
125 )
126 {
127  if ((pos = find(s1, pos)) != npos)
128  {
129  std::string::replace(pos, s1.size(), s2);
130  }
131 
132  return *this;
133 }
134 
135 
137 (
138  const std::string& s1,
139  const std::string& s2,
140  size_type pos
141 )
142 {
143  const auto n1 = s1.length();
144  const auto n2 = s2.length();
145 
146  if (n1)
147  {
148  while ((pos = find(s1, pos)) != npos)
149  {
150  std::string::replace(pos, n1, s2);
151  pos += n2;
152  }
153  }
154 
155  return *this;
156 }
157 
158 
160 (
161  const std::string& s1,
162  const char c2,
163  size_type pos
164 )
165 {
166  if (s1.length())
167  {
168  while ((pos = find_first_of(s1, pos)) != npos)
169  {
170  if (c2)
171  {
172  operator[](pos) = c2;
173  ++pos;
174  }
175  else
176  {
177  erase(pos, 1);
178  }
179  }
180  }
181 
182  return *this;
183 }
184 
185 
186 Foam::string& Foam::string::expand(const bool allowEmpty)
187 {
188  stringOps::inplaceExpand(*this, allowEmpty);
189  return *this;
190 }
191 
192 
193 bool Foam::string::removeRepeated(const char character)
194 {
195  bool changed = false;
196 
197  if (character && find(character) != npos)
198  {
199  string::size_type nChar = 0;
200  iterator outIter = begin();
201 
202  char prev = 0;
203 
204  for (auto iter = cbegin(); iter != cend(); ++iter)
205  {
206  const char c = *iter;
207 
208  if (prev == c && c == character)
209  {
210  changed = true;
211  }
212  else
213  {
214  *outIter = prev = c;
215  ++outIter;
216  ++nChar;
217  }
218  }
219 
220  erase(nChar);
221  }
222 
223  return changed;
224 }
225 
226 
227 bool Foam::string::removeStart(const std::string& text)
228 {
229  const auto txtLen = text.length();
230  const auto strLen = length();
231 
232  if (txtLen && strLen >= txtLen && !compare(0, txtLen, text))
233  {
234  erase(0, txtLen);
235  return true;
236  }
237 
238  return false;
239 }
240 
241 
242 bool Foam::string::removeEnd(const std::string& text)
243 {
244  const auto txtLen = text.length();
245  const auto strLen = length();
246 
247  if (txtLen && strLen >= txtLen && !compare(strLen - txtLen, npos, text))
248  {
249  erase(strLen - txtLen);
250  return true;
251  }
252 
253  return false;
254 }
255 
256 
257 bool Foam::string::removeStart(const char c)
258 {
259  if (length() > 1 && front() == c)
260  {
261  erase(0, 1);
262  return true;
263  }
264 
265  return false;
266 }
267 
268 
269 bool Foam::string::removeEnd(const char c)
270 {
271  const auto n = length();
272  if (n > 1 && back() == c)
273  {
274  erase(n-1);
275  return true;
276  }
277 
278  return false;
279 }
280 
281 
282 // ************************************************************************* //
Foam::string::replace
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:121
Foam::string::find_ext
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition: stringI.H:44
Foam::string::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::debug::debugSwitch
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:225
string.H
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
erase
srcOptions erase("case")
Foam::string::typeName
static const char *const typeName
The type name "string".
Definition: string.H:141
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::string::debug
static int debug
The debug flag.
Definition: string.H:144
n
label n
Definition: TABSMDCalcMethod2.H:31
wordRe.H
Foam::string::ext
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Definition: stringOps.C:733
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::string::replaceAny
string & replaceAny(const std::string &s1, const char c2, size_type pos=0)
Definition: string.C:160
Foam::string::removeRepeated
bool removeRepeated(const char character)
Remove repeated characters.
Definition: string.C:193
Foam::string::null
static const string null
An empty string.
Definition: string.H:147
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::string::replaceAll
string & replaceAll(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:137
Foam::constant::physicoChemical::c2
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:137
stdFoam::cbegin
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:113
Foam::string::count
size_type count(const char c) const
Definition: string.C:114
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:186
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::string::removeStart
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
Definition: string.C:227
Foam::stringOps::count
std::string::size_type count(const std::string &s, const char c)
Count the number of occurrences of the specified character.
Definition: stringOps.C:699
word.H
stringOps.H
Foam::string::removeEnd
bool removeEnd(const std::string &text)
Remove the given text from the end of the string.
Definition: string.C:242
Foam::wordRe::match
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:229
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177