src/detail/pct_format.cpp

100.0% Lines (80/80) 100.0% List of functions (2/2)
pct_format.cpp
f(x) Functions (2)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.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
11 #include <boost/url/detail/config.hpp>
12 #include "pct_format.hpp"
13 #include <boost/url/grammar/parse.hpp>
14 #include <boost/url/grammar/unsigned_rule.hpp>
15
16 namespace boost {
17 namespace urls {
18 namespace detail {
19
20 std::size_t
21 301x pct_vmeasure(
22 grammar::lut_chars const& cs,
23 format_parse_context& pctx,
24 measure_context& mctx)
25 {
26 301x auto it0 = pctx.begin();
27 301x auto end = pctx.end();
28 589x while( it0 != end )
29 {
30 // look for replacement id
31 354x char const* it1 = it0;
32 354x while(
33 912x it1 != end &&
34 848x *it1 != '{' )
35 {
36 558x ++it1;
37 }
38
39 // output literal prefix
40 354x if( it0 != it1 )
41 {
42 701x for (char const* i = it0; i != it1; ++i)
43 558x mctx.advance_to( mctx.out() + measure_one(*i, cs));
44 }
45
46 // over
47 354x if( it1 == end )
48 {
49 64x break;
50 }
51
52 // enter replacement id
53 290x ++it1;
54 290x BOOST_ASSERT(it1 != end);
55
56 // handle escaped replacement (second '{')
57 // there's no "{{" in URL templates because
58 // '{'s are not allowed in URLs
59 290x BOOST_ASSERT(*it1 != '{');
60 /*
61 if( *it1 == '{' )
62 {
63 mctx.advance_to( mctx.out() + measure_one('{', cs));
64 ++it1;
65 // this was not a real replacement,
66 // so we just keep moving
67 continue;
68 }
69 */
70
71
72 // parse {id} or {id:specs}
73 290x char const* id_start = it1;
74 290x while (it1 != end &&
75 540x *it1 != ':' &&
76 498x *it1 != '}')
77 {
78 250x ++it1;
79 }
80 290x core::string_view id(id_start, it1);
81
82 // move to specs start
83 290x if (it1 != end &&
84 290x *it1 == ':')
85 42x ++it1;
86 290x pctx.advance_to( it1 );
87
88 // get format_arg to use
89 290x auto idv = grammar::parse(
90 290x id, grammar::unsigned_rule<std::size_t>{});
91 290x if (idv)
92 {
93 33x mctx.arg( *idv ).measure( pctx, mctx, cs );
94 }
95 257x else if (!id.empty())
96 {
97 43x mctx.arg( id ).measure( pctx, mctx, cs );
98 }
99 else
100 {
101 214x std::size_t arg_id = pctx.next_arg_id();
102 214x mctx.arg( arg_id ).measure( pctx, mctx, cs );
103 }
104
105
106 288x it1 = pctx.begin();
107 288x BOOST_ASSERT(*it1 == '}');
108 288x it0 = it1 + 1;
109 }
110
111 299x return mctx.out();
112 }
113
114 char*
115 296x pct_vformat(
116 grammar::lut_chars const& cs,
117 format_parse_context& pctx,
118 format_context& fctx)
119 {
120 296x auto it0 = pctx.begin();
121 296x auto end = pctx.end();
122 582x while( it0 != end )
123 {
124 // look for replacement id
125 349x char const* it1 = it0;
126 349x while(
127 898x it1 != end &&
128 835x *it1 != '{' )
129 {
130 549x ++it1;
131 }
132
133 // output literal prefix
134 349x if( it0 != it1 )
135 {
136 691x for (char const* i = it0; i != it1; ++i)
137 {
138 549x char* o = fctx.out();
139 549x encode_one(o, *i, cs);
140 549x fctx.advance_to(o);
141 }
142 }
143
144 // over
145 349x if( it1 == end )
146 {
147 63x break;
148 }
149
150 // enter replacement id
151 286x ++it1;
152 286x BOOST_ASSERT(it1 != end);
153
154 // handle escaped replacement (second '{')
155 // there's no "{{" in URL templates because
156 // '{'s are not allowed in URLs
157 286x BOOST_ASSERT(*it1 != '{');
158 /*
159 if( *it1 == '{' )
160 {
161 char* o = fctx.out();
162 encode_one(o, '{', cs);
163 fctx.advance_to(o);
164 ++it1;
165 // this was not a real replacement,
166 // so we just keep moving
167 continue;
168 }
169 */
170
171 // parse {id} or {id:specs}
172 286x char const* id_start = it1;
173 286x while (it1 != end &&
174 536x *it1 != ':' &&
175 496x *it1 != '}')
176 {
177 250x ++it1;
178 }
179 286x core::string_view id(id_start, it1);
180
181 // move to specs part
182 286x if (it1 != end &&
183 286x *it1 == ':')
184 40x ++it1;
185 286x pctx.advance_to( it1 );
186
187 // get format_arg to use
188 286x auto idv = grammar::parse(
189 286x id, grammar::unsigned_rule<std::size_t>{});
190 286x if (idv)
191 {
192 33x fctx.arg( *idv ).format( pctx, fctx, cs );
193 }
194 253x else if (!id.empty())
195 {
196 43x fctx.arg( id ).format( pctx, fctx, cs );
197 }
198 else
199 {
200 210x std::size_t arg_id = pctx.next_arg_id();
201 210x fctx.arg( arg_id ).format( pctx, fctx, cs );
202 }
203
204 286x it1 = pctx.begin();
205 286x BOOST_ASSERT(*it1 == '}');
206 286x it0 = it1 + 1;
207 }
208
209 296x return fctx.out();
210 }
211
212 } // detail
213 } // urls
214 } // boost
215
216