SubStrings.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) 2017-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 Class
27  Foam::SubStrings
28 
29 Description
30  Sub-ranges of a string with a structure similar to std::match_results,
31  but without the underlying regular expression matching.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Foam_SubStrings_H
36 #define Foam_SubStrings_H
37 
38 #include <regex> // For std::sub_match
39 #include <string>
40 #include <vector>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class SubStrings Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 template<class StringType>
52 class SubStrings
53 :
54  public std::vector<std::sub_match<typename StringType::const_iterator>>
55 {
56 public:
57 
58  // Types
59 
60  //- The element type
61  using value_type =
62  typename std::sub_match<typename StringType::const_iterator>;
63 
64  //- The const_iterator for the underlying string type
65  using string_iterator =
66  typename StringType::const_iterator;
67 
68 
69  // Member Functions
70 
71  //- The total string length of all sub-elements.
72  // Use size() for the number elements.
74  {
75  std::string::size_type len = 0;
76 
77  for (const auto& elem : *this)
78  {
79  len += elem.length();
80  }
81 
82  return len;
83  }
84 
85  //- Retrieve element at pos, converted to a string type.
86  StringType str(size_t pos) const
87  {
88  return (*this)[pos].str();
89  }
90 
91  //- Append sub-string defined by begin/end iterators
92  void append
93  (
94  const typename StringType::const_iterator& b,
95  const typename StringType::const_iterator& e
96  )
97  {
99  range.first = b;
100  range.second = e;
101  range.matched = true;
102 
103  this->push_back(range);
104  }
105 
106 
107  //- Reduce size by 1 or more elements. Can be called on an empty list.
108  void pop_back(size_t n = 1)
109  {
110  if (n >= this->size())
111  {
112  this->clear();
113  }
114  else if (n > 0)
115  {
116  this->resize(this->size() - n);
117  }
118  }
119 
120  //- Reduce size by 1 or more elements (from the front).
121  //- Can be called on an empty list.
122  void pop_front(size_t n = 1)
123  {
124  if (n >= this->size())
125  {
126  this->clear();
127  }
128  else if (n > 0)
129  {
130  // Overlapping range, avoid std::copy, std::move
131  for (size_t src = n, dst = 0; src < this->size(); ++src, ++dst)
132  {
133  (*this)[dst] = (*this)[src];
134  }
135  this->resize(this->size() - n);
136  }
137  }
138 
139 
140  // FUTURE?
141  // #if __cplusplus >= 201703L
142  // std::string_view view(size_t pos) const
143  // {}
144  // #else
145  // stdFoam::span<const char> view(size_t pos) const
146  // {}
147  // #endif
148 };
149 
150 
151 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
152 
153 } // End namespace Foam
154 
155 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
156 
157 #endif
158 
159 // ************************************************************************* //
patchWriters resize(patchIds.size())
typename std::sub_match< typename StringType::const_iterator > value_type
The element type.
Definition: SubStrings.H:57
void pop_back(size_t n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: SubStrings.H:114
std::string::size_type length() const
The total string length of all sub-elements.
Definition: SubStrings.H:73
scalar range
dimensionedScalar pos(const dimensionedScalar &ds)
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
StringType str(size_t pos) const
Retrieve element at pos, converted to a string type.
Definition: SubStrings.H:88
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Vector< scalar > vector
Definition: vector.H:57
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
surface1 clear()
typename StringType::const_iterator string_iterator
The const_iterator for the underlying string type.
Definition: SubStrings.H:63
label n
void pop_front(size_t n=1)
Reduce size by 1 or more elements (from the front). Can be called on an empty list.
Definition: SubStrings.H:130
Namespace for OpenFOAM.