From c8fde33aba9750a728254bd13a9b420bba014be6 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 15 Sep 2022 19:41:21 -0700 Subject: [PATCH] tidy up hier-part rule --- .../boost/url/rfc/detail/hier_part_rule.hpp | 8 +- .../url/rfc/detail/impl/hier_part_rule.ipp | 89 ++++++++++--------- include/boost/url/rfc/detail/path_rules.hpp | 2 +- .../boost/url/rfc/impl/absolute_uri_rule.ipp | 4 +- include/boost/url/rfc/impl/uri_rule.ipp | 4 +- 5 files changed, 59 insertions(+), 48 deletions(-) diff --git a/include/boost/url/rfc/detail/hier_part_rule.hpp b/include/boost/url/rfc/detail/hier_part_rule.hpp index 5b79d836..c9627191 100644 --- a/include/boost/url/rfc/detail/hier_part_rule.hpp +++ b/include/boost/url/rfc/detail/hier_part_rule.hpp @@ -37,10 +37,12 @@ struct hier_part_rule_t { struct value_type { - bool has_authority = false; authority_view authority; - grammar::range< - pct_string_view> path; + pct_string_view path; + std::size_t segment_count = 0; + bool has_authority = false; + + //grammar::range path; }; BOOST_URL_DECL diff --git a/include/boost/url/rfc/detail/impl/hier_part_rule.ipp b/include/boost/url/rfc/detail/impl/hier_part_rule.ipp index 2ce09568..811a871e 100644 --- a/include/boost/url/rfc/detail/impl/hier_part_rule.ipp +++ b/include/boost/url/rfc/detail/impl/hier_part_rule.ipp @@ -28,65 +28,74 @@ parse( result { value_type t; - if(it == end) { // path-empty - BOOST_URL_RETURN(t); + return {}; } - if(it[0] != '/') + if(end - it == 1) { - // path-rootless - auto const it0 = it; - auto rv = grammar::parse( - it, end, - path_rootless_rule); - if( rv ) + if(*it == '/') { - t.path = std::move(*rv); - BOOST_URL_RETURN(t); + t.path = detail::make_pct_string_view( + it, 1, 1); + t.segment_count = 1; + ++it; + return t; } - it = it0; - - // path-empty - BOOST_URL_RETURN(t); - } - if( end - it == 1 || - it[1] != '/') - { - // path-absolute auto rv = grammar::parse( - it, end, - path_absolute_rule); + it, end, segment_rule); if(! rv) return rv.error(); - t.path = std::move(*rv); - t.has_authority = false; - BOOST_URL_RETURN(t); + t.path = *rv; + t.segment_count = 1; + return t; } - - // "//" authority path-abempty - it += 2; - // authority + if( it[0] == '/' && + it[1] == '/') { + it += 2; auto rv = grammar::parse( it, end, authority_rule); if(! rv) return rv.error(); t.authority = *rv; - } - - // path-abempty - { - auto rv = grammar::parse( - it, end, path_abempty_rule); - if(! rv) - return rv.error(); - t.path = std::move(*rv); t.has_authority = true; } - - BOOST_URL_RETURN(t); + auto const it0 = it; + std::size_t dn = 0; + if( it != end && + *it != '/') + { + auto rv = grammar::parse( + it, end, segment_rule); + if(! rv) + return rv.error(); + if(rv->empty()) + return t; + dn += rv->decoded_size(); + ++t.segment_count; + } + while(it != end) + { + if(*it == '/') + { + ++dn; + ++it; + ++t.segment_count; + continue; + } + auto rv = grammar::parse( + it, end, segment_rule); + if(! rv) + return rv.error(); + if(rv->empty()) + break; + dn += rv->decoded_size(); + } + t.path = detail::make_pct_string_view( + it0, it - it0, dn); + return t; } } // detail diff --git a/include/boost/url/rfc/detail/path_rules.hpp b/include/boost/url/rfc/detail/path_rules.hpp index 2fe29589..9af81058 100644 --- a/include/boost/url/rfc/detail/path_rules.hpp +++ b/include/boost/url/rfc/detail/path_rules.hpp @@ -35,7 +35,7 @@ namespace detail { @ref grammar::parse. */ constexpr auto segment_rule = - pct_encoded_rule(grammar::ref(pchars)); + pct_encoded_rule(pchars); //------------------------------------------------ diff --git a/include/boost/url/rfc/impl/absolute_uri_rule.ipp b/include/boost/url/rfc/impl/absolute_uri_rule.ipp index 5fd0cf8f..7a3bbd01 100644 --- a/include/boost/url/rfc/impl/absolute_uri_rule.ipp +++ b/include/boost/url/rfc/impl/absolute_uri_rule.ipp @@ -56,8 +56,8 @@ parse( if(rv->has_authority) u.apply_authority(rv->authority); u.apply_path( - rv->path.string(), - rv->path.size()); + rv->path, + rv->segment_count); } // [ "?" query ] diff --git a/include/boost/url/rfc/impl/uri_rule.ipp b/include/boost/url/rfc/impl/uri_rule.ipp index 1139b7e6..5e7a0a62 100644 --- a/include/boost/url/rfc/impl/uri_rule.ipp +++ b/include/boost/url/rfc/impl/uri_rule.ipp @@ -57,8 +57,8 @@ parse( if(rv->has_authority) u.apply_authority(rv->authority); u.apply_path( - rv->path.string(), - rv->path.size()); + rv->path, + rv->segment_count); } // [ "?" query ]