-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatch_view.hh
59 lines (46 loc) · 1.32 KB
/
match_view.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Match view
#pragma once
#include "snn-core/array_view.hh"
#include "snn-core/math/common.hh"
#include <regex>
namespace snn::regex
{
// ## Classes
// ### match_view
class match_view final
{
public:
[[nodiscard]] usize count() const
{
return math::within_bounds_or<usize>(matches_.length(index_), 0);
}
[[nodiscard]] usize position() const
{
return math::within_bounds_or<usize>(matches_.position(index_), 0);
}
[[nodiscard]] cstrview view() const
{
const std::csub_match& sub = matches_[index_];
snn_should(sub.matched);
return cstrview{meta::iterators, sub.first, sub.second};
}
[[nodiscard]] usize size() const
{
return count();
}
private:
const std::cmatch& matches_;
usize index_;
template <typename>
friend class matches_base;
friend class matches_iterator;
explicit match_view(const std::cmatch& matches, const usize index) noexcept
: matches_{matches},
index_{index}
{
snn_should(index_ < matches_.size());
}
};
}