Loading...
Searching...
No Matches
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-2025 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 Foam_SubStrings_H
36#define Foam_SubStrings_H
37
38#include <regex> // For std::ssub_match
39#include <memory>
40#include <string_view>
41#include <string>
42#include <vector>
43
44// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45
46namespace Foam
47{
48
49/*---------------------------------------------------------------------------*\
50 Class SubStrings Declaration
51\*---------------------------------------------------------------------------*/
52
53class SubStrings
54:
55 public std::vector<std::ssub_match>
56{
57public:
58
59 // Static Functions
60
61 //- Return match as a string_view
62 static std::string_view view(const std::ssub_match& m)
63 {
64 if (!m.matched) return std::string_view();
65
66 #if __cplusplus >= 202002L
67 return std::string_view(m.first, m.second);
68 #else
69 // No string_view construct from iterator pairs before c++20
70 return std::string_view
71 (
72 std::pointer_traits<const char*>::pointer_to(*(m.first)),
73 (m.second - m.first)
74 );
75 #endif
76 }
77
78
79 // Member Functions
80
81 //- The total string length of all sub-elements.
82 // Use size() for the number elements.
83 std::string::size_type length() const
84 {
85 std::string::size_type len = 0;
86
87 for (const auto& elem : *this)
88 {
89 len += elem.length();
90 }
91
92 return len;
93 }
94
95 //- Return length of element at pos
96 std::string::size_type length(size_t pos) const
97 {
98 return (*this)[pos].length();
99 }
100
101 //- Retrieve element at pos, as a string
102 std::string str(size_t pos) const
103 {
104 return (*this)[pos].str();
105 }
106
107 //- Return element at pos as a string_view
108 std::string_view view(size_t pos) const
109 {
110 return view((*this)[pos]);
111 }
113 //- Append sub-string defined by begin/end iterators
114 void append
115 (
116 std::string::const_iterator b,
117 std::string::const_iterator e
118 )
119 {
120 auto& range = this->emplace_back();
121 range.first = b;
122 range.second = e;
123 range.matched = true;
124 }
125
126 //- Reduce size by 1 or more elements. Can be called on an empty list.
127 void pop_back(size_t n = 1)
128 {
129 if (n >= this->size())
130 {
131 this->clear();
132 }
133 else if (n > 0)
134 {
135 this->resize(this->size() - n);
136 }
137 }
138
139 //- Reduce size by 1 or more elements (from the front).
140 //- Can be called on an empty list.
141 void pop_front(size_t n = 1)
142 {
143 if (n >= this->size())
144 {
145 this->clear();
146 }
147 else if (n > 0)
148 {
149 // Overlapping range, avoid std::copy, std::move
150 for (size_t src = n, dst = 0; src < this->size(); ++src, ++dst)
152 (*this)[dst] = (*this)[src];
153 }
154 this->resize(this->size() - n);
155 }
156 }
157};
158
159
160// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161
162} // End namespace Foam
163
164// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
165
166#endif
167
168// ************************************************************************* //
scalar range
label n
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition SubStrings.H:49
void pop_back(size_t n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition SubStrings.H:135
std::string::size_type length(size_t pos) const
Return length of element at pos.
Definition SubStrings.H:96
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:151
std::string::size_type length() const
The total string length of all sub-elements.
Definition SubStrings.H:81
static std::string_view view(const std::ssub_match &m)
Return match as a string_view.
Definition SubStrings.H:57
void append(std::string::const_iterator b, std::string::const_iterator e)
Append sub-string defined by begin/end iterators.
Definition SubStrings.H:121
std::string str(size_t pos) const
Retrieve element at pos, as a string.
Definition SubStrings.H:104
std::string_view view(size_t pos) const
Return element at pos as a string_view.
Definition SubStrings.H:112
patchWriters resize(patchIds.size())
surface1 clear()
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
volScalarField & b
volScalarField & e