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-2018 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>
32 (
33  const StringType& str,
34  const char delim,
35  const bool keepEmpty
36 )
37 {
39  if (str.empty() || !delim)
40  {
41  return lst;
42  }
43 
44  lst.reserve(20);
45 
46  std::string::size_type beg = 0, end = 0;
47  while ((end = str.find(delim, beg)) != std::string::npos)
48  {
49  if (keepEmpty || (beg < end))
50  {
51  lst.append(str.cbegin() + beg, str.cbegin() + end);
52  }
53  beg = end + 1;
54  }
55 
56  // Trailing element
57  if (keepEmpty ? (beg == str.size()) : (beg < str.size()))
58  {
59  lst.append(str.cbegin() + beg, str.cend());
60  }
61 
62  return lst;
63 }
64 
65 
66 template<class StringType>
68 (
69  const StringType& str,
70  const std::string& delim,
71  const bool keepEmpty
72 )
73 {
75  if (str.empty() || delim.empty())
76  {
77  return lst;
78  }
79 
80  lst.reserve(20);
81 
82  std::string::size_type beg = 0, end = 0;
83  while ((end = str.find(delim, beg)) != std::string::npos)
84  {
85  if (keepEmpty || (beg < end))
86  {
87  lst.append(str.cbegin() + beg, str.cbegin() + end);
88  }
89  beg = end + delim.size();
90  }
91 
92  // Trailing element
93  if (keepEmpty ? (beg == str.size()) : (beg < str.size()))
94  {
95  lst.append(str.cbegin() + beg, str.cend());
96  }
97 
98  return lst;
99 }
100 
101 
102 template<class StringType>
104 (
105  const StringType& str,
106  const std::string& delim
107 )
108 {
110  if (str.empty() || delim.empty())
111  {
112  return lst;
113  }
114 
115  lst.reserve(20);
116 
117  for
118  (
120  (pos = str.find_first_not_of(delim, pos)) != std::string::npos;
121  /*nil*/
122  )
123  {
124  const auto end = str.find_first_of(delim, pos);
125 
126  if (end == std::string::npos)
127  {
128  // Trailing element
129  lst.append(str.cbegin() + pos, str.cend());
130  break;
131  }
132 
133  // Intermediate element
134  lst.append(str.cbegin() + pos, str.cbegin() + end);
135 
136  pos = end + 1;
137  }
138 
139  return lst;
140 }
141 
142 
143 template<class StringType>
145 (
146  const StringType& str,
147  const std::string::size_type width,
148  const std::string::size_type start
149 )
150 {
152  if (str.empty() || !width)
153  {
154  return lst;
155  }
156 
157  const auto len = str.size();
158  lst.reserve(1 + (len / width));
159 
160  for (std::string::size_type pos = start; pos < len; pos += width)
161  {
162  const auto end = (pos + width);
163 
164  if (end >= len)
165  {
166  // Trailing element
167  lst.append(str.cbegin() + pos, str.cend());
168  break;
169  }
170 
171  lst.append(str.cbegin() + pos, str.cbegin() + end);
172  }
173 
174  return lst;
175 }
176 
177 
178 template<class StringType>
180 (
181  const StringType& str
182 )
183 {
184  return splitAny(str, "\t\n\v\f\r ");
185 }
186 
187 
188 // ************************************************************************* //
Foam::stringOps::split
Foam::SubStrings< StringType > split(const StringType &str, const char delim, const bool keepEmpty=false)
Split string into sub-strings at the delimiter character.
Definition: stringOpsTemplates.C:32
Foam::stringOps::splitSpace
Foam::SubStrings< StringType > splitSpace(const StringType &str)
Split string into sub-strings at whitespace (TAB, NL, VT, FF, CR, SPC)
Definition: stringOpsTemplates.C:180
Foam::SubStrings
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: CStringList.H:63
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::stringOps::splitAny
Foam::SubStrings< StringType > splitAny(const StringType &str, const std::string &delim)
Split string into sub-strings using any characters in delimiter.
Definition: stringOpsTemplates.C:104
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::stringOps::splitFixed
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.
Definition: stringOpsTemplates.C:145
Foam::SubStrings::append
void append(const typename String::const_iterator &b, const typename String::const_iterator &e)
Append sub-string defined by begin/end iterators.
Definition: SubStrings.H:92
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177