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-2020 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 SubStrings_H
36 #define SubStrings_H
37 
38 #include <regex>
39 #include <string>
40 #include <vector>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class SubStrings Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 template<class String>
52 class SubStrings
53 :
54  public std::vector<std::sub_match<typename String::const_iterator>>
55 {
56 public:
57 
58  // Typedefs
59 
60  //- The element type
61  using value_type =
62  typename std::sub_match<typename String::const_iterator>;
63 
64  //- The const_iterator for the underlying string type
65  using string_iterator = typename String::const_iterator;
66 
67 
68  // Constructors
69 
70  //- Default construct
71  SubStrings() = default;
72 
73 
74  // Member Functions
75 
76  //- The total string length of all sub-elements.
77  // Use size() for the number elements.
79  {
80  std::string::size_type len = 0;
81 
82  for (const auto& elem : *this)
83  {
84  len += elem.length();
85  }
86 
87  return len;
88  }
89 
90 
91  //- Append sub-string defined by begin/end iterators
92  void append
93  (
94  const typename String::const_iterator& b,
95  const typename String::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  //- Const reference to the first element,
108  //- for consistency with other OpenFOAM containers
109  auto first() const -> decltype(this->front())
110  {
111  return this->front();
112  }
113 
114 
115  //- Const reference to the last element,
116  //- for consistency with other OpenFOAM containers
117  auto last() const -> decltype(this->back())
118  {
119  return this->back();
120  }
121 
122 
123  //- Get element pos, converted to a string type.
124  String str(size_t pos) const
125  {
126  return (*this)[pos].str();
127  }
128 };
129 
130 
131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
132 
133 } // End namespace Foam
134 
135 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
136 
137 #endif
138 
139 // ************************************************************************* //
Foam::SubStrings::str
String str(size_t pos) const
Get element pos, converted to a string type.
Definition: SubStrings.H:123
Foam::SubStrings::length
std::string::size_type length() const
The total string length of all sub-elements.
Definition: SubStrings.H:77
Foam::SubStrings::value_type
typename std::sub_match< typename String::const_iterator > value_type
The element type.
Definition: SubStrings.H:61
Foam::SubStrings::last
auto last() const -> decltype(this->back())
Definition: SubStrings.H:116
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::SubStrings::SubStrings
SubStrings()=default
Default construct.
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:51
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::SubStrings::first
auto first() const -> decltype(this->front())
Definition: SubStrings.H:108
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::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::SubStrings::string_iterator
typename String::const_iterator string_iterator
The const_iterator for the underlying string type.
Definition: SubStrings.H:64
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177