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-2021 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  // Constructors
70 
71  //- Default construct
72  SubStrings() = default;
73 
74 
75  // Member Functions
76 
77  //- The total string length of all sub-elements.
78  // Use size() for the number elements.
80  {
82 
83  for (const auto& elem : *this)
84  {
85  len += elem.length();
86  }
87 
88  return len;
89  }
90 
91  //- Retrieve element at pos, converted to a string type.
92  StringType str(size_t pos) const
93  {
94  return (*this)[pos].str();
95  }
96 
97  //- Append sub-string defined by begin/end iterators
98  void append
99  (
100  const typename StringType::const_iterator& b,
101  const typename StringType::const_iterator& e
102  )
103  {
105  range.first = b;
106  range.second = e;
107  range.matched = true;
108 
109  this->push_back(range);
110  }
111 
112  //- Const reference to the first element,
113  //- for consistency with other OpenFOAM containers
114  auto first() const -> decltype(this->front())
115  {
116  return this->front();
117  }
118 
119  //- Const reference to the last element,
120  //- for consistency with other OpenFOAM containers
121  auto last() const -> decltype(this->back())
122  {
123  return this->back();
124  }
125 };
126 
127 
128 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
129 
130 } // End namespace Foam
132 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
133 
134 #endif
135 
136 // ************************************************************************* //
typename std::sub_match< typename StringType::const_iterator > value_type
The element type.
Definition: SubStrings.H:57
auto first() const -> decltype(this->front())
Const reference to the first element, for consistency with other OpenFOAM containers.
Definition: SubStrings.H:122
std::string::size_type length() const
The total string length of all sub-elements.
Definition: SubStrings.H:81
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:96
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:68
auto last() const -> decltype(this->back())
Const reference to the last element, for consistency with other OpenFOAM containers.
Definition: SubStrings.H:131
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:105
typename StringType::const_iterator string_iterator
The const_iterator for the underlying string type.
Definition: SubStrings.H:63
SubStrings()=default
Default construct.
Namespace for OpenFOAM.