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-2021 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 wordRe& ending) const
82 {
83  if (ending.isLiteral() || ending.empty())
84  {
85  return hasExt(static_cast<const std::string&>(ending));
86  }
87 
88  const auto i = find_ext();
89  if (i == npos)
90  {
91  return false;
92  }
93 
94  // Regex match - compare *after* the dot
95  return ending.match(substr(i+1));
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 {
103  return stringOps::count(*this, c);
104 }
105 
106 
108 (
109  const std::string& s1,
110  const std::string& s2,
111  size_type pos
112 )
113 {
114  if ((pos = find(s1, pos)) != npos)
115  {
116  std::string::replace(pos, s1.size(), s2);
117  }
118 
119  return *this;
120 }
121 
122 
124 (
125  const std::string& s1,
126  const std::string& s2,
127  size_type pos
128 )
129 {
130  const auto n1 = s1.length();
131  const auto n2 = s2.length();
132 
133  if (n1)
134  {
135  while ((pos = find(s1, pos)) != npos)
136  {
137  std::string::replace(pos, n1, s2);
138  pos += n2;
139  }
140  }
141 
142  return *this;
143 }
144 
145 
147 (
148  const std::string& s1,
149  const char c2,
150  size_type pos
151 )
152 {
153  if (s1.length())
154  {
155  while ((pos = find_first_of(s1, pos)) != npos)
156  {
157  if (c2)
158  {
159  operator[](pos) = c2;
160  ++pos;
161  }
162  else
163  {
164  erase(pos, 1);
165  }
166  }
167  }
168 
169  return *this;
170 }
171 
172 
173 Foam::string& Foam::string::expand(const bool allowEmpty)
174 {
175  stringOps::inplaceExpand(*this, allowEmpty);
176  return *this;
177 }
178 
179 
180 bool Foam::string::removeRepeated(const char character)
181 {
182  bool changed = false;
183 
184  if (character && find(character) != npos)
185  {
186  string::size_type nChar = 0;
187  iterator outIter = begin();
188 
189  char prev = 0;
190 
191  for (auto iter = cbegin(); iter != cend(); ++iter)
192  {
193  const char c = *iter;
194 
195  if (prev == c && c == character)
196  {
197  changed = true;
198  }
199  else
200  {
201  *outIter = prev = c;
202  ++outIter;
203  ++nChar;
204  }
205  }
206 
207  erase(nChar);
208  }
209 
210  return changed;
211 }
212 
213 
214 bool Foam::string::removeStart(const std::string& text)
215 {
216  const auto txtLen = text.length();
217  const auto strLen = length();
218 
219  if (txtLen && strLen >= txtLen && !compare(0, txtLen, text))
220  {
221  erase(0, txtLen);
222  return true;
223  }
224 
225  return false;
226 }
227 
228 
229 bool Foam::string::removeEnd(const std::string& text)
230 {
231  const auto txtLen = text.length();
232  const auto strLen = length();
233 
234  if (txtLen && strLen >= txtLen && !compare(strLen - txtLen, npos, text))
235  {
236  erase(strLen - txtLen);
237  return true;
238  }
239 
240  return false;
241 }
242 
243 
244 bool Foam::string::removeStart(const char c)
245 {
246  if (length() > 1 && front() == c)
247  {
248  erase(0, 1);
249  return true;
250  }
251 
252  return false;
253 }
254 
255 
256 bool Foam::string::removeEnd(const char c)
257 {
258  const auto n = length();
259  if (n > 1 && back() == c)
260  {
261  erase(n-1);
262  return true;
263  }
264 
265  return false;
266 }
267 
268 
269 // ************************************************************************* //
Foam::string::replace
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:108
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:65
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:76
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:163
Foam::wordRe::isLiteral
bool isLiteral() const noexcept
The wordRe is treated as literal string, not as pattern.
Definition: wordReI.H:101
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::string::debug
static int debug
The debug flag.
Definition: string.H:166
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
wordRe.H
Foam::string::ext
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
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:147
Foam::string::removeRepeated
bool removeRepeated(const char character)
Remove repeated characters.
Definition: string.C:180
Foam::string::null
static const string null
An empty string.
Definition: string.H:169
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:124
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:101
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:173
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
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:214
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:697
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:229
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:174
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177