Loading...
Searching...
No Matches
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-2023 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 Foam_string_H
51#define Foam_string_H
52
53#include "char.H"
54#include "Hasher.H"
55#include <cstdlib>
56#include <cstring>
57#include <string_view>
58#include <string>
59
60// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61
62namespace Foam
63{
64
65// Forward Declarations
66class string;
67class word;
68class wordRe;
69class Istream;
70class Ostream;
71
72template<class T> struct Hash;
74/*---------------------------------------------------------------------------*\
75 Class string Declaration
76\*---------------------------------------------------------------------------*/
77
78class string
79:
80 public std::string
81{
82protected:
83
84 // Protected Member Functions
85
86 //- Find position of a file extension dot, return npos on failure.
87 // A wrapped version of find_last_of("./") with additional logic.
88 inline static std::string::size_type find_ext(const std::string& str);
89
90 //- Find position of a file extension dot, return npos on failure.
91 // A wrapped version of find_last_of("./") with additional logic.
92 inline std::string::size_type find_ext() const;
93
94 //- A printf-style formatter for a primitive.
95 template<class PrimitiveType>
96 static std::string::size_type string_printf
97 (
98 std::string& output,
99 const char* fmt,
100 const PrimitiveType& val
101 );
102
103 //- A printf-style formatter for a primitive.
104 template<class PrimitiveType>
105 static std::string::size_type string_printf
106 (
107 std::string& output,
108 const std::string& fmt,
109 const PrimitiveType& val
110 );
111
112 //- Return file name extension (part after last .)
113 word ext() const;
114
115 //- Append a '.' and the ending.
116 // The '.' and ending will not be added when the ending is empty,
117 // or when the object was or ended with a '/'.
118 //
119 // \return True if append occurred.
120 bool ext(const word& ending);
121
122 //- Return true if it has an extension or simply ends with a '.'
123 inline bool has_ext() const;
124
125 //- Return true if the extension is the same as the given ending.
126 // No proper nullptr protection.
127 inline bool has_ext(const char* ending) const;
128
129 //- Return true if the extension is the same as the given ending.
130 inline bool has_ext(const std::string& ending) const;
131
132 //- Return true if the extension matches the given ending.
133 bool has_ext(const wordRe& ending) const;
134
135 //- Remove leading path, return true if string changed.
136 inline bool remove_path();
137
138 //- Remove extension, return true if string changed.
139 inline bool remove_ext();
140
141
142public:
143
144 // Public Classes
145
146 //- Hashing functor for string and derived string classes
147 struct hasher
148 {
149 unsigned operator()(const std::string& str, unsigned seed=0) const
150 {
151 return Foam::Hasher(str.data(), str.length(), seed);
152 }
153 };
154
155 //- Deprecated hashing functor - use hasher
156 // \deprecated(2021-04) - use hasher
157 struct hash : string::hasher {};
158
159
160 // Static Data Members
161
162 //- The type name "string"
163 static const char* const typeName;
164
165 //- The debug flag
166 static int debug;
167
168 //- An empty string
169 static const string null;
170
171
172 // Constructors
173
174 //- Default construct
175 string() = default;
176
177 //- Copy construct from std::string
178 inline string(const std::string& str);
179
180 //- Move construct from std::string
181 inline string(std::string&& str);
182
183 //- Construct as copy of character array
184 inline string(const char* str);
186 //- Construct as copy with a maximum number of characters
187 inline string(const char* str, const size_type len);
188
189 //- Construct from a single character
190 inline explicit string(const char c);
191
192 //- Construct fill copies of a single character
193 inline string(const size_type len, const char c);
194
195 //- Construct from Istream
196 explicit string(Istream& is);
197
199 // Static Member Functions
200
201 //- Avoid masking the normal std::string length() method
202 using std::string::length;
204 //- Length of the character sequence (with nullptr protection)
205 static inline std::string::size_type length(const char* s)
206 {
207 return (s ? strlen(s) : 0);
208 }
209
210 //- The length of the string
211 static inline std::string::size_type length(const std::string& s)
212 {
213 return s.size();
214 }
215
216 //- Does the string contain valid characters only?
217 template<class StringType>
218 static inline bool valid(const std::string& str);
219
220 //- Strip invalid characters from the given string
221 template<class StringType>
222 static inline bool stripInvalid(std::string& str);
223
224 //- Return a valid String from the given string
225 template<class StringType>
226 static inline StringType validate(const std::string& str);
227
228
229 // Member Functions
230
231 //- Test for equality.
232 // \return True when strings match literally.
233 inline bool match(const std::string& text) const;
234
235 //- Avoid masking the normal std::string replace
236 using std::string::replace;
237
238 //- Replace first occurrence of sub-string s1 with s2,
239 //- beginning at pos
240 string& replace
241 (
242 const std::string& s1,
243 const std::string& s2,
244 size_type pos = 0
245 );
246
247 //- Replace all occurrences of sub-string s1 with s2,
248 //- beginning at pos in the string.
249 // A no-op if s1 is empty.
250 string& replaceAll
251 (
252 const std::string& s1,
253 const std::string& s2,
254 size_type pos = 0
255 );
256
257 //- Replace any occurrence of s1 characters with c2,
258 //- beginning at pos in the string.
259 // A no-op if s1 is empty.
260 string& replaceAny
261 (
262 const std::string& s1,
263 const char c2,
264 size_type pos = 0
265 );
266
267 //- Inplace expand initial tags, tildes, and all occurrences of
268 //- environment variables as per stringOps::expand
269 //
270 // Any unknown entries are removed silently if allowEmpty is true
271 // \sa
272 // Foam::findEtcFile
273 string& expand(const bool allowEmpty = false);
274
275 //- Remove repeated characters
276 // \return True if string changed
277 bool removeRepeated(const char character);
278
279 //- Remove the given text from the start of the string.
280 // \return True if the removal occurred
281 bool removeStart(const std::string& text);
282
283 //- Remove leading character, unless string is a single character
284 // \return True if the removal occurred
285 bool removeStart(const char c);
286
287 //- Remove the given text from the end of the string.
288 // \return True if the removal occurred
289 bool removeEnd(const std::string& text);
290
291 //- Remove trailing character, unless string is a single character
292 // \return True if the removal occurred
293 bool removeEnd(const char c);
294
295
296 // Editing
297
298 //- Swap contents. Self-swapping is a no-op.
299 inline void swap(std::string& str);
300
301
302 // Member Operators
303
304 //- Test for equality. Allows use as a predicate.
305 // \return True when strings match literally.
306 inline bool operator()(const std::string& text) const;
307
308
309 // Housekeeping
310
311 //- True if string contains given character (cf. C++23)
312 bool contains(char c) const noexcept
313 {
314 return (find(c) != std::string::npos);
315 }
316
317 //- True if string contains given [string view] substring (cf. C++23)
318 bool contains(const std::string& s) const noexcept
319 {
320 return (find(s) != std::string::npos);
321 }
322
323 //- True if string contains given substring (cf. C++23)
324 bool contains(const char* s) const
325 {
326 return (find(s) != std::string::npos);
327 }
328
329 //- True if string starts with given character (cf. C++20)
330 bool starts_with(char c) const
331 {
332 return (!empty() && front() == c);
333 }
334
335 //- True if string starts with given [string view] prefix (C++20)
336 bool starts_with(const std::string& s) const
337 {
338 return (size() >= s.size() && !compare(0, s.size(), s));
339 }
340
341 //- True if string starts with given prefix (C++20)
342 bool starts_with(const char* s) const
343 {
344 const auto len = strlen(s);
345 return (size() >= len && !compare(0, len, s, len));
346 }
347
348 //- True if string ends with given character (cf. C++20)
349 bool ends_with(char c) const
350 {
351 return (!empty() && back() == c);
352 }
353
354 //- True if string ends with given [string view] suffix (cf. C++20)
355 bool ends_with(const std::string& s) const
356 {
357 return (size() >= s.size() && !compare(size()-s.size(), npos, s));
358 }
359
360 //- True if string ends with given suffix (cf. C++20)
361 bool ends_with(const char* s) const
362 {
363 const auto len = strlen(s);
364 return (size() >= len && !compare(size()-len, npos, s, len));
365 }
366
367 //- Count the number of occurrences of the specified character
368 //- in the string
369 // Partially deprecated (NOV-2017) in favour of stringOps::count
370 size_type count(const char c) const;
371
372 //- Deprecated(2019-11)
373 // \deprecated(2019-11) use starts_with instead
374 bool startsWith(const std::string& s) const { return starts_with(s); }
375
376 //- Deprecated(2019-11)
377 // \deprecated(2019-11) use ends_with instead
378 bool endsWith(const std::string& s) const { return ends_with(s); }
379
380 //- Deprecated(2019-11)
381 // \deprecated(2019-11) use removeEnd instead
382 bool removeTrailing(const char c) { return removeEnd(c); }
383};
384
385
386// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
387
388//- Hashing for Foam::string
389template<> struct Hash<string> : string::hasher {};
390
391//- Hashing for std:::string
392template<> struct Hash<std::string> : string::hasher {};
393
394
395// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
396
397// IOstream Operators
398
399//- Read operator
400Istream& operator>>(Istream& is, string& val);
401
402//- Write operator
403Ostream& operator<<(Ostream& os, const string& val);
404
405//- Write operator
406Ostream& operator<<(Ostream& os, const std::string& val);
407
408
409// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
410
411} // End namespace Foam
413// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
414
415#include "stringI.H"
416
417#ifdef NoRepository
418 #include "stringTemplates.C"
419#endif
421// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
422
423#endif
424
425// ************************************************************************* //
Miscellaneous hashing functions, mostly from Bob Jenkins.
graph_traits< Graph >::vertices_size_type size_type
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:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A class for handling character strings derived from std::string.
Definition string.H:76
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
Definition string.C:207
static const string null
An empty string.
Definition string.H:203
bool startsWith(const std::string &s) const
Deprecated(2019-11).
Definition string.H:496
bool endsWith(const std::string &s) const
Deprecated(2019-11).
Definition string.H:503
static std::string::size_type length(const std::string &s)
The length of the string.
Definition string.H:267
bool ends_with(const std::string &s) const
True if string ends with given [string view] suffix (cf. C++20).
Definition string.H:469
static bool valid(const std::string &str)
Does the string contain valid characters only?
Definition stringI.H:149
bool remove_ext()
Remove extension, return true if string changed.
Definition stringI.H:93
bool ends_with(const char *s) const
True if string ends with given suffix (cf. C++20).
Definition string.H:477
size_type count(const char c) const
Count the number of occurrences of the specified character in the string.
Definition string.C:94
string()=default
Default construct.
bool contains(const char *s) const
True if string contains given substring (cf. C++23).
Definition string.H:428
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition stringI.H:164
static std::string::size_type length(const char *s)
Length of the character sequence (with nullptr protection).
Definition string.H:259
bool starts_with(const char *s) const
True if string starts with given prefix (C++20).
Definition string.H:452
bool contains(char c) const noexcept
True if string contains given character (cf. C++23).
Definition string.H:412
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition stringI.H:37
bool match(const std::string &text) const
Test for equality.
Definition stringI.H:217
bool starts_with(const std::string &s) const
True if string starts with given [string view] prefix (C++20).
Definition string.H:444
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition stringI.H:193
bool removeTrailing(const char c)
Deprecated(2019-11).
Definition string.H:510
bool remove_path()
Remove leading path, return true if string changed.
Definition stringI.H:79
string & replaceAll(const std::string &s1, const std::string &s2, size_type pos=0)
Replace all occurrences of sub-string s1 with s2, beginning at pos in the string.
Definition string.C:117
bool ends_with(char c) const
True if string ends with given character (cf. C++20).
Definition string.H:461
bool removeRepeated(const char character)
Remove repeated characters.
Definition string.C:173
string & expand(const bool allowEmpty=false)
Inplace expand initial tags, tildes, and all occurrences of environment variables as per stringOps::e...
Definition string.C:166
bool has_ext() const
Return true if it has an extension or simply ends with a '.'.
Definition stringI.H:43
word ext() const
Return file name extension (part after last .).
Definition string.C:38
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition stringI.H:235
bool starts_with(char c) const
True if string starts with given character (cf. C++20).
Definition string.H:436
string & replaceAny(const std::string &s1, const char c2, size_type pos=0)
Replace any occurrence of s1 characters with c2, beginning at pos in the string.
Definition string.C:140
static int debug
The debug flag.
Definition string.H:198
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Replace first occurrence of sub-string s1 with s2, beginning at pos.
Definition string.C:101
static std::string::size_type string_printf(std::string &output, const char *fmt, const PrimitiveType &val)
A printf-style formatter for a primitive.
bool contains(const std::string &s) const noexcept
True if string contains given [string view] substring (cf. C++23).
Definition string.H:420
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition stringI.H:223
static const char *const typeName
The type name "string".
Definition string.H:193
bool removeEnd(const std::string &text)
Remove the given text from the end of the string.
Definition string.C:222
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
A class for handling words, derived from Foam::string.
Definition word.H:66
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:575
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition Hash.H:48
Deprecated hashing functor - use hasher.
Definition string.H:185
Hashing functor for string and derived string classes.
Definition string.H:173
unsigned operator()(const std::string &str, unsigned seed=0) const
Definition string.H:174