Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) | ||
3 | // | ||
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
6 | // | ||
7 | // Official repository: https://github.com/boostorg/url | ||
8 | // | ||
9 | |||
10 | #ifndef BOOST_URL_GRAMMAR_DELIM_RULE_HPP | ||
11 | #define BOOST_URL_GRAMMAR_DELIM_RULE_HPP | ||
12 | |||
13 | #include <boost/url/detail/config.hpp> | ||
14 | #include <boost/core/detail/string_view.hpp> | ||
15 | #include <boost/url/grammar/charset.hpp> | ||
16 | #include <boost/url/grammar/error.hpp> | ||
17 | #include <boost/url/grammar/type_traits.hpp> | ||
18 | #include <type_traits> | ||
19 | |||
20 | namespace boost { | ||
21 | namespace urls { | ||
22 | namespace grammar { | ||
23 | |||
24 | /** Match a character literal | ||
25 | |||
26 | This matches the specified character. | ||
27 | The value is a reference to the character | ||
28 | in the underlying buffer, expressed as a | ||
29 | `core::string_view`. The function @ref squelch | ||
30 | may be used to turn this into `void` instead. | ||
31 | If there is no more input, the error code | ||
32 | @ref error::need_more is returned. | ||
33 | |||
34 | @par Value Type | ||
35 | @code | ||
36 | using value_type = core::string_view; | ||
37 | @endcode | ||
38 | |||
39 | @par Example | ||
40 | Rules are used with the function @ref parse. | ||
41 | @code | ||
42 | system::result< core::string_view > rv = parse( ".", delim_rule('.') ); | ||
43 | @endcode | ||
44 | |||
45 | @par BNF | ||
46 | @code | ||
47 | char = %00-FF | ||
48 | @endcode | ||
49 | |||
50 | @param ch The character to match | ||
51 | |||
52 | @see | ||
53 | @ref parse, | ||
54 | @ref squelch. | ||
55 | */ | ||
56 | #ifdef BOOST_URL_DOCS | ||
57 | constexpr | ||
58 | __implementation_defined__ | ||
59 | delim_rule( char ch ) noexcept; | ||
60 | #else | ||
61 | struct ch_delim_rule | ||
62 | { | ||
63 | using value_type = core::string_view; | ||
64 | |||
65 | constexpr | ||
66 | 11591 | ch_delim_rule(char ch) noexcept | |
67 | 11591 | : ch_(ch) | |
68 | { | ||
69 | 11591 | } | |
70 | |||
71 | BOOST_URL_DECL | ||
72 | system::result<value_type> | ||
73 | parse( | ||
74 | char const*& it, | ||
75 | char const* end) const noexcept; | ||
76 | |||
77 | private: | ||
78 | char ch_; | ||
79 | }; | ||
80 | |||
81 | constexpr | ||
82 | ch_delim_rule | ||
83 | 11591 | delim_rule( char ch ) noexcept | |
84 | { | ||
85 | 11591 | return ch_delim_rule(ch); | |
86 | } | ||
87 | #endif | ||
88 | |||
89 | //------------------------------------------------ | ||
90 | |||
91 | /** Match a single character from a character set | ||
92 | |||
93 | This matches exactly one character which | ||
94 | belongs to the specified character set. | ||
95 | The value is a reference to the character | ||
96 | in the underlying buffer, expressed as a | ||
97 | `core::string_view`. The function @ref squelch | ||
98 | may be used to turn this into `void` instead. | ||
99 | If there is no more input, the error code | ||
100 | @ref error::need_more is returned. | ||
101 | |||
102 | @par Value Type | ||
103 | @code | ||
104 | using value_type = core::string_view; | ||
105 | @endcode | ||
106 | |||
107 | @par Example | ||
108 | Rules are used with the function @ref parse. | ||
109 | @code | ||
110 | system::result< core::string_view > rv = parse( "X", delim_rule( alpha_chars ) ); | ||
111 | @endcode | ||
112 | |||
113 | @param cs The character set to use. | ||
114 | |||
115 | @see | ||
116 | @ref alpha_chars, | ||
117 | @ref parse, | ||
118 | @ref squelch. | ||
119 | */ | ||
120 | #ifdef BOOST_URL_DOCS | ||
121 | template<class CharSet> | ||
122 | constexpr | ||
123 | __implementation_defined__ | ||
124 | delim_rule( CharSet const& cs ) noexcept; | ||
125 | #else | ||
126 | template<class CharSet> | ||
127 | struct cs_delim_rule | ||
128 | { | ||
129 | using value_type = core::string_view; | ||
130 | |||
131 | constexpr | ||
132 | 3 | cs_delim_rule( | |
133 | CharSet const& cs) noexcept | ||
134 | : cs_(cs) | ||
135 | { | ||
136 | 3 | } | |
137 | |||
138 | system::result<value_type> | ||
139 | 877 | parse( | |
140 | char const*& it, | ||
141 | char const* end) const noexcept | ||
142 | { | ||
143 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 876 times.
|
877 | if(it == end) |
144 | { | ||
145 | // end | ||
146 | 1 | BOOST_URL_RETURN_EC( | |
147 | error::need_more); | ||
148 | } | ||
149 |
2/2✓ Branch 1 taken 350 times.
✓ Branch 2 taken 526 times.
|
876 | if(! cs_(*it)) |
150 | { | ||
151 | // wrong character | ||
152 | 350 | BOOST_URL_RETURN_EC( | |
153 | error::mismatch); | ||
154 | } | ||
155 | 1052 | return core::string_view{ | |
156 | 526 | it++, 1 }; | |
157 | } | ||
158 | |||
159 | private: | ||
160 | CharSet cs_; | ||
161 | }; | ||
162 | |||
163 | template<class CharSet> | ||
164 | constexpr | ||
165 | typename std::enable_if< | ||
166 | ! std::is_convertible< | ||
167 | CharSet, char>::value, | ||
168 | cs_delim_rule<CharSet>>::type | ||
169 | 3 | delim_rule( | |
170 | CharSet const& cs) noexcept | ||
171 | { | ||
172 | // If you get a compile error here it | ||
173 | // means that your type does not meet | ||
174 | // the requirements for a CharSet. | ||
175 | // Please consult the documentation. | ||
176 | static_assert( | ||
177 | is_charset<CharSet>::value, | ||
178 | "CharSet requirements not met"); | ||
179 | |||
180 | 3 | return cs_delim_rule<CharSet>(cs); | |
181 | } | ||
182 | #endif | ||
183 | |||
184 | } // grammar | ||
185 | } // urls | ||
186 | } // boost | ||
187 | |||
188 | #endif | ||
189 |