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-2021 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 
62 inline bool Foam::string::hasExt(const char* ending) const
63 {
64  return (ending && string::hasExt(std::string(ending)));
65 }
66 
67 
68 inline bool Foam::string::hasExt(const std::string& ending) const
69 {
70  const auto len = ending.size();
71  auto i = find_ext();
72  if (i == npos || !len)
73  {
74  return false;
75  }
76 
77  ++i; // Compare *after* the dot
78  return ((size() - i) == len) && !compare(i, npos, ending);
79 }
80 
81 
83 {
84  const auto i = rfind('/');
85 
86  if (npos == i)
87  {
88  return false;
89  }
90 
91  erase(0, i+1);
92  return true;
93 }
94 
95 
97 {
98  const auto i = find_ext();
99 
100  if (npos == i)
101  {
102  return false;
103  }
104 
105  erase(i);
106  return true;
107 }
108 
109 
110 
111 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
112 
113 inline Foam::string::string(const std::string& str)
114 :
115  std::string(str)
116 {}
117 
118 
119 inline Foam::string::string(std::string&& str)
120 :
121  std::string(std::move(str))
122 {}
123 
124 
125 inline Foam::string::string(const char* str)
126 :
127  std::string(str)
128 {}
129 
130 
131 inline Foam::string::string(const char* str, const size_type len)
132 :
133  std::string(str, len)
134 {}
135 
136 
137 inline Foam::string::string(const char c)
138 :
139  std::string(1, c)
140 {}
141 
142 
143 inline Foam::string::string(const size_type len, const char c)
144 :
145  std::string(len, c)
146 {}
147 
148 
149 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
150 
151 template<class StringType>
152 inline bool Foam::string::valid(const std::string& str)
153 {
154  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
155  {
156  if (!StringType::valid(*iter))
157  {
158  return false;
159  }
160  }
161 
162  return true;
163 }
164 
165 
166 template<class StringType>
167 inline bool Foam::string::stripInvalid(std::string& str)
168 {
169  if (!string::valid<StringType>(str))
170  {
171  size_type nChar = 0;
172  iterator outIter = str.begin();
173 
174  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
175  {
176  const char c = *iter;
177 
178  if (StringType::valid(c))
179  {
180  *outIter = c;
181  ++outIter;
182  ++nChar;
183  }
184  }
185 
186  str.erase(nChar);
187 
188  return true;
189  }
190 
191  return false;
192 }
193 
194 
195 template<class StringType>
196 inline StringType Foam::string::validate(const std::string& str)
197 {
198  StringType out;
199  out.resize(str.size());
200 
201  size_type len = 0;
202  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
203  {
204  const char c = *iter;
205  if (StringType::valid(c))
206  {
207  out[len] = c;
208  ++len;
209  }
210  }
211 
212  out.erase(len);
213 
214  return out;
215 }
216 
217 
218 inline bool Foam::string::match(const std::string& text) const
219 {
220  return !compare(text); // Always compare as literal string
221 }
222 
223 
224 inline void Foam::string::swap(std::string& str)
225 {
226  if (this != &str)
227  {
228  // Self-swap is a no-op
229  std::string::swap(str);
230  }
231 }
232 
233 
234 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
235 
236 inline bool Foam::string::operator()(const std::string& text) const
237 {
238  return !compare(text); // Always compare as literal string
239 }
240 
241 
242 // ************************************************************************* //
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:96
Foam::string::operator()
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition: stringI.H:236
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
erase
srcOptions erase("case")
Foam::string::match
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:218
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:152
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:82
Foam::string::stripInvalid
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition: stringI.H:167
Foam::string::string
string()=default
Default construct.
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:224
Foam::string::validate
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition: stringI.H:196