stringOpsTemplates.C
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) 2016-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
29 
30 template<class StringType, class UnaryPredicate>
32 (
33  const StringType& str,
34  const UnaryPredicate& meta,
35  const char quote
36 )
37 {
38  if (str.empty() || !quote)
39  {
40  return str;
41  }
42 
43  StringType result;
44  result.reserve(1.5*str.size()); // Moderately pessimistic
45 
46  bool escaped = false;
47  for (const char c : str)
48  {
49  if (c == quote)
50  {
51  escaped = !escaped; // toggle state
52  }
53  else if (escaped)
54  {
55  escaped = false;
56  }
57  else if (meta(c))
58  {
59  result += quote;
60  }
61  result += c;
62  }
63 
64  result.shrink_to_fit();
65  return result;
66 }
67 
68 
69 template<class StringType, class UnaryPredicate>
71 (
72  const std::string& str,
73  const UnaryPredicate& accept,
74  const bool invert
75 )
76 {
77  StringType out;
78  out.resize(str.length());
79 
80  std::string::size_type len = 0;
81 
82  for (std::string::size_type i = 0; i < str.length(); ++i)
83  {
84  const char c = str[i];
85  if (accept(c) ? !invert : invert)
86  {
87  out[len++] += c;
88  }
89  }
90 
91  out.erase(len);
92  return out;
93 }
94 
95 
96 template<class StringType>
98 (
99  const StringType& str,
100  const char delim,
102  const bool keepEmpty
103 )
104 {
106 
107  if
108  (
109  !delim
110  || (pos == std::string::npos || pos >= str.size())
111  )
112  {
113  return list;
114  }
115 
116  list.reserve(20);
117 
119  while ((end = str.find(delim, pos)) != std::string::npos)
120  {
121  if (keepEmpty || (pos < end))
122  {
123  list.append(str.cbegin() + pos, str.cbegin() + end);
124  }
125  pos = end + 1;
126  }
127 
128  // Trailing element
129  if (keepEmpty ? (pos <= str.size()) : (pos < str.size()))
130  {
131  list.append(str.cbegin() + pos, str.cend());
132  }
134  return list;
135 }
136 
137 
138 template<class StringType>
140 (
141  const StringType& str,
142  const std::string& delim,
144  const bool keepEmpty
145 )
146 {
148 
149  if
150  (
151  delim.empty()
152  || (pos == std::string::npos || pos >= str.size())
153  )
154  {
155  return list;
156  }
157 
158  list.reserve(20);
159 
161  while ((end = str.find(delim, pos)) != std::string::npos)
162  {
163  if (keepEmpty || (pos < end))
164  {
165  list.append(str.cbegin() + pos, str.cbegin() + end);
166  }
167  pos = end + delim.size();
168  }
169 
170  // Trailing element
171  if (keepEmpty ? (pos <= str.size()) : (pos < str.size()))
172  {
173  list.append(str.cbegin() + pos, str.cend());
174  }
176  return list;
177 }
178 
179 
180 template<class StringType>
182 (
183  const StringType& str,
184  const std::string& delim,
186 )
187 {
189 
190  if
191  (
192  delim.empty()
193  || (pos == std::string::npos || pos >= str.size())
194  )
195  {
196  return list;
197  }
198 
199  list.reserve(20);
200 
201  while ((pos = str.find_first_not_of(delim, pos)) != std::string::npos)
202  {
203  const auto end = str.find_first_of(delim, pos);
204 
205  if (end == std::string::npos)
206  {
207  // Trailing element
208  list.append(str.cbegin() + pos, str.cend());
209  break;
210  }
211 
212  // Intermediate element
213  list.append(str.cbegin() + pos, str.cbegin() + end);
214 
215  pos = end + 1;
216  }
218  return list;
219 }
220 
221 
222 template<class StringType>
224 (
225  const StringType& str,
226  const std::string::size_type width,
228 )
229 {
231 
232  if
233  (
234  !width
235  || (pos == std::string::npos || pos >= str.size())
236  )
237  {
238  return list;
239  }
240 
241  list.reserve(1 + ((str.size() - pos) / width));
242 
243  const auto len = str.size();
244 
245  while (pos < len)
246  {
247  const auto end = (pos + width);
248 
249  if (end >= len)
250  {
251  // Trailing element
252  list.append(str.cbegin() + pos, str.cend());
253  break;
254  }
255 
256  // Intermediate element
257  list.append(str.cbegin() + pos, str.cbegin() + end);
258 
259  pos += width;
260  }
262  return list;
263 }
264 
265 
266 template<class StringType>
268 (
269  const StringType& str,
271 )
272 {
273  return splitAny(str, "\t\n\v\f\r ", pos);
274 }
275 
276 
277 // ************************************************************************* //
Foam::SubStrings< StringType > split(const StringType &str, const char delim, std::string::size_type pos=0, const bool keepEmpty=false)
Split string into sub-strings at the delimiter character.
Foam::SubStrings< StringType > splitFixed(const StringType &str, const std::string::size_type width, std::string::size_type pos=0)
Split string into sub-strings using a fixed field width.
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: CStringList.H:60
Foam::SubStrings< StringType > splitAny(const StringType &str, const std::string &delim, std::string::size_type pos=0)
Split string into sub-strings using any characters in delimiter.
StringType quotemeta(const StringType &str, const UnaryPredicate &meta, const char quote='\\')
Quote any meta-characters in given string.
StringType validate(const std::string &str, const UnaryPredicate &accept, const bool invert=false)
Return a copy of the input string with validated characters.
dimensionedScalar pos(const dimensionedScalar &ds)
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:67
void append(const typename StringType::const_iterator &b, const typename StringType::const_iterator &e)
Append sub-string defined by begin/end iterators.
Definition: SubStrings.H:97
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:201
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:30
const dimensionedScalar c
Speed of light in a vacuum.
Foam::SubStrings< StringType > splitSpace(const StringType &str, std::string::size_type pos=0)
Split string into sub-strings at whitespace (TAB, NL, VT, FF, CR, SPC)