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-2023 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::has_ext() const
51 {
52  return (std::string::npos != find_ext());
53 }
54 
55 
56 inline bool Foam::string::has_ext(const char* ending) const
57 {
58  const auto n1 = size();
59  const auto n2 = strlen(ending);
60 
61  // Like ends_with with extra check for dot
62  return
63  (
64  n1 > n2
65  && operator[](n1-n2-1) == '.' // Require a dot separator
66  && !compare(n1-n2, npos, ending, n2)
67  );
68 }
69 
70 
71 inline bool Foam::string::has_ext(const std::string& ending) const
72 {
73  const auto n1 = size();
74  const auto n2 = ending.size();
75 
76  // Like ends_with with extra check for dot
77  return
78  (
79  n1 > n2
80  && operator[](n1-n2-1) == '.' // Require a dot separator
81  && !compare(n1-n2, npos, ending)
82  );
83 }
84 
85 
86 inline bool Foam::string::remove_path()
87 {
88  const auto i = rfind('/');
89 
90  if (npos == i)
91  {
92  return false;
93  }
94 
95  erase(0, i+1);
96  return true;
97 }
98 
99 
100 inline bool Foam::string::remove_ext()
101 {
102  const auto i = find_ext();
103 
104  if (npos == i)
105  {
106  return false;
107  }
108 
109  erase(i);
110  return true;
111 }
112 
113 
114 
115 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
117 inline Foam::string::string(const std::string& str)
118 :
119  std::string(str)
120 {}
121 
123 inline Foam::string::string(std::string&& str)
124 :
125  std::string(std::move(str))
126 {}
127 
129 inline Foam::string::string(const char* str)
130 :
131  std::string(str)
132 {}
133 
135 inline Foam::string::string(const char* str, const size_type len)
136 :
137  std::string(str, len)
138 {}
139 
141 inline Foam::string::string(const char c)
142 :
143  std::string(1, c)
144 {}
145 
146 
147 inline Foam::string::string(const size_type len, const char c)
148 :
149  std::string(len, c)
150 {}
151 
152 
153 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
154 
155 template<class StringType>
156 inline bool Foam::string::valid(const std::string& str)
157 {
158  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
159  {
160  if (!StringType::valid(*iter))
161  {
162  return false;
163  }
164  }
165 
166  return true;
167 }
168 
169 
170 template<class StringType>
171 inline bool Foam::string::stripInvalid(std::string& str)
172 {
173  if (!string::valid<StringType>(str))
174  {
175  size_type nChar = 0;
176  iterator outIter = str.begin();
177 
178  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
179  {
180  const char c = *iter;
181 
182  if (StringType::valid(c))
183  {
184  *outIter = c;
185  ++outIter;
186  ++nChar;
187  }
188  }
189 
190  str.erase(nChar);
191 
192  return true;
193  }
194 
195  return false;
196 }
197 
198 
199 template<class StringType>
200 inline StringType Foam::string::validate(const std::string& str)
201 {
202  StringType out;
203  out.resize(str.size());
204 
205  size_type len = 0;
206  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
207  {
208  const char c = *iter;
209  if (StringType::valid(c))
210  {
211  out[len] = c;
212  ++len;
213  }
214  }
215 
216  out.erase(len);
218  return out;
219 }
220 
221 
222 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
224 inline bool Foam::string::match(const std::string& text) const
225 {
226  return !compare(text); // Always compare as literal string
227 }
228 
229 
230 inline void Foam::string::swap(std::string& str)
231 {
232  if (this != &str)
233  {
234  // Self-swap is a no-op
235  std::string::swap(str);
236  }
237 }
238 
239 
240 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
241 
242 inline bool Foam::string::operator()(const std::string& text) const
243 {
244  return !compare(text); // Always compare as literal string
245 }
246 
247 
248 // ************************************************************************* //
std::string::size_type find_ext() const
Find position of a file extension dot, return npos on failure.
Definition: stringI.H:37
srcOptions erase("case")
bool operator()(const std::string &text) const
Test for equality. Allows use as a predicate.
Definition: stringI.H:235
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:217
bool has_ext() const
Return true if it has an extension or simply ends with a &#39;.&#39;.
Definition: stringI.H:43
static bool valid(const std::string &str)
Does the string contain valid characters only?
Definition: stringI.H:149
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:68
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition: stringI.H:164
bool remove_path()
Remove leading path, return true if string changed.
Definition: stringI.H:79
const dimensionedScalar c
Speed of light in a vacuum.
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:223
A class for handling character strings derived from std::string.
Definition: string.H:72
string()=default
Default construct.
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition: stringI.H:193
bool remove_ext()
Remove extension, return true if string changed.
Definition: stringI.H:93