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-2020 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 <string>
56 #include <cstring>
57 #include <cstdlib>
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 namespace Foam
62 {
63 
64 // Forward Declarations
65 class word;
66 class wordRe;
67 class Istream;
68 class Ostream;
69 
70 /*---------------------------------------------------------------------------*\
71  Class string Declaration
72 \*---------------------------------------------------------------------------*/
73 
74 class string
75 :
76  public std::string
77 {
78 protected:
79 
80  // Protected Member Functions
81 
82  //- Find position of a file extension dot, return npos on failure.
83  // A wrapped version of find_last_of("./") with additional logic.
84  inline static std::string::size_type find_ext(const std::string& str);
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 std::string::size_type find_ext() const;
89 
90  //- A printf-style formatter for a primitive.
91  template<class PrimitiveType>
93  (
94  std::string& output,
95  const char* fmt,
96  const PrimitiveType& val
97  );
98 
99  //- A printf-style formatter for a primitive.
100  template<class PrimitiveType>
102  (
103  std::string& output,
104  const std::string& fmt,
105  const PrimitiveType& val
106  );
107 
108  //- Return file name extension (part after last .)
109  word ext() const;
110 
111  //- Append a '.' and the ending.
112  // The '.' and ending will not be added when the ending is empty,
113  // or when the object was or ended with a '/'.
114  //
115  // \return True if append occurred.
116  bool ext(const word& ending);
117 
118  //- Return true if it contains a '/' character
119  inline bool hasPath() const;
120 
121  //- Return true if it has an extension or simply ends with a '.'
122  inline bool hasExt() const;
123 
124  //- Return true if the extension is the same as the given ending.
125  bool hasExt(const word& ending) const;
126 
127  //- Return true if the extension matches the given ending.
128  bool hasExt(const wordRe& ending) const;
129 
130  //- Remove extension, returning true if string changed.
131  inline bool removeExt();
132 
133  //- Remove leading path, returning true if string changed.
134  inline bool removePath();
135 
136 
137 public:
138 
139  // Static Data Members
140 
141  //- The type name "string"
142  static const char* const typeName;
143 
144  //- The debug flag
145  static int debug;
146 
147  //- An empty string
148  static const string null;
149 
150 
151  //- Hashing function for string and derived string classes
152  struct hash
153  {
154  inline unsigned operator()
155  (
156  const std::string& str,
157  unsigned seed = 0
158  ) const;
159  };
160 
161 
162  // Constructors
163 
164  //- Construct null
165  string() = default;
166 
167  //- Copy construct from std::string
168  inline string(const std::string& str);
169 
170  //- Move construct from std::string
171  inline string(std::string&& str);
172 
173  //- Construct as copy of character array
174  inline string(const char* str);
175 
176  //- Construct as copy with a maximum number of characters
177  inline string(const char* str, const size_type len);
178 
179  //- Construct from a single character
180  inline explicit string(const char c);
181 
182  //- Construct fill copies of a single character
183  inline string(const size_type len, const char c);
184 
185  //- Construct from Istream
186  explicit string(Istream& is);
187 
188 
189  // Static Member Functions
190 
191  //- Does the string contain valid characters only?
192  template<class String>
193  static inline bool valid(const std::string& str);
194 
195  //- Does this string contain meta-characters?
196  // The meta characters can be optionally quoted.
197  template<class String>
198  static inline bool meta(const std::string& str, const char quote='\\');
199 
200  //- Strip invalid characters from the given string
201  template<class String>
202  static inline bool stripInvalid(std::string& str);
203 
204  //- Return a valid String from the given string
205  template<class String>
206  static inline String validate(const std::string& str);
207 
208  //- Return a String with quoted meta-characters from the given string
209  template<class String>
210  static inline string quotemeta
211  (
212  const std::string& str,
213  const char quote = '\\'
214  );
215 
216 
217  // Member Functions
218 
219  //- Test for equality.
220  // \return True when strings match literally.
221  inline bool match(const std::string& text) const;
222 
223  //- Avoid masking the normal std::string replace
224  using std::string::replace;
225 
226  //- Replace first occurrence of sub-string s1 with s2,
227  //- beginning at pos
228  string& replace
229  (
230  const std::string& s1,
231  const std::string& s2,
232  size_type pos = 0
233  );
234 
235  //- Replace all occurrences of sub-string s1 with s2,
236  //- beginning at pos in the string.
237  // A no-op if s1 is empty.
238  string& replaceAll
239  (
240  const std::string& s1,
241  const std::string& s2,
242  size_type pos = 0
243  );
244 
245  //- Replace any occurrence of s1 characters with c2,
246  //- beginning at pos in the string.
247  // A no-op if s1 is empty.
248  string& replaceAny
249  (
250  const std::string& s1,
251  const char c2,
252  size_type pos = 0
253  );
254 
255  //- Inplace expand initial tags, tildes, and all occurrences of
256  //- environment variables as per stringOps::expand
257  //
258  // Any unknown entries are removed silently if allowEmpty is true
259  // \sa
260  // Foam::findEtcFile
261  string& expand(const bool allowEmpty = false);
262 
263  //- Remove repeated characters
264  // \return True if string changed
265  bool removeRepeated(const char character);
266 
267  //- Remove the given text from the start of the string.
268  // \return True if the removal occurred
269  bool removeStart(const std::string& text);
270 
271  //- Remove leading character, unless string is a single character
272  // \return True if the removal occurred
273  bool removeStart(const char c);
274 
275  //- Remove the given text from the end of the string.
276  // \return True if the removal occurred
277  bool removeEnd(const std::string& text);
278 
279  //- Remove trailing character, unless string is a single character
280  // \return True if the removal occurred
281  bool removeEnd(const char c);
282 
283 
284  // Editing
285 
286  //- Swap contents. Self-swapping is a no-op.
287  inline void swap(std::string& str);
288 
289 
290  // Member Operators
291 
292  //- Test for equality. Allows use as a predicate.
293  // \return True when strings match literally.
294  inline bool operator()(const std::string& text) const;
295 
296 
297  // Housekeeping
298 
299  //- True if string starts with the given prefix (cf. C++20)
300  bool starts_with(const std::string& s) const
301  {
302  return (size() >= s.size() && !compare(0, s.size(), s));
303  }
304 
305  //- True if string starts with the given character (cf. C++20)
306  bool starts_with(const char c) const
307  {
308  return (!empty() && front() == c);
309  }
310 
311  //- True if string ends with the given suffix (cf. C++20)
312  bool ends_with(const std::string& s) const
313  {
314  return (size() >= s.size() && !compare(size()-s.size(), npos, s));
315  }
316 
317  //- True if string ends with the given character (cf. C++20)
318  bool ends_with(const char c) const
319  {
320  return (!empty() && back() == c);
321  }
322 
323  //- Count the number of occurrences of the specified character
324  //- in the string
325  // Partially deprecated (NOV-2017) in favour of stringOps::count
326  size_type count(const char c) const;
327 
328  //- Deprecated(2019-11)
329  // \deprecated(2019-11) use starts_with instead
330  bool startsWith(const std::string& s) const { return starts_with(s); }
331 
332  //- Deprecated(2019-11)
333  // \deprecated(2019-11) use ends_with instead
334  bool endsWith(const std::string& s) const { return ends_with(s); }
335 
336  //- Deprecated(2019-11)
337  // \deprecated(2019-11) use removeEnd instead
338  bool removeTrailing(const char c) { return removeEnd(c); }
339 };
340 
341 
342 // IOstream Operators
343 
344 //- Read operator
345 Istream& operator>>(Istream& is, string& val);
346 
347 //- Write operator
348 Ostream& operator<<(Ostream& os, const string& val);
349 
350 //- Write operator
351 Ostream& operator<<(Ostream& os, const std::string& val);
352 
353 
354 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
355 
356 } // End namespace Foam
357 
358 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
359 
360 #include "stringI.H"
361 
362 #ifdef NoRepository
363  #include "stringTemplates.C"
364 #endif
365 
366 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
367 
368 #endif
369 
370 // ************************************************************************* //
Foam::string::replace
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:121
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::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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:76
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:299
Foam::string::operator()
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition: stringI.H:278
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:73
Foam::string::endsWith
bool endsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:333
Foam::string::typeName
static const char *const typeName
The type name "string".
Definition: string.H:141
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::string::debug
static int debug
The debug flag.
Definition: string.H:144
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::string::match
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:260
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
Misc. 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:305
Foam::string::hash
Hashing function for string and derived string classes.
Definition: string.H:151
Foam::string::validate
static String validate(const std::string &str)
Return a valid String from the given string.
Definition: stringI.H:238
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:160
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:317
Foam::string::removeRepeated
bool removeRepeated(const char character)
Remove repeated characters.
Definition: string.C:193
Foam::string::valid
static bool valid(const std::string &str)
Does the string contain valid characters only?
Definition: stringI.H:132
Foam::string::removeTrailing
bool removeTrailing(const char c)
Deprecated(2019-11)
Definition: string.H:337
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:137
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:62
Foam::string::meta
static bool meta(const std::string &str, const char quote='\\')
Does this string contain meta-characters?
Definition: stringI.H:176
Foam::string::startsWith
bool startsWith(const std::string &s) const
Deprecated(2019-11)
Definition: string.H:329
Foam::string::count
size_type count(const char c) const
Definition: string.C:114
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:311
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:186
Foam::string::stripInvalid
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition: stringI.H:147
Foam::string::string
string()=default
Construct null.
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:266
Foam::string::removeStart
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
Definition: string.C:227
Foam::string::quotemeta
static string quotemeta(const std::string &str, const char quote='\\')
Return a String with quoted meta-characters from the given string.
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:242
char.H
A character and a pointer to a character string.
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177