string.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-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
27Class
28 Foam::string
29
30Description
31 A class for handling character strings derived from std::string.
32
33 Strings may contain any characters and therefore are delimited by quotes
34 for IO : "any list of characters".
35
36 Used as a base class for word and fileName.
37
38See also
39 Foam::findEtcFile() for information about the site/user OpenFOAM
40 configuration directory
41
42SourceFiles
43 stringI.H
44 string.C
45 stringIO.C
46 stringTemplates.C
47
48\*---------------------------------------------------------------------------*/
49
50#ifndef string_H
51#define string_H
52
53#include "char.H"
54#include "Hasher.H"
55#include <cstdlib>
56#include <cstring>
57#include <string>
58
59// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60
61namespace Foam
62{
63
64// Forward Declarations
65class string;
66class word;
67class wordRe;
68class Istream;
69class Ostream;
70
71template<class T> struct Hash;
72
73/*---------------------------------------------------------------------------*\
74 Class string Declaration
75\*---------------------------------------------------------------------------*/
77class string
78:
79 public std::string
80{
81protected:
82
83 // Protected Member Functions
84
85 //- Find position of a file extension dot, return npos on failure.
86 // A wrapped version of find_last_of("./") with additional logic.
87 inline static std::string::size_type find_ext(const std::string& str);
88
89 //- Find position of a file extension dot, return npos on failure.
90 // A wrapped version of find_last_of("./") with additional logic.
91 inline std::string::size_type find_ext() const;
92
93 //- A printf-style formatter for a primitive.
94 template<class PrimitiveType>
95 static std::string::size_type string_printf
96 (
97 std::string& output,
98 const char* fmt,
99 const PrimitiveType& val
100 );
101
102 //- A printf-style formatter for a primitive.
103 template<class PrimitiveType>
104 static std::string::size_type string_printf
105 (
106 std::string& output,
107 const std::string& fmt,
108 const PrimitiveType& val
109 );
110
111 //- Return file name extension (part after last .)
112 word ext() const;
113
114 //- Append a '.' and the ending.
115 // The '.' and ending will not be added when the ending is empty,
116 // or when the object was or ended with a '/'.
117 //
118 // \return True if append occurred.
119 bool ext(const word& ending);
120
121 //- Return true if it contains a '/' character
122 inline bool hasPath() const;
123
124 //- Return true if it has an extension or simply ends with a '.'
125 inline bool hasExt() const;
126
127 //- Return true if the extension is the same as the given ending.
128 inline bool hasExt(const char* ending) const;
129
130 //- Return true if the extension is the same as the given ending.
131 inline bool hasExt(const std::string& ending) const;
132
133 //- Return true if the extension matches the given ending.
134 bool hasExt(const wordRe& ending) const;
135
136 //- Remove extension, returning true if string changed.
137 inline bool removeExt();
138
139 //- Remove leading path, returning true if string changed.
140 inline bool removePath();
141
142
143public:
144
145 // Public Classes
146
147 //- Hashing functor for string and derived string classes
148 struct hasher
150 unsigned operator()(const std::string& str, unsigned seed=0) const
151 {
152 return Foam::Hasher(str.data(), str.length(), seed);
153 }
154 };
155
156 //- Deprecated hashing functor - use hasher
157 // \deprecated(2021-04) - use hasher
158 struct hash : string::hasher {};
159
160
161 // Static Data Members
162
163 //- The type name "string"
164 static const char* const typeName;
165
166 //- The debug flag
167 static int debug;
168
169 //- An empty string
170 static const string null;
171
172
173 // Constructors
174
175 //- Default construct
176 string() = default;
177
178 //- Copy construct from std::string
179 inline string(const std::string& str);
180
181 //- Move construct from std::string
182 inline string(std::string&& str);
183
184 //- Construct as copy of character array
185 inline string(const char* str);
186
187 //- Construct as copy with a maximum number of characters
188 inline string(const char* str, const size_type len);
189
190 //- Construct from a single character
191 inline explicit string(const char c);
192
193 //- Construct fill copies of a single character
194 inline string(const size_type len, const char c);
195
196 //- Construct from Istream
197 explicit string(Istream& is);
198
199
200 // Static Member Functions
201
202 //- Does the string contain valid characters only?
203 template<class StringType>
204 static inline bool valid(const std::string& str);
205
206 //- Strip invalid characters from the given string
207 template<class StringType>
208 static inline bool stripInvalid(std::string& str);
209
210 //- Return a valid String from the given string
211 template<class StringType>
212 static inline StringType validate(const std::string& str);
213
214
215 // Member Functions
216
217 //- Test for equality.
218 // \return True when strings match literally.
219 inline bool match(const std::string& text) const;
220
221 //- Avoid masking the normal std::string replace
222 using std::string::replace;
223
224 //- Replace first occurrence of sub-string s1 with s2,
225 //- beginning at pos
226 string& replace
227 (
228 const std::string& s1,
229 const std::string& s2,
230 size_type pos = 0
231 );
232
233 //- Replace all occurrences of sub-string s1 with s2,
234 //- beginning at pos in the string.
235 // A no-op if s1 is empty.
236 string& replaceAll
237 (
238 const std::string& s1,
239 const std::string& s2,
240 size_type pos = 0
241 );
242
243 //- Replace any occurrence of s1 characters with c2,
244 //- beginning at pos in the string.
245 // A no-op if s1 is empty.
246 string& replaceAny
247 (
248 const std::string& s1,
249 const char c2,
250 size_type pos = 0
251 );
252
253 //- Inplace expand initial tags, tildes, and all occurrences of
254 //- environment variables as per stringOps::expand
255 //
256 // Any unknown entries are removed silently if allowEmpty is true
257 // \sa
258 // Foam::findEtcFile
259 string& expand(const bool allowEmpty = false);
260
261 //- Remove repeated characters
262 // \return True if string changed
263 bool removeRepeated(const char character);
264
265 //- Remove the given text from the start of the string.
266 // \return True if the removal occurred
267 bool removeStart(const std::string& text);
268
269 //- Remove leading character, unless string is a single character
270 // \return True if the removal occurred
271 bool removeStart(const char c);
272
273 //- Remove the given text from the end of the string.
274 // \return True if the removal occurred
275 bool removeEnd(const std::string& text);
276
277 //- Remove trailing character, unless string is a single character
278 // \return True if the removal occurred
279 bool removeEnd(const char c);
280
281
282 // Editing
283
284 //- Swap contents. Self-swapping is a no-op.
285 inline void swap(std::string& str);
286
287
288 // Member Operators
289
290 //- Test for equality. Allows use as a predicate.
291 // \return True when strings match literally.
292 inline bool operator()(const std::string& text) const;
293
294
295 // Housekeeping
296
297 //- True if string starts with the given prefix (cf. C++20)
298 bool starts_with(const std::string& s) const
299 {
300 return (size() >= s.size() && !compare(0, s.size(), s));
301 }
302
303 //- True if string starts with the given character (cf. C++20)
304 bool starts_with(const char c) const
305 {
306 return (!empty() && front() == c);
307 }
308
309 //- True if string ends with the given suffix (cf. C++20)
310 bool ends_with(const std::string& s) const
311 {
312 return (size() >= s.size() && !compare(size()-s.size(), npos, s));
313 }
314
315 //- True if string ends with the given character (cf. C++20)
316 bool ends_with(const char c) const
317 {
318 return (!empty() && back() == c);
319 }
320
321 //- Count the number of occurrences of the specified character
322 //- in the string
323 // Partially deprecated (NOV-2017) in favour of stringOps::count
324 size_type count(const char c) const;
325
326 //- Deprecated(2019-11)
327 // \deprecated(2019-11) use starts_with instead
328 bool startsWith(const std::string& s) const { return starts_with(s); }
329
330 //- Deprecated(2019-11)
331 // \deprecated(2019-11) use ends_with instead
332 bool endsWith(const std::string& s) const { return ends_with(s); }
333
334 //- Deprecated(2019-11)
335 // \deprecated(2019-11) use removeEnd instead
336 bool removeTrailing(const char c) { return removeEnd(c); }
337};
338
339
340// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
341
342//- Hashing for Foam::string
343template<> struct Hash<string> : string::hasher {};
344
345//- Hashing for std:::string
346template<> struct Hash<std::string> : string::hasher {};
347
348
349// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350
351// IOstream Operators
352
353//- Read operator
354Istream& operator>>(Istream& is, string& val);
355
356//- Write operator
357Ostream& operator<<(Ostream& os, const string& val);
358
359//- Write operator
360Ostream& operator<<(Ostream& os, const std::string& val);
361
362
363// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364
365} // End namespace Foam
366
367// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
368
369#include "stringI.H"
370
371#ifdef NoRepository
372 #include "stringTemplates.C"
373#endif
374
375// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
376
377#endif
378
379// ************************************************************************* //
Miscellaneous hashing functions, mostly from Bob Jenkins.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:75
A character and a pointer to a character string.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
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
bool startsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:327
bool endsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:331
bool ends_with(const std::string &s) const
True if string ends with the given suffix (cf. C++20)
Definition: string.H:309
static bool valid(const std::string &str)
Does the string contain valid characters only?
Definition: stringI.H:152
size_type count(const char c) const
Definition: string.C:101
string()=default
Default construct.
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition: stringI.H:167
bool starts_with(const char c) const
True if string starts with the given character (cf. C++20)
Definition: string.H:303
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 starts_with(const std::string &s) const
True if string starts with the given prefix (cf. C++20)
Definition: string.H:297
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition: stringI.H:196
bool removeTrailing(const char c)
Deprecated(2019-11)
Definition: string.H:335
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
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:96
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition: stringI.H:236
bool ends_with(const char c) const
True if string ends with the given character (cf. C++20)
Definition: string.H:315
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 std::string::size_type string_printf(std::string &output, const char *fmt, const PrimitiveType &val)
A printf-style formatter for a primitive.
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:224
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
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:581
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
Deprecated hashing functor - use hasher.
Definition: string.H:157
Hashing functor for string and derived string classes.
Definition: string.H:148
unsigned operator()(const std::string &str, unsigned seed=0) const
Definition: string.H:149