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 -------------------------------------------------------------------------------
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 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 
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 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 
143 public:
144 
145  // Public Classes
146 
147  //- Hashing functor for string and derived string classes
148  struct hasher
149  {
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
343 template<> struct Hash<string> : string::hasher {};
344 
345 //- Hashing for std:::string
346 template<> struct Hash<std::string> : string::hasher {};
347 
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 // IOstream Operators
352 
353 //- Read operator
354 Istream& operator>>(Istream& is, string& val);
355 
356 //- Write operator
357 Ostream& operator<<(Ostream& os, const string& val);
358 
359 //- Write operator
360 Ostream& 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 // ************************************************************************* //
Foam::string::replace
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:108
Foam::string::find_ext
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition: stringI.H:44
Foam::string::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::string::string_printf
static std::string::size_type string_printf(std::string &output, const char *fmt, const PrimitiveType &val)
A printf-style formatter for a primitive.
Definition: stringTemplates.C:36
Foam::string::hasher::operator()
unsigned operator()(const std::string &str, unsigned seed=0) const
Definition: string.H:149
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
s
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))
Definition: gmvOutputSpray.H:25
Foam::string::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:96
Foam::Hasher
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:581
Foam::string::starts_with
bool starts_with(const std::string &s) const
True if string starts with the given prefix (cf. C++20)
Definition: string.H:297
Foam::string::operator()
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition: stringI.H:236
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::string::endsWith
bool endsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:331
Foam::string::typeName
static const char *const typeName
The type name "string".
Definition: string.H:163
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::string::debug
static int debug
The debug flag.
Definition: string.H:166
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
Foam::string::match
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:218
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::string::ext
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
Hasher.H
Miscellaneous hashing functions, mostly from Bob Jenkins.
Foam::string::starts_with
bool starts_with(const char c) const
True if string starts with the given character (cf. C++20)
Definition: string.H:303
Foam::string::hash
Deprecated hashing functor - use hasher.
Definition: string.H:157
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::string::replaceAny
string & replaceAny(const std::string &s1, const char c2, size_type pos=0)
Definition: string.C:147
Foam::string::hasPath
bool hasPath() const
Return true if it contains a '/' character.
Definition: stringI.H:50
Foam::string::ends_with
bool ends_with(const char c) const
True if string ends with the given character (cf. C++20)
Definition: string.H:315
Foam::string::removeRepeated
bool removeRepeated(const char character)
Remove repeated characters.
Definition: string.C:180
Foam::string::valid
static bool valid(const std::string &str)
Does the string contain valid characters only?
Definition: stringI.H:152
os
OBJstream os(runTime.globalPath()/outputName)
Foam::string::removeTrailing
bool removeTrailing(const char c)
Deprecated(2019-11)
Definition: string.H:335
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::string::replaceAll
string & replaceAll(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:124
Foam::constant::physicoChemical::c2
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
Foam::string::removePath
bool removePath()
Remove leading path, returning true if string changed.
Definition: stringI.H:82
Foam::string::startsWith
bool startsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:327
Foam::string::count
size_type count(const char c) const
Definition: string.C:101
Foam::string::hasher
Hashing functor for string and derived string classes.
Definition: string.H:147
Foam::string::ends_with
bool ends_with(const std::string &s) const
True if string ends with the given suffix (cf. C++20)
Definition: string.H:309
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:173
Foam::string::stripInvalid
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition: stringI.H:167
Foam::string::string
string()=default
Default construct.
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::string::swap
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:224
Foam::string::removeStart
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
Definition: string.C:214
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
stringTemplates.C
stringI.H
Foam::string::removeEnd
bool removeEnd(const std::string &text)
Remove the given text from the end of the string.
Definition: string.C:229
char.H
A character and a pointer to a character string.
Foam::string::validate
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition: stringI.H:196
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177