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-------------------------------------------------------------------------------
11License
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
36const 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
58bool 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
81bool 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,
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,
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,
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
173Foam::string& Foam::string::expand(const bool allowEmpty)
174{
175 stringOps::inplaceExpand(*this, allowEmpty);
176 return *this;
177}
178
179
180bool 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
214bool 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
229bool 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
245{
246 if (length() > 1 && front() == c)
247 {
248 erase(0, 1);
249 return true;
250 }
251
252 return false;
253}
254
255
256bool 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// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:75
label n
label size_type
The type to represent the size of a buffer.
label count() const
A class for handling character strings derived from std::string.
Definition: string.H:79
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
Definition: string.C:214
static const string null
An empty string.
Definition: string.H:169
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition: stringI.H:44
string & replaceAll(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:124
bool removeRepeated(const char character)
Remove repeated characters.
Definition: string.C:180
string & expand(const bool allowEmpty=false)
Definition: string.C:173
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
string & replaceAny(const std::string &s1, const char c2, size_type pos=0)
Definition: string.C:147
static int debug
The debug flag.
Definition: string.H:166
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:108
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
static const char *const typeName
The type name "string".
Definition: string.H:163
bool removeEnd(const std::string &text)
Remove the given text from the end of the string.
Definition: string.C:229
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
bool isLiteral() const noexcept
The wordRe is a literal string, not a pattern.
Definition: wordReI.H:105
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:200
A class for handling words, derived from Foam::string.
Definition: word.H:68
static const word null
An empty word.
Definition: word.H:80
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:225
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
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
dimensionedScalar pos(const dimensionedScalar &ds)
srcOptions erase("case")