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 -------------------------------------------------------------------------------
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  //- Avoid masking the normal std::string length() method
201  using std::string::length;
203  //- Length of the character sequence (with nullptr protection)
204  static inline std::string::size_type length(const char* s)
205  {
206  return (s ? strlen(s) : 0);
207  }
208 
209  //- The length of the string
210  static inline std::string::size_type length(const std::string& s)
211  {
212  return s.size();
213  }
214 
215  //- Does the string contain valid characters only?
216  template<class StringType>
217  static inline bool valid(const std::string& str);
218 
219  //- Strip invalid characters from the given string
220  template<class StringType>
221  static inline bool stripInvalid(std::string& str);
222 
223  //- Return a valid String from the given string
224  template<class StringType>
225  static inline StringType validate(const std::string& str);
226 
227 
228  // Member Functions
229 
230  //- Test for equality.
231  // \return True when strings match literally.
232  inline bool match(const std::string& text) const;
233 
234  //- Avoid masking the normal std::string replace
235  using std::string::replace;
236 
237  //- Replace first occurrence of sub-string s1 with s2,
238  //- beginning at pos
239  string& replace
240  (
241  const std::string& s1,
242  const std::string& s2,
243  size_type pos = 0
244  );
245 
246  //- Replace all occurrences of sub-string s1 with s2,
247  //- beginning at pos in the string.
248  // A no-op if s1 is empty.
249  string& replaceAll
250  (
251  const std::string& s1,
252  const std::string& s2,
253  size_type pos = 0
254  );
255 
256  //- Replace any occurrence of s1 characters with c2,
257  //- beginning at pos in the string.
258  // A no-op if s1 is empty.
259  string& replaceAny
260  (
261  const std::string& s1,
262  const char c2,
263  size_type pos = 0
264  );
265 
266  //- Inplace expand initial tags, tildes, and all occurrences of
267  //- environment variables as per stringOps::expand
268  //
269  // Any unknown entries are removed silently if allowEmpty is true
270  // \sa
271  // Foam::findEtcFile
272  string& expand(const bool allowEmpty = false);
273 
274  //- Remove repeated characters
275  // \return True if string changed
276  bool removeRepeated(const char character);
277 
278  //- Remove the given text from the start of the string.
279  // \return True if the removal occurred
280  bool removeStart(const std::string& text);
281 
282  //- Remove leading character, unless string is a single character
283  // \return True if the removal occurred
284  bool removeStart(const char c);
285 
286  //- Remove the given text from the end of the string.
287  // \return True if the removal occurred
288  bool removeEnd(const std::string& text);
289 
290  //- Remove trailing character, unless string is a single character
291  // \return True if the removal occurred
292  bool removeEnd(const char c);
293 
294 
295  // Editing
296 
297  //- Swap contents. Self-swapping is a no-op.
298  inline void swap(std::string& str);
299 
300 
301  // Member Operators
302 
303  //- Test for equality. Allows use as a predicate.
304  // \return True when strings match literally.
305  inline bool operator()(const std::string& text) const;
306 
307 
308  // Housekeeping
309 
310  //- True if string contains given character (cf. C++23)
311  bool contains(char c) const noexcept
312  {
313  return (find(c) != std::string::npos);
314  }
315 
316  //- True if string contains given [string view] substring (cf. C++23)
317  bool contains(const std::string& s) const noexcept
318  {
319  return (find(s) != std::string::npos);
320  }
321 
322  //- True if string contains given substring (cf. C++23)
323  bool contains(const char* s) const
324  {
325  return (find(s) != std::string::npos);
326  }
327 
328  //- True if string starts with given character (cf. C++20)
329  bool starts_with(char c) const
330  {
331  return (!empty() && front() == c);
332  }
333 
334  //- True if string starts with given [string view] prefix (C++20)
335  bool starts_with(const std::string& s) const
336  {
337  return (size() >= s.size() && !compare(0, s.size(), s));
338  }
339 
340  //- True if string starts with given prefix (C++20)
341  bool starts_with(const char* s) const
342  {
343  const auto len = strlen(s);
344  return (size() >= len && !compare(0, len, s, len));
345  }
346 
347  //- True if string ends with given character (cf. C++20)
348  bool ends_with(char c) const
349  {
350  return (!empty() && back() == c);
351  }
352 
353  //- True if string ends with given [string view] suffix (cf. C++20)
354  bool ends_with(const std::string& s) const
355  {
356  return (size() >= s.size() && !compare(size()-s.size(), npos, s));
357  }
358 
359  //- True if string ends with given suffix (cf. C++20)
360  bool ends_with(const char* s) const
361  {
362  const auto len = strlen(s);
363  return (size() >= len && !compare(size()-len, npos, s, len));
364  }
365 
366  //- Count the number of occurrences of the specified character
367  //- in the string
368  // Partially deprecated (NOV-2017) in favour of stringOps::count
369  size_type count(const char c) const;
370 
371  //- Deprecated(2019-11)
372  // \deprecated(2019-11) use starts_with instead
373  bool startsWith(const std::string& s) const { return starts_with(s); }
374 
375  //- Deprecated(2019-11)
376  // \deprecated(2019-11) use ends_with instead
377  bool endsWith(const std::string& s) const { return ends_with(s); }
378 
379  //- Deprecated(2019-11)
380  // \deprecated(2019-11) use removeEnd instead
381  bool removeTrailing(const char c) { return removeEnd(c); }
382 };
383 
384 
385 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
386 
387 //- Hashing for Foam::string
388 template<> struct Hash<string> : string::hasher {};
389 
390 //- Hashing for std:::string
391 template<> struct Hash<std::string> : string::hasher {};
392 
393 
394 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
395 
396 // IOstream Operators
397 
398 //- Read operator
399 Istream& operator>>(Istream& is, string& val);
400 
401 //- Write operator
402 Ostream& operator<<(Ostream& os, const string& val);
403 
404 //- Write operator
405 Ostream& operator<<(Ostream& os, const std::string& val);
406 
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410 } // End namespace Foam
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 
414 #include "stringI.H"
415 
416 #ifdef NoRepository
417  #include "stringTemplates.C"
418 #endif
420 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
421 
422 #endif
423 
424 // ************************************************************************* //
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:795
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition: stringI.H:37
static std::string::size_type length(const char *s)
Length of the character sequence (with nullptr protection)
Definition: string.H:258
bool contains(char c) const noexcept
True if string contains given character (cf. C++23)
Definition: string.H:411
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:235
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:502
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:217
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:460
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:509
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:495
static const string null
An empty string.
Definition: string.H:202
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:77
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:435
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:44
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:223
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