stringI.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-2019 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 \*---------------------------------------------------------------------------*/
25 
26 #include <iostream>
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
31 {}
32 
33 
34 inline Foam::string::string(const std::string& str)
35 :
36  std::string(str)
37 {}
38 
39 
40 inline Foam::string::string(const string& str)
41 :
42  std::string(str)
43 {}
44 
45 
46 inline Foam::string::string(string&& str)
47 :
48  std::string(move(str))
49 {}
50 
51 
52 inline Foam::string::string(const char* str)
53 :
54  std::string(str)
55 {}
56 
57 
58 inline Foam::string::string(const char* str, const size_type len)
59 :
60  std::string(str, len)
61 {}
62 
63 
64 inline Foam::string::string(const char c)
65 :
66  std::string(1, c)
67 {}
68 
69 
70 inline Foam::string::string(const size_type len, const char c)
71 :
72  std::string(len, c)
73 {}
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
78 template<class String>
79 inline bool Foam::string::valid(const string& str)
80 {
81  for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
82  {
83  if (!String::valid(*iter))
84  {
85  return false;
86  }
87  }
88  return true;
89 }
90 
91 
92 template<class String>
93 inline bool Foam::string::stripInvalid(string& str)
94 {
95  if (!valid<String>(str))
96  {
97  size_type nValid = 0;
98  iterator iter2 = str.begin();
99 
100  for
101  (
102  const_iterator iter1 = iter2;
103  iter1 != const_cast<const string&>(str).end();
104  iter1++
105  )
106  {
107  char c = *iter1;
108 
109  if (String::valid(c))
110  {
111  *iter2 = c;
112  ++iter2;
113  ++nValid;
114  }
115  }
116 
117  str.resize(nValid);
118 
119  return true;
120  }
121 
122  return false;
123 }
124 
125 
126 template<class String>
127 inline bool Foam::string::meta(const string& str, const char quote)
128 {
129  int escaped = 0;
130  for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
131  {
132  if (quote && *iter == quote)
133  {
134  escaped ^= 1; // toggle state
135  }
136  else if (escaped)
137  {
138  escaped = false;
139  }
140  else if (String::meta(*iter))
141  {
142  return true;
143  }
144  }
145  return false;
146 }
147 
148 
149 template<class String>
150 inline Foam::string
151 Foam::string::quotemeta(const string& str, const char quote)
152 {
153  if (!quote)
154  {
155  return str;
156  }
157 
158  string sQuoted;
159  sQuoted.reserve(2*str.length());
160 
161  int escaped = 0;
162  for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
163  {
164  if (*iter == quote)
165  {
166  escaped ^= 1; // toggle state
167  }
168  else if (escaped)
169  {
170  escaped = 0;
171  }
172  else if (String::meta(*iter))
173  {
174  sQuoted += quote;
175  }
176 
177  sQuoted += *iter;
178  }
179 
180  sQuoted.resize(sQuoted.length());
181 
182  return sQuoted;
183 }
184 
185 
186 template<class String>
187 inline String Foam::string::validate(const string& str)
188 {
189  string ss = str;
190  stripInvalid<String>(ss);
191  return ss;
192 }
193 
194 inline bool Foam::string::match(const std::string& str) const
195 {
196  // check as string
197  return (str == *this);
198 }
199 
200 
201 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
202 
203 inline Foam::string Foam::string::operator()
204 (
205  const size_type i,
206  const size_type n
207 ) const
208 {
209  return substr(i, n);
210 }
211 
212 
214 {
215  return substr(0, n);
216 }
217 
218 
219 inline unsigned Foam::string::hash::operator()
220 (
221  const string& key,
222  unsigned seed
223 ) const
224 {
225  return Hasher(key.data(), key.size(), seed);
226 }
227 
228 
229 void Foam::string::operator=(const string& str)
230 {
231  std::string::operator=(str);
232 }
233 
234 
235 void Foam::string::operator=(string&& str)
236 {
237  std::string::operator=(move(str));
238 }
239 
240 
241 // ************************************************************************* //
static bool meta(const string &, const char quote='\\')
Does this string have particular meta-characters?
Definition: stringI.H:127
string operator()(const size_type i, const size_type n) const
Return the sub-string from the i-th character for n characters.
Definition: stringI.H:204
static String validate(const string &)
Return a valid String from the given string.
Definition: stringI.H:187
const dimensionedScalar c
Speed of light in a vacuum.
bool match(const std::string &) const
True when strings match literally.
Definition: stringI.H:194
static string quotemeta(const string &, const char quote='\\')
Return a String with quoted meta-characters from the given string.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
string()
Construct null.
Definition: stringI.H:30
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins&#39;s 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:476
void operator=(const string &)
Definition: stringI.H:229
label n
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
A class for handling character strings derived from std::string.
Definition: string.H:76