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