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 | Copyright (C) 2011-2016 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 
71 /*---------------------------------------------------------------------------*\
72  Class string Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 class string
76 :
77  public std::string
78 {
79 public:
80 
81  // Static data members
82 
83  static const char* const typeName;
84  static int debug;
85 
86  //- An empty string
87  static const string null;
88 
89 
90  //- Hashing function class, shared by all the derived classes
91  class hash
92  {
93  public:
94  hash()
95  {}
96 
97  inline unsigned operator()(const string&, unsigned seed = 0) const;
98  };
99 
100 
101  // Constructors
102 
103  //- Construct null
104  inline string();
105 
106  //- Construct from std::string
107  inline string(const std::string&);
108 
109  //- Construct as copy of character array
110  inline string(const char*);
111 
112  //- Construct as copy of specified number of characters
113  inline string(const char*, const size_type);
114 
115  //- Construct from a single character
116  inline string(const char);
117 
118  //- Construct from copies of a single character
119  inline string(const size_type, const char);
120 
121  //- Construct from Istream
122  string(Istream&);
123 
124 
125  // Member Functions
126 
127  //- Count and return the number of a given character in the string
128  size_type count(const char) const;
129 
130  //- Is this string type valid?
131  template<class String>
132  static inline bool valid(const string&);
133 
134  //- Does this string have particular meta-characters?
135  // The meta characters can be optionally quoted.
136  template<class String>
137  static inline bool meta(const string&, const char quote='\\');
138 
139  //- Strip invalid characters from the given string
140  template<class String>
141  static inline bool stripInvalid(string&);
142 
143  //- Return a valid String from the given string
144  template<class String>
145  static inline String validate(const string&);
146 
147  //- Return a String with quoted meta-characters from the given string
148  template<class String>
149  static inline string quotemeta(const string&, const char quote='\\');
150 
151  //- True when strings match literally
152  inline bool match(const std::string&) const;
153 
154  //- Avoid masking the normal std::string replace
155  using std::string::replace;
156 
157  //- Replace first occurence of sub-string oldStr with newStr
158  // starting at start
159  string& replace
160  (
161  const string& oldStr,
162  const string& newStr,
163  size_type start = 0
164  );
165 
166  //- Replace all occurences of sub-string oldStr with newStr
167  // starting at start
168  string& replaceAll
169  (
170  const string& oldStr,
171  const string& newStr,
172  size_type start = 0
173  );
174 
175  //- Expand initial tildes and all occurences of environment variables
176  // Expansion includes:
177  // -# environment variables
178  // - "$VAR", "${VAR}"
179  // -# current directory
180  // - leading "./" : the current directory
181  // -# tilde expansion
182  // - leading "~/" : home directory
183  // - leading "~user" : home directory for specified user
184  // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
185  //
186  // Any unknown entries are removed silently if allowEmpty is true
187  // \sa
188  // Foam::findEtcFile
189  string& expand(const bool allowEmpty = false);
190 
191  //- Remove repeated characters returning true if string changed
192  bool removeRepeated(const char);
193 
194  //- Return string with repeated characters removed
195  string removeRepeated(const char) const;
196 
197  //- Remove trailing character returning true if string changed
198  bool removeTrailing(const char);
199 
200  //- Return string with trailing character removed
201  string removeTrailing(const char) const;
202 
203 
204  // Member Operators
205 
206  //- Return the sub-string from the i-th character for \a n characters
207  inline string operator()
208  (
209  const size_type i,
210  const size_type n
211  ) const;
212 
213  //- Return the sub-string from the first character for \a n characters
214  inline string operator()
215  (
216  const size_type n
217  ) const;
218 
219 
220  // IOstream Operators
221 
222  friend Istream& operator>>(Istream&, string&);
223  friend Ostream& operator<<(Ostream&, const string&);
224 };
225 
226 
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 } // End namespace Foam
230 
231 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 
233 #include "stringI.H"
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 #endif
238 
239 // ************************************************************************* //
bool removeRepeated(const char)
Remove repeated characters returning true if string changed.
Definition: string.C:102
static bool meta(const string &, const char quote='\\')
Does this string have particular meta-characters?
Definition: stringI.H:118
Hashing function class, shared by all the derived classes.
Definition: string.H:90
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:39
string & replaceAll(const string &oldStr, const string &newStr, size_type start=0)
Replace all occurences of sub-string oldStr with newStr.
Definition: string.C:74
unsigned operator()(const string &, unsigned seed=0) const
Definition: stringI.H:211
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static String validate(const string &)
Return a valid String from the given string.
Definition: stringI.H:178
bool match(const std::string &) const
True when strings match literally.
Definition: stringI.H:185
friend Ostream & operator<<(Ostream &, const string &)
A character and a pointer to a character string.
static string quotemeta(const string &, const char quote='\\')
Return a String with quoted meta-characters from the given string.
static const char *const typeName
Definition: string.H:82
friend Istream & operator>>(Istream &, string &)
static int debug
Definition: string.H:83
Istream & operator>>(Istream &, directionInfo &)
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
static const string null
An empty string.
Definition: string.H:86
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
string()
Construct null.
Definition: stringI.H:30
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurence of sub-string oldStr with newStr.
Definition: string.C:56
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurences of environment variables.
Definition: string.C:95
Misc. hashing functions, mostly from Bob Jenkins.
Ostream & operator<<(Ostream &, const ensightPart &)
label n
static bool valid(const string &)
Is this string type valid?
Definition: stringI.H:70
static bool stripInvalid(string &)
Strip invalid characters from the given string.
Definition: stringI.H:84
bool removeTrailing(const char)
Remove trailing character returning true if string changed.
Definition: string.C:148
A class for handling character strings derived from std::string.
Definition: string.H:74
Namespace for OpenFOAM.