-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnamespace_iterator.h
215 lines (176 loc) · 5 KB
/
namespace_iterator.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
#include <iterator>
#include <string_view>
#include "impl/base.h"
struct namespace_iterator
{
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = std::string_view;
using reference = value_type;
// Proxy type for operator->
struct pointer
{
std::string_view value;
std::string_view* operator->() { return &value; }
};
constexpr namespace_iterator(std::string_view ns, std::string_view::size_type index) noexcept :
m_fullNamespace(ns),
m_index(index)
{
if (m_index != std::string_view::npos)
{
initialize_current();
}
}
constexpr reference operator*() const noexcept
{
XLANG_ASSERT(valid());
return m_currentNamespace;
}
constexpr pointer operator->() const noexcept
{
XLANG_ASSERT(valid());
return pointer{ m_currentNamespace };
}
constexpr namespace_iterator& operator++() noexcept
{
advance_forward();
return *this;
}
constexpr namespace_iterator operator++(int) noexcept
{
auto copy = *this;
advance_forward();
return copy;
}
constexpr namespace_iterator& operator--() noexcept
{
advance_backward();
return *this;
}
constexpr namespace_iterator operator--(int) noexcept
{
auto copy = *this;
advance_backward();
return copy;
}
friend constexpr bool operator==(const namespace_iterator& lhs, const namespace_iterator& rhs) noexcept
{
assert(lhs.m_fullNamespace.data() == rhs.m_fullNamespace.data());
return lhs.m_index == rhs.m_index;
}
friend constexpr bool operator!=(const namespace_iterator& lhs, const namespace_iterator& rhs) noexcept
{
return !(lhs == rhs);
}
private:
constexpr bool valid() const noexcept
{
return m_index != std::string_view::npos;
}
constexpr void initialize_current() noexcept
{
XLANG_ASSERT(valid() && (m_index < m_fullNamespace.length()));
auto pos = m_fullNamespace.find_first_of('.', m_index);
auto length = pos - m_index;
XLANG_ASSERT(length > 0);
m_currentNamespace = m_fullNamespace.substr(m_index, length);
}
constexpr void advance_forward() noexcept
{
XLANG_ASSERT(valid());
m_index += m_currentNamespace.length();
if (m_index >= m_fullNamespace.length())
{
m_index = std::string_view::npos;
m_currentNamespace = {};
}
else
{
++m_index; // Move past the '.'
initialize_current();
}
}
constexpr void advance_backward() noexcept
{
XLANG_ASSERT(m_index != 0);
auto endPos = m_index;
auto pos = m_fullNamespace.find_last_of('.', m_index - 2);
if (pos == std::string_view::npos)
{
// We've hit the beginning
m_index = 0;
}
else
{
m_index = pos + 1; // 'pos' is the index of the period
}
m_currentNamespace = m_fullNamespace.substr(m_index, endPos - m_index - 1);
}
std::string_view m_fullNamespace;
std::string_view m_currentNamespace;
std::string_view::size_type m_index;
};
struct namespace_range
{
using const_iterator = namespace_iterator;
using iterator = const_iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
std::string_view ns;
constexpr const_iterator begin() const noexcept
{
return const_iterator{ ns, 0 };
}
constexpr const_iterator cbegin() const noexcept
{
return begin();
}
constexpr const_iterator end() const noexcept
{
return const_iterator{ ns, std::string_view::npos };
}
constexpr const_iterator cend() const noexcept
{
return end();
}
constexpr const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator{ end() };
}
constexpr const_reverse_iterator crbegin() const noexcept
{
return rbegin();
}
constexpr const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator{ begin() };
}
constexpr const_reverse_iterator crend() const noexcept
{
return rend();
}
};
struct reverse_namespace_range
{
using const_iterator = std::reverse_iterator<namespace_iterator>;
using iterator = const_iterator;
std::string_view ns;
constexpr const_iterator begin() const noexcept
{
return const_iterator{ namespace_iterator{ ns, std::string_view::npos } };
}
constexpr const_iterator cbegin() const noexcept
{
return begin();
}
constexpr const_iterator end() const noexcept
{
return const_iterator{ namespace_iterator{ ns, 0 } };
}
constexpr const_iterator cend() const noexcept
{
return end();
}
};