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-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
30template<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
69template<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
96template<class StringType>
98(
99 const StringType& str,
100 const char delim,
101 const bool keepEmpty
102)
103{
105 if (str.empty() || !delim)
106 {
107 return lst;
108 }
109
110 lst.reserve(20);
111
112 std::string::size_type beg = 0, end = 0;
113 while ((end = str.find(delim, beg)) != std::string::npos)
114 {
115 if (keepEmpty || (beg < end))
116 {
117 lst.append(str.cbegin() + beg, str.cbegin() + end);
118 }
119 beg = end + 1;
120 }
121
122 // Trailing element
123 if (keepEmpty ? (beg == str.size()) : (beg < str.size()))
124 {
125 lst.append(str.cbegin() + beg, str.cend());
126 }
127
128 return lst;
129}
130
131
132template<class StringType>
134(
135 const StringType& str,
136 const std::string& delim,
137 const bool keepEmpty
138)
139{
141 if (str.empty() || delim.empty())
142 {
143 return lst;
144 }
145
146 lst.reserve(20);
147
148 std::string::size_type beg = 0, end = 0;
149 while ((end = str.find(delim, beg)) != std::string::npos)
150 {
151 if (keepEmpty || (beg < end))
152 {
153 lst.append(str.cbegin() + beg, str.cbegin() + end);
154 }
155 beg = end + delim.size();
156 }
157
158 // Trailing element
159 if (keepEmpty ? (beg == str.size()) : (beg < str.size()))
160 {
161 lst.append(str.cbegin() + beg, str.cend());
162 }
163
164 return lst;
165}
166
167
168template<class StringType>
170(
171 const StringType& str,
172 const std::string& delim
173)
174{
176 if (str.empty() || delim.empty())
177 {
178 return lst;
179 }
180
181 lst.reserve(20);
182
183 for
184 (
185 std::string::size_type pos = 0;
186 (pos = str.find_first_not_of(delim, pos)) != std::string::npos;
187 /*nil*/
188 )
189 {
190 const auto end = str.find_first_of(delim, pos);
191
192 if (end == std::string::npos)
193 {
194 // Trailing element
195 lst.append(str.cbegin() + pos, str.cend());
196 break;
197 }
198
199 // Intermediate element
200 lst.append(str.cbegin() + pos, str.cbegin() + end);
201
202 pos = end + 1;
203 }
204
205 return lst;
206}
207
208
209template<class StringType>
211(
212 const StringType& str,
213 const std::string::size_type width,
214 const std::string::size_type start
215)
216{
218 if (str.empty() || !width)
219 {
220 return lst;
221 }
222
223 const auto len = str.size();
224 lst.reserve(1 + (len / width));
225
226 for (std::string::size_type pos = start; pos < len; pos += width)
227 {
228 const auto end = (pos + width);
229
230 if (end >= len)
231 {
232 // Trailing element
233 lst.append(str.cbegin() + pos, str.cend());
234 break;
235 }
236
237 lst.append(str.cbegin() + pos, str.cbegin() + end);
238 }
239
240 return lst;
241}
242
243
244template<class StringType>
246(
247 const StringType& str
248)
249{
250 return splitAny(str, "\t\n\v\f\r ");
251}
252
253
254// ************************************************************************* //
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: SubStrings.H:54
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:92
StringType quotemeta(const StringType &str, const UnaryPredicate &meta, const char quote='\\')
Quote any meta-characters in given string.
Foam::SubStrings< StringType > splitFixed(const StringType &str, const std::string::size_type width, const std::string::size_type start=0)
Split string into sub-strings using a fixed field width.
Foam::SubStrings< StringType > splitSpace(const StringType &str)
Split string into sub-strings at whitespace (TAB, NL, VT, FF, CR, SPC)
StringType validate(const std::string &str, const UnaryPredicate &accept, const bool invert=false)
Return a copy of the input string with validated characters.
Foam::SubStrings< StringType > splitAny(const StringType &str, const std::string &delim)
Split string into sub-strings using any characters in delimiter.
Foam::SubStrings< StringType > split(const StringType &str, const char delim, const bool keepEmpty=false)
Split string into sub-strings at the delimiter character.
dimensionedScalar pos(const dimensionedScalar &ds)
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36