pointer.ipp
6.58 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// Copyright (c) 2022 Dmitry Arkhipov (grisumbras@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//
#ifndef BOOST_JSON_IMPL_POINTER_IPP
#define BOOST_JSON_IMPL_POINTER_IPP
#include <boost/json/value.hpp>
BOOST_JSON_NS_BEGIN
namespace detail {
class pointer_token
{
public:
class iterator;
pointer_token(
char const* b,
char const* e) noexcept
: b_(b)
, e_(e)
{
}
iterator begin() const noexcept;
iterator end() const noexcept;
private:
char const* b_;
char const* e_;
};
class pointer_token::iterator
{
public:
explicit iterator(char const* base) noexcept
: base_(base)
{
}
char operator*() const noexcept
{
switch( char c = *base_ )
{
case '~':
c = base_[1];
if( '0' == c )
return '~';
BOOST_ASSERT('1' == c);
return '/';
default:
return c;
}
}
iterator& operator++() noexcept
{
if( '~' == *base_ )
base_ += 2;
else
++base_;
return *this;
}
char const* base() const noexcept
{
return base_;
}
private:
char const* base_;
};
bool operator==(pointer_token::iterator l, pointer_token::iterator r) noexcept
{
return l.base() == r.base();
}
bool operator!=(pointer_token::iterator l, pointer_token::iterator r) noexcept
{
return l.base() != r.base();
}
pointer_token::iterator pointer_token::begin() const noexcept
{
return iterator(b_);
}
pointer_token::iterator pointer_token::end() const noexcept
{
return iterator(e_);
}
bool operator==(pointer_token token, string_view sv) noexcept
{
auto t_b = token.begin();
auto const t_e = token.end();
auto s_b = sv.begin();
auto const s_e = sv.end();
while( s_b != s_e )
{
if( t_e == t_b )
return false;
if( *t_b != *s_b )
return false;
++t_b;
++s_b;
}
return t_b == t_e;
}
bool is_invalid_zero(
char const* b,
char const* e) noexcept
{
// in JSON Pointer only zero index can start character '0'
if( *b != '0' )
return false;
// if an index token starts with '0', then it should not have any more
// characters: either the string should end, or new token should start
++b;
if( b == e )
return false;
return *b != '/';
}
bool is_past_the_end_token(
char const* b,
char const* e) noexcept
{
if( *b != '-' )
return false;
++b;
if( b == e )
return true;
return *b == '/';
}
std::size_t
parse_number_token(
char const*& b,
char const* e,
error_code& ec) noexcept
{
if( ( b == e )
|| is_invalid_zero(b, e) )
{
BOOST_JSON_FAIL(ec, error::token_not_number);
return {};
}
if( is_past_the_end_token(b, e) )
{
BOOST_JSON_FAIL(ec, error::past_the_end);
return {};
}
std::size_t result = 0;
for( ; b != e; ++b )
{
char const c = *b;
if( '/' == c)
break;
unsigned d = c - '0';
if( d > 9 )
{
BOOST_JSON_FAIL(ec, error::token_not_number);
return {};
}
std::size_t new_result = result * 10 + d;
if( new_result < result )
{
BOOST_JSON_FAIL(ec, error::token_overflow);
return {};
}
result = new_result;
}
return result;
}
pointer_token
get_token(
char const* b,
char const* e,
error_code& ec) noexcept
{
char const* start = b;
for( ; b < e; ++b )
{
char const c = *b;
if( '/' == c )
break;
if( '~' == c )
{
if( ++b == e )
{
BOOST_JSON_FAIL(ec, error::invalid_escape);
break;
}
switch (*b)
{
case '0': // fall through
case '1':
// valid escape sequence
continue;
default: {
BOOST_JSON_FAIL(ec, error::invalid_escape);
break;
}
}
break;
}
}
return pointer_token(start, b);
}
value*
if_contains_token(object const& obj, pointer_token token)
{
if( obj.empty() )
return nullptr;
auto const it = detail::find_in_object(obj, token).first;
if( !it )
return nullptr;
return &it->value();
}
} // namespace detail
value const&
value::at_pointer(string_view ptr) const&
{
error_code ec;
auto const found = find_pointer(ptr, ec);
if( !found )
detail::throw_system_error(ec, BOOST_CURRENT_LOCATION);
return *found;
}
value const*
value::find_pointer(string_view ptr, error_code& ec) const noexcept
{
ec.clear();
value const* result = this;
char const* cur = ptr.data();
char const* const end = cur + ptr.size();
while( end != cur )
{
if( *cur++ != '/' )
{
BOOST_JSON_FAIL(ec, error::missing_slash);
return nullptr;
}
if( auto const po = result->if_object() )
{
auto const token = detail::get_token(cur, end, ec);
if( ec )
return nullptr;
result = detail::if_contains_token(*po, token);
cur = token.end().base();
}
else if( auto const pa = result->if_array() )
{
auto const index = detail::parse_number_token(cur, end, ec);
if( ec )
return nullptr;
result = pa->if_contains(index);
}
else
{
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return nullptr;
}
if( !result )
{
BOOST_JSON_FAIL(ec, error::not_found);
return nullptr;
}
}
BOOST_ASSERT(result);
return result;
}
value*
value::find_pointer(string_view ptr, error_code& ec) noexcept
{
value const& self = *this;
return const_cast<value*>(self.find_pointer(ptr, ec));
}
value const*
value::find_pointer(string_view ptr, std::error_code& ec) const noexcept
{
error_code jec;
value const* result = find_pointer(ptr, jec);
ec = jec;
return result;
}
value*
value::find_pointer(string_view ptr, std::error_code& ec) noexcept
{
value const& self = *this;
return const_cast<value*>(self.find_pointer(ptr, ec));
}
BOOST_JSON_NS_END
#endif // BOOST_JSON_IMPL_POINTER_IPP