stringI.H
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-2015 OpenFOAM Foundation
9 Copyright (C) 2017-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// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30
31inline std::string::size_type Foam::string::find_ext(const std::string& str)
32{
33 const auto i = str.find_last_of("./");
34
35 if (i == npos || i == 0 || str[i] == '/')
36 {
37 return npos;
38 }
39
40 return i;
41}
42
43
44inline std::string::size_type Foam::string::find_ext() const
45{
46 return find_ext(*this);
47}
48
49
50inline bool Foam::string::hasPath() const
51{
52 return (npos != find('/'));
53}
54
55
56inline bool Foam::string::hasExt() const
57{
58 return (npos != find_ext());
59}
60
61
62inline bool Foam::string::hasExt(const char* ending) const
63{
64 return (ending && string::hasExt(std::string(ending)));
65}
66
67
68inline bool Foam::string::hasExt(const std::string& ending) const
69{
70 const auto len = ending.size();
71 auto i = find_ext();
72 if (i == npos || !len)
73 {
74 return false;
75 }
76
77 ++i; // Compare *after* the dot
78 return ((size() - i) == len) && !compare(i, npos, ending);
79}
80
81
83{
84 const auto i = rfind('/');
85
86 if (npos == i)
87 {
88 return false;
89 }
90
91 erase(0, i+1);
92 return true;
93}
94
95
97{
98 const auto i = find_ext();
99
100 if (npos == i)
101 {
102 return false;
103 }
104
105 erase(i);
106 return true;
107}
108
109
110
111// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
112
113inline Foam::string::string(const std::string& str)
114:
115 std::string(str)
116{}
117
118
119inline Foam::string::string(std::string&& str)
120:
121 std::string(std::move(str))
122{}
123
125inline Foam::string::string(const char* str)
126:
127 std::string(str)
128{}
129
131inline Foam::string::string(const char* str, const size_type len)
132:
133 std::string(str, len)
134{}
135
137inline Foam::string::string(const char c)
138:
139 std::string(1, c)
140{}
141
142
143inline Foam::string::string(const size_type len, const char c)
144:
145 std::string(len, c)
146{}
147
148
149// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
150
151template<class StringType>
152inline bool Foam::string::valid(const std::string& str)
153{
154 for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
155 {
156 if (!StringType::valid(*iter))
157 {
158 return false;
159 }
160 }
161
162 return true;
163}
164
165
166template<class StringType>
167inline bool Foam::string::stripInvalid(std::string& str)
168{
169 if (!string::valid<StringType>(str))
170 {
171 size_type nChar = 0;
172 iterator outIter = str.begin();
173
174 for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
175 {
176 const char c = *iter;
177
178 if (StringType::valid(c))
179 {
180 *outIter = c;
181 ++outIter;
182 ++nChar;
183 }
184 }
185
186 str.erase(nChar);
187
188 return true;
189 }
190
191 return false;
192}
193
194
195template<class StringType>
196inline StringType Foam::string::validate(const std::string& str)
197{
198 StringType out;
199 out.resize(str.size());
200
201 size_type len = 0;
202 for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
203 {
204 const char c = *iter;
205 if (StringType::valid(c))
206 {
207 out[len] = c;
208 ++len;
209 }
210 }
211
212 out.erase(len);
213
214 return out;
215}
216
217
218inline bool Foam::string::match(const std::string& text) const
219{
220 return !compare(text); // Always compare as literal string
221}
222
223
224inline void Foam::string::swap(std::string& str)
225{
226 if (this != &str)
227 {
228 // Self-swap is a no-op
229 std::string::swap(str);
230 }
231}
232
233
234// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
235
236inline bool Foam::string::operator()(const std::string& text) const
237{
238 return !compare(text); // Always compare as literal string
239}
240
241
242// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:75
bool valid() const
True if all internal ids are non-negative.
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
void stripInvalid()
Strip invalid characters.
virtual void validate()
Validate the turbulence fields after construction.
Definition: kkLOmega.C:597
A class for handling character strings derived from std::string.
Definition: string.H:79
string()=default
Default construct.
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition: stringI.H:44
bool removePath()
Remove leading path, returning true if string changed.
Definition: stringI.H:82
bool hasPath() const
Return true if it contains a '/' character.
Definition: stringI.H:50
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:218
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:96
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:224
srcOptions erase("case")