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-2019 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 
159 Foam::string& Foam::string::expand(const bool allowEmpty)
160 {
161  stringOps::inplaceExpand(*this, allowEmpty);
162  return *this;
163 }
164 
165 
166 bool Foam::string::removeRepeated(const char character)
167 {
168  bool changed = false;
169 
170  if (character && find(character) != npos)
171  {
172  string::size_type nChar = 0;
173  iterator outIter = begin();
174 
175  char prev = 0;
176 
177  for (auto iter = cbegin(); iter != cend(); ++iter)
178  {
179  const char c = *iter;
180 
181  if (prev == c && c == character)
182  {
183  changed = true;
184  }
185  else
186  {
187  *outIter = prev = c;
188  ++outIter;
189  ++nChar;
190  }
191  }
192 
193  resize(nChar);
194  }
195 
196  return changed;
197 }
198 
199 
200 bool Foam::string::removeStart(const std::string& text)
201 {
202  const auto txtLen = text.length();
203  const auto strLen = length();
204 
205  if (txtLen && strLen >= txtLen && !compare(0, txtLen, text))
206  {
207  erase(0, txtLen);
208  return true;
209  }
210 
211  return false;
212 }
213 
214 
215 bool Foam::string::removeEnd(const std::string& text)
216 {
217  const auto txtLen = text.length();
218  const auto strLen = length();
219 
220  if (txtLen && strLen >= txtLen && !compare(strLen - txtLen, npos, text))
221  {
222  resize(strLen - txtLen);
223  return true;
224  }
225 
226  return false;
227 }
228 
229 
230 bool Foam::string::removeStart(const char c)
231 {
232  if (length() > 1 && front() == c)
233  {
234  erase(0, 1);
235  return true;
236  }
237 
238  return false;
239 }
240 
241 
242 bool Foam::string::removeEnd(const char c)
243 {
244  const auto n = length();
245  if (n > 1 && back() == c)
246  {
247  resize(n-1);
248  return true;
249  }
250 
251  return false;
252 }
253 
254 
255 // ************************************************************************* //
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:91
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
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
Foam::stringOps::count
std::string::size_type count(const std::string &str, const char c)
Count the number of occurrences of the specified character.
Definition: stringOps.C:698
n
label n
Definition: TABSMDCalcMethod2.H:31
resize
patchWriters resize(patchIds.size())
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:752
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:75
Foam::string::removeRepeated
bool removeRepeated(const char character)
Remove repeated characters.
Definition: string.C:166
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:115
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
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:131
stdFoam::cbegin
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:107
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:159
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:200
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
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:215
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