stringI.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-2015 OpenFOAM Foundation
9  Copyright (C) 2017-2019 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 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30 
31 inline std::string::size_type Foam::string::find_ext(const std::string& str)
32 {
33  const auto i = str.find_last_of("./");
34 
35  if (i == npos || i == 0 || str[i] == '/')
36  {
37  return npos;
38  }
39 
40  return i;
41 }
42 
43 
45 {
46  return find_ext(*this);
47 }
48 
49 
50 inline bool Foam::string::hasPath() const
51 {
52  return (npos != find('/'));
53 }
54 
55 
56 inline bool Foam::string::hasExt() const
57 {
58  return (npos != find_ext());
59 }
60 
61 
63 {
64  const auto i = rfind('/');
65 
66  if (npos == i)
67  {
68  return false;
69  }
70 
71  erase(0, i+1);
72  return true;
73 }
74 
75 
77 {
78  const auto i = find_ext();
79 
80  if (npos == i)
81  {
82  return false;
83  }
84 
85  erase(i);
86  return true;
87 }
88 
89 
90 
91 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
92 
93 inline Foam::string::string(const std::string& str)
94 :
95  std::string(str)
96 {}
97 
98 
99 inline Foam::string::string(std::string&& str)
100 :
101  std::string(std::move(str))
102 {}
103 
104 
105 inline Foam::string::string(const char* str)
106 :
107  std::string(str)
108 {}
109 
110 
111 inline Foam::string::string(const char* str, const size_type len)
112 :
113  std::string(str, len)
114 {}
115 
116 
117 inline Foam::string::string(const char c)
118 :
119  std::string(1, c)
120 {}
121 
122 
123 inline Foam::string::string(const size_type len, const char c)
124 :
125  std::string(len, c)
126 {}
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
131 template<class String>
132 inline bool Foam::string::valid(const std::string& str)
133 {
134  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
135  {
136  if (!String::valid(*iter))
137  {
138  return false;
139  }
140  }
141 
142  return true;
143 }
144 
145 
146 template<class String>
147 inline bool Foam::string::stripInvalid(std::string& str)
148 {
149  if (!valid<String>(str))
150  {
151  size_type nChar = 0;
152  iterator outIter = str.begin();
153 
154  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
155  {
156  const char c = *iter;
157 
158  if (String::valid(c))
159  {
160  *outIter = c;
161  ++outIter;
162  ++nChar;
163  }
164  }
165 
166  str.erase(nChar);
167 
168  return true;
169  }
170 
171  return false;
172 }
173 
174 
175 template<class String>
176 inline bool Foam::string::meta(const std::string& str, const char quote)
177 {
178  int escaped = 0;
179  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
180  {
181  const char c = *iter;
182  if (quote && c == quote)
183  {
184  escaped ^= 1; // toggle state
185  }
186  else if (escaped)
187  {
188  escaped = 0;
189  }
190  else if (String::meta(c))
191  {
192  return true;
193  }
194  }
195  return false;
196 }
197 
198 
199 template<class String>
200 inline Foam::string
201 Foam::string::quotemeta(const std::string& str, const char quote)
202 {
203  if (!quote)
204  {
205  return str;
206  }
207 
208  string sQuoted;
209  sQuoted.reserve(2*str.size());
210 
211  int escaped = 0;
212  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
213  {
214  const char c = *iter;
215  if (c == quote)
216  {
217  escaped ^= 1; // toggle state
218  }
219  else if (escaped)
220  {
221  escaped = 0;
222  }
223  else if (String::meta(c))
224  {
225  sQuoted += quote;
226  }
227 
228  sQuoted += c;
229  }
230 
231  sQuoted.shrink_to_fit();
232 
233  return sQuoted;
234 }
235 
236 
237 template<class String>
238 inline String Foam::string::validate(const std::string& str)
239 {
240  String out;
241  out.resize(str.size());
242 
243  size_type len = 0;
244  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
245  {
246  const char c = *iter;
247  if (String::valid(c))
248  {
249  out[len] = c;
250  ++len;
251  }
252  }
253 
254  out.erase(len);
255 
256  return out;
257 }
258 
259 
260 inline bool Foam::string::match(const std::string& text) const
261 {
262  return !compare(text); // Always compare as literal string
263 }
264 
265 
266 inline void Foam::string::swap(std::string& str)
267 {
268  if (this != &str)
269  {
270  // Self-swap is a no-op
271  std::string::swap(str);
272  }
273 }
274 
275 
276 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
277 
278 inline bool Foam::string::operator()(const std::string& text) const
279 {
280  return !compare(text); // Always compare as literal string
281 }
282 
283 
284 inline unsigned Foam::string::hash::operator()
285 (
286  const std::string& str,
287  unsigned seed
288 ) const
289 {
290  return Hasher(str.data(), str.size(), seed);
291 }
292 
293 
294 // ************************************************************************* //
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::string::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:76
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:473
Foam::string::operator()
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition: stringI.H:278
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
erase
srcOptions erase("case")
Foam::string::match
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:260
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::hasPath
bool hasPath() const
Return true if it contains a '/' character.
Definition: stringI.H:50
Foam::string::valid
static bool valid(const std::string &str)
Does the string contain valid characters only?
Definition: stringI.H:132
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
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::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::quotemeta
static string quotemeta(const std::string &str, const char quote='\\')
Return a String with quoted meta-characters from the given string.