string.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::string
26 
27 Description
28  A class for handling character strings derived from std::string.
29 
30  Strings may contain any characters and therefore are delimited by quotes
31  for IO : "any list of characters".
32 
33  Used as a base class for word and fileName.
34 
35 See also
36  Foam::findEtcFile() for information about the site/user OpenFOAM
37  configuration directory
38 
39 SourceFiles
40  string.C
41  stringIO.C
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef string_H
46 #define string_H
47 
48 #include "char.H"
49 #include "Hasher.H"
50 
51 #include <string>
52 #include <cstring>
53 #include <cstdlib>
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 // Forward declaration of classes
61 class Istream;
62 class Ostream;
63 
64 // Forward declaration of friend functions and operators
65 class string;
66 Istream& operator>>(Istream&, string&);
67 Ostream& operator<<(Ostream&, const string&);
68 Ostream& operator<<(Ostream&, const std::string&);
69 
70 template<class T>
71 class UList;
72 
73 /*---------------------------------------------------------------------------*\
74  Class string Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 class string
78 :
79  public std::string
80 {
81 public:
82 
83  // Static Data Members
84 
85  static const char* const typeName;
86  static int debug;
87 
88  //- An empty string
89  static const string null;
90 
91 
92  //- Hashing function class, shared by all the derived classes
93  class hash
94  {
95  public:
96  hash()
97  {}
98 
99  inline unsigned operator()(const string&, unsigned seed = 0) const;
100  };
101 
102 
103  // Constructors
104 
105  //- Construct null
106  inline string();
107 
108  //- Construct from std::string
109  inline string(const std::string&);
110 
111  //- Copy constructor
112  inline string(const string&);
113 
114  //- Move constructor
115  inline string(string&&);
116 
117  //- Construct as copy of character array
118  inline string(const char*);
119 
120  //- Construct as copy of UList of character
121  string(const UList<char>&);
122 
123  //- Construct as copy of specified number of characters
124  inline string(const char*, const size_type);
125 
126  //- Construct from a single character
127  inline string(const char);
128 
129  //- Construct from copies of a single character
130  inline string(const size_type, const char);
131 
132  //- Construct from Istream
133  string(Istream&);
134 
135 
136  // Member Functions
137 
138  //- Count and return the number of a given character in the string
139  size_type count(const char) const;
140 
141  //- Is this string type valid?
142  template<class String>
143  static inline bool valid(const string&);
144 
145  //- Does this string have particular meta-characters?
146  // The meta characters can be optionally quoted.
147  template<class String>
148  static inline bool meta(const string&, const char quote='\\');
149 
150  //- Strip invalid characters from the given string
151  template<class String>
152  static inline bool stripInvalid(string&);
153 
154  //- Return a valid String from the given string
155  template<class String>
156  static inline String validate(const string&);
157 
158  //- Return a String with quoted meta-characters from the given string
159  template<class String>
160  static inline string quotemeta(const string&, const char quote='\\');
161 
162  //- True when strings match literally
163  inline bool match(const std::string&) const;
164 
165  //- Avoid masking the normal std::string replace
166  using std::string::replace;
167 
168  //- In this string replace first occurrence of sub-string oldStr
169  // with newStr starting at start
170  string& replace
171  (
172  const string& oldStr,
173  const string& newStr,
174  size_type start = 0
175  );
176 
177  //- Replace first occurrence of sub-string oldStr with newStr
178  // starting at start
179  string replace
180  (
181  const string& oldStr,
182  const string& newStr,
183  size_type start = 0
184  ) const;
185 
186  //- In this string replace all occurrences of sub-string oldStr
187  // with newStr starting at start
188  string& replaceAll
189  (
190  const string& oldStr,
191  const string& newStr,
192  size_type start = 0
193  );
194 
195  //- Replace all occurrences of sub-string oldStr with newStr
196  // starting at start
197  string replaceAll
198  (
199  const string& oldStr,
200  const string& newStr,
201  size_type start = 0
202  ) const;
203 
204  //- Expand initial tildes and all occurrences of environment variables
205  // Expansion includes:
206  // -# environment variables
207  // - "$VAR", "${VAR}"
208  // -# current directory
209  // - leading "./" : the current directory
210  // -# tilde expansion
211  // - leading "~/" : home directory
212  // - leading "~user" : home directory for specified user
213  // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
214  //
215  // Any unknown entries are removed silently if allowEmpty is true
216  // \sa
217  // Foam::findEtcFile
218  string& expand(const bool allowEmpty = false);
219 
220  //- Remove all occurrences of character returning true if string changed
221  bool remove(const char);
222 
223  //- Remove all occurrences of character and return the string
224  string remove(const char) const;
225 
226  //- Remove repeated character returning true if string changed
227  bool removeRepeated(const char);
228 
229  //- Remove repeated character and return the string
230  string removeRepeated(const char) const;
231 
232  //- Remove trailing character returning true if string changed
233  bool removeTrailing(const char);
234 
235  //- Return string with trailing character removed
236  string removeTrailing(const char) const;
237 
238  //- Remove trailing string returning true if string changed
239  bool removeTrailing(const string&);
240 
241  //- Return string with trailing string removed
242  string removeTrailing(const string&) const;
243 
244  //- Strip characters from the start and end of the string
245  void strip(const string&);
246 
247  //- Find the closing character. Brackets counting algorithm. The
248  // opening bracket character is taken to be the one at the starting
249  // position. The closing character is provided by argument.
250  //
251  // Examples:
252  //
253  // [char #1] [char #15]
254  // | |
255  // V V
256  // string("0(2(4,6)8,a(c)e)g").findClosing(')',1) == 15
257  //
258  // [char #3] [char #7]
259  // | |
260  // V V
261  // string("0(2(4,6)8,a(c)e)g").findClosing(')',3) == 7
262  //
263  size_type findClosing(const char, const size_type) const;
264 
265 
266  // Member Operators
267 
268  //- Return the sub-string from the i-th character for \a n characters
269  inline string operator()
270  (
271  const size_type i,
272  const size_type n
273  ) const;
274 
275  //- Return the sub-string from the first character for \a n characters
276  inline string operator()
277  (
278  const size_type n
279  ) const;
280 
281  inline void operator=(const string&);
282  inline void operator=(string&&);
283 
284 
285  // IOstream Operators
286 
287  friend Istream& operator>>(Istream&, string&);
288  friend Ostream& operator<<(Ostream&, const string&);
289 };
290 
291 
292 void writeEntry(Ostream& os, const char* value);
293 void writeEntry(Ostream& os, const string& value);
294 
295 
296 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
297 
298 } // End namespace Foam
299 
300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301 
302 #include "stringI.H"
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 #endif
307 
308 // ************************************************************************* //
Misc. hashing functions, mostly from Bob Jenkins.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
label n
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:57
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
Hashing function class, shared by all the derived classes.
Definition: string.H:93
unsigned operator()(const string &, unsigned seed=0) const
Definition: stringI.H:220
A class for handling character strings derived from std::string.
Definition: string.H:79
string & replaceAll(const string &oldStr, const string &newStr, size_type start=0)
In this string replace all occurrences of sub-string oldStr.
Definition: string.C:93
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:47
friend Ostream & operator<<(Ostream &, const string &)
bool removeTrailing(const char)
Remove trailing character returning true if string changed.
Definition: string.C:219
size_type findClosing(const char, const size_type) const
Find the closing character. Brackets counting algorithm. The.
Definition: string.C:299
void strip(const string &)
Strip characters from the start and end of the string.
Definition: string.C:265
friend Istream & operator>>(Istream &, string &)
static bool meta(const string &, const char quote='\\')
Does this string have particular meta-characters?
Definition: stringI.H:127
static String validate(const string &)
Return a valid String from the given string.
Definition: stringI.H:187
static string quotemeta(const string &, const char quote='\\')
Return a String with quoted meta-characters from the given string.
string & replace(const string &oldStr, const string &newStr, size_type start=0)
In this string replace first occurrence of sub-string oldStr.
Definition: string.C:64
static bool valid(const string &)
Is this string type valid?
Definition: stringI.H:79
static bool stripInvalid(string &)
Strip invalid characters from the given string.
Definition: stringI.H:93
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurrences of environment variables.
Definition: string.C:125
bool remove(const char)
Remove all occurrences of character returning true if string changed.
Definition: string.C:132
static int debug
Definition: string.H:85
string()
Construct null.
Definition: stringI.H:30
void operator=(const string &)
Definition: stringI.H:229
bool removeRepeated(const char)
Remove repeated character returning true if string changed.
Definition: string.C:175
bool match(const std::string &) const
True when strings match literally.
Definition: stringI.H:194
static const char *const typeName
Definition: string.H:84
Namespace for OpenFOAM.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)