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-------------------------------------------------------------------------------
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
26Class
27 Foam::SubStrings
28
29Description
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> // For std::sub_match
39#include <string>
40#include <vector>
41
42// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43
44namespace Foam
45{
46
47/*---------------------------------------------------------------------------*\
48 Class SubStrings Declaration
49\*---------------------------------------------------------------------------*/
50
51template<class StringType>
52class SubStrings
53:
54 public std::vector<std::sub_match<typename StringType::const_iterator>>
55{
56public:
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.
79 std::string::size_type length() const
80 {
81 std::string::size_type len = 0;
82
83 for (const auto& elem : *this)
84 {
85 len += elem.length();
86 }
87
88 return len;
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 //- Const reference to the first element,
107 //- for consistency with other OpenFOAM containers
108 auto first() const -> decltype(this->front())
109 {
110 return this->front();
111 }
112
113 //- Const reference to the last element,
114 //- for consistency with other OpenFOAM containers
115 auto last() const -> decltype(this->back())
116 {
117 return this->back();
118 }
119
120 //- Get element at pos, converted to a string type.
121 StringType str(size_t pos) const
122 {
123 return (*this)[pos].str();
124 }
125};
126
127
128// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
129
130} // End namespace Foam
131
132// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
133
134#endif
135
136// ************************************************************************* //
scalar range
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: SubStrings.H:54
SubStrings()=default
Default construct.
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
std::string::size_type length() const
The total string length of all sub-elements.
Definition: SubStrings.H:78
auto last() const -> decltype(this->back())
Definition: SubStrings.H:114
StringType str(size_t pos) const
Get element at pos, converted to a string type.
Definition: SubStrings.H:120
typename std::sub_match< typename StringType::const_iterator > value_type
The element type.
Definition: SubStrings.H:61
typename StringType::const_iterator string_iterator
The const_iterator for the underlying string type.
Definition: SubStrings.H:65
auto first() const -> decltype(this->front())
Definition: SubStrings.H:107
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
volScalarField & b
Definition: createFields.H:27
volScalarField & e
Definition: createFields.H:11