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-2022 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 Class
28  Foam::string
29 
30 Description
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 
38 See also
39  Foam::findEtcFile() for information about the site/user OpenFOAM
40  configuration directory
41 
42 SourceFiles
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>
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 namespace Foam
62 {
63 
64 // Forward Declarations
65 class string;
66 class word;
67 class wordRe;
68 class Istream;
69 class Ostream;
70 
71 template<class T> struct Hash;
72 
73 /*---------------------------------------------------------------------------*\
74  Class string Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 class string
78 :
79  public std::string
80 {
81 protected:
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>
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>
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 has an extension or simply ends with a '.'
122  inline bool has_ext() const;
123 
124  //- Return true if the extension is the same as the given ending.
125  // No proper nullptr protection.
126  inline bool has_ext(const char* ending) const;
127 
128  //- Return true if the extension is the same as the given ending.
129  inline bool has_ext(const std::string& ending) const;
130 
131  //- Return true if the extension matches the given ending.
132  bool has_ext(const wordRe& ending) const;
133 
134  //- Remove leading path, return true if string changed.
135  inline bool remove_path();
136 
137  //- Remove extension, return true if string changed.
138  inline bool remove_ext();
139 
140 
141 public:
142 
143  // Public Classes
144 
145  //- Hashing functor for string and derived string classes
146  struct hasher
147  {
148  unsigned operator()(const std::string& str, unsigned seed=0) const
149  {
150  return Foam::Hasher(str.data(), str.length(), seed);
151  }
152  };
153 
154  //- Deprecated hashing functor - use hasher
155  // \deprecated(2021-04) - use hasher
156  struct hash : string::hasher {};
157 
158 
159  // Static Data Members
160 
161  //- The type name "string"
162  static const char* const typeName;
163 
164  //- The debug flag
165  static int debug;
166 
167  //- An empty string
168  static const string null;
169 
170 
171  // Constructors
172 
173  //- Default construct
174  string() = default;
175 
176  //- Copy construct from std::string
177  inline string(const std::string& str);
178 
179  //- Move construct from std::string
180  inline string(std::string&& str);
181 
182  //- Construct as copy of character array
183  inline string(const char* str);
185  //- Construct as copy with a maximum number of characters
186  inline string(const char* str, const size_type len);
187 
188  //- Construct from a single character
189  inline explicit string(const char c);
190 
191  //- Construct fill copies of a single character
192  inline string(const size_type len, const char c);
193 
194  //- Construct from Istream
195  explicit string(Istream& is);
196 
198  // Static Member Functions
199 
200  //- Does the string contain valid characters only?
201  template<class StringType>
202  static inline bool valid(const std::string& str);
203 
204  //- Strip invalid characters from the given string
205  template<class StringType>
206  static inline bool stripInvalid(std::string& str);
207 
208  //- Return a valid String from the given string
209  template<class StringType>
210  static inline StringType validate(const std::string& str);
211 
212 
213  // Member Functions
214 
215  //- Test for equality.
216  // \return True when strings match literally.
217  inline bool match(const std::string& text) const;
218 
219  //- Avoid masking the normal std::string replace
220  using std::string::replace;
221 
222  //- Replace first occurrence of sub-string s1 with s2,
223  //- beginning at pos
224  string& replace
225  (
226  const std::string& s1,
227  const std::string& s2,
228  size_type pos = 0
229  );
230 
231  //- Replace all occurrences of sub-string s1 with s2,
232  //- beginning at pos in the string.
233  // A no-op if s1 is empty.
234  string& replaceAll
235  (
236  const std::string& s1,
237  const std::string& s2,
238  size_type pos = 0
239  );
240 
241  //- Replace any occurrence of s1 characters with c2,
242  //- beginning at pos in the string.
243  // A no-op if s1 is empty.
244  string& replaceAny
245  (
246  const std::string& s1,
247  const char c2,
248  size_type pos = 0
249  );
250 
251  //- Inplace expand initial tags, tildes, and all occurrences of
252  //- environment variables as per stringOps::expand
253  //
254  // Any unknown entries are removed silently if allowEmpty is true
255  // \sa
256  // Foam::findEtcFile
257  string& expand(const bool allowEmpty = false);
258 
259  //- Remove repeated characters
260  // \return True if string changed
261  bool removeRepeated(const char character);
262 
263  //- Remove the given text from the start of the string.
264  // \return True if the removal occurred
265  bool removeStart(const std::string& text);
266 
267  //- Remove leading character, unless string is a single character
268  // \return True if the removal occurred
269  bool removeStart(const char c);
270 
271  //- Remove the given text from the end of the string.
272  // \return True if the removal occurred
273  bool removeEnd(const std::string& text);
274 
275  //- Remove trailing character, unless string is a single character
276  // \return True if the removal occurred
277  bool removeEnd(const char c);
278 
279 
280  // Editing
281 
282  //- Swap contents. Self-swapping is a no-op.
283  inline void swap(std::string& str);
284 
285 
286  // Member Operators
287 
288  //- Test for equality. Allows use as a predicate.
289  // \return True when strings match literally.
290  inline bool operator()(const std::string& text) const;
291 
292 
293  // Housekeeping
294 
295  //- True if string contains given character (cf. C++23)
296  bool contains(char c) const noexcept
297  {
298  return (find(c) != std::string::npos);
299  }
300 
301  //- True if string contains given [string view] substring (cf. C++23)
302  bool contains(const std::string& s) const noexcept
303  {
304  return (find(s) != std::string::npos);
305  }
306 
307  //- True if string contains given substring (cf. C++23)
308  bool contains(const char* s) const
309  {
310  return (find(s) != std::string::npos);
311  }
312 
313  //- True if string starts with given character (cf. C++20)
314  bool starts_with(char c) const
315  {
316  return (!empty() && front() == c);
317  }
318 
319  //- True if string starts with given [string view] prefix (C++20)
320  bool starts_with(const std::string& s) const
321  {
322  return (size() >= s.size() && !compare(0, s.size(), s));
323  }
324 
325  //- True if string starts with given prefix (C++20)
326  bool starts_with(const char* s) const
327  {
328  const auto len = strlen(s);
329  return (size() >= len && !compare(0, len, s, len));
330  }
331 
332  //- True if string ends with given character (cf. C++20)
333  bool ends_with(char c) const
334  {
335  return (!empty() && back() == c);
336  }
337 
338  //- True if string ends with given [string view] suffix (cf. C++20)
339  bool ends_with(const std::string& s) const
340  {
341  return (size() >= s.size() && !compare(size()-s.size(), npos, s));
342  }
343 
344  //- True if string ends with given suffix (cf. C++20)
345  bool ends_with(const char* s) const
346  {
347  const auto len = strlen(s);
348  return (size() >= len && !compare(size()-len, npos, s, len));
349  }
350 
351  //- Count the number of occurrences of the specified character
352  //- in the string
353  // Partially deprecated (NOV-2017) in favour of stringOps::count
354  size_type count(const char c) const;
355 
356  //- Deprecated(2019-11)
357  // \deprecated(2019-11) use starts_with instead
358  bool startsWith(const std::string& s) const { return starts_with(s); }
359 
360  //- Deprecated(2019-11)
361  // \deprecated(2019-11) use ends_with instead
362  bool endsWith(const std::string& s) const { return ends_with(s); }
363 
364  //- Deprecated(2019-11)
365  // \deprecated(2019-11) use removeEnd instead
366  bool removeTrailing(const char c) { return removeEnd(c); }
367 };
368 
369 
370 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
371 
372 //- Hashing for Foam::string
373 template<> struct Hash<string> : string::hasher {};
374 
375 //- Hashing for std:::string
376 template<> struct Hash<std::string> : string::hasher {};
377 
378 
379 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380 
381 // IOstream Operators
382 
383 //- Read operator
384 Istream& operator>>(Istream& is, string& val);
385 
386 //- Write operator
387 Ostream& operator<<(Ostream& os, const string& val);
388 
389 //- Write operator
390 Ostream& operator<<(Ostream& os, const std::string& val);
391 
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 } // End namespace Foam
396 
397 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
399 #include "stringI.H"
400 
401 #ifdef NoRepository
402  #include "stringTemplates.C"
403 #endif
404 
405 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407 #endif
408 
409 // ************************************************************************* //
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
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:790
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition: stringI.H:37
bool contains(char c) const noexcept
True if string contains given character (cf. C++23)
Definition: string.H:390
Deprecated hashing functor - use hasher.
Definition: string.H:184
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition: stringI.H:233
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static std::string::size_type string_printf(std::string &output, const char *fmt, const PrimitiveType &val)
A printf-style formatter for a primitive.
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
bool endsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:481
unsigned operator()(const std::string &str, unsigned seed=0) const
Definition: string.H:173
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:215
A character and a pointer to a character string.
static const char *const typeName
The type name "string".
Definition: string.H:192
dimensionedScalar pos(const dimensionedScalar &ds)
bool ends_with(char c) const
True if string ends with given character (cf. C++20)
Definition: string.H:439
bool has_ext() const
Return true if it has an extension or simply ends with a &#39;.&#39;.
Definition: stringI.H:43
static bool valid(const std::string &str)
Does the string contain valid characters only?
Definition: stringI.H:149
static int debug
The debug flag.
Definition: string.H:197
A class for handling words, derived from Foam::string.
Definition: word.H:63
word ext() const
Return file name extension (part after last .)
Definition: string.C:38
Istream & operator>>(Istream &, directionInfo &)
bool removeTrailing(const char c)
Deprecated(2019-11)
Definition: string.H:488
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
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:68
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
bool removeRepeated(const char character)
Remove repeated characters.
Definition: string.C:173
bool startsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:474
static const string null
An empty string.
Definition: string.H:202
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition: stringI.H:164
Hashing functor for string and derived string classes.
Definition: string.H:171
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
bool remove_path()
Remove leading path, return true if string changed.
Definition: stringI.H:79
bool starts_with(char c) const
True if string starts with given character (cf. C++20)
Definition: string.H:414
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
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins&#39;s 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:575
size_type count(const char c) const
Count the number of occurrences of the specified character in the string.
Definition: string.C:94
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
Miscellaneous hashing functions, mostly from Bob Jenkins.
const dimensionedScalar c
Speed of light in a vacuum.
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
Definition: string.C:207
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:58
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:221
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))
A class for handling character strings derived from std::string.
Definition: string.H:72
string()=default
Default construct.
bool removeEnd(const std::string &text)
Remove the given text from the end of the string.
Definition: string.C:222
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition: stringI.H:193
Namespace for OpenFOAM.
bool remove_ext()
Remove extension, return true if string changed.
Definition: stringI.H:93