parse_path.ipp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_IMPL_PARSE_PATH_IPP
  11. #define BOOST_URL_IMPL_PARSE_PATH_IPP
  12. #include <boost/url/parse_path.hpp>
  13. #include <boost/url/error.hpp>
  14. #include <boost/url/detail/path.hpp>
  15. #include <boost/url/grammar/parse.hpp>
  16. #include <boost/url/rfc/detail/path_rules.hpp>
  17. namespace boost {
  18. namespace urls {
  19. result<segments_encoded_view>
  20. parse_path(string_view s) noexcept
  21. {
  22. auto it = s.data();
  23. auto const end = it + s.size();
  24. std::size_t dn = 0;
  25. std::size_t nseg = 0;
  26. if( it != end &&
  27. *it != '/')
  28. ++nseg;
  29. while(it != end)
  30. {
  31. if(*it == '/')
  32. {
  33. ++it;
  34. ++dn;
  35. ++nseg;
  36. continue;
  37. }
  38. auto rv = grammar::parse(
  39. it, end, detail::segment_rule);
  40. if(! rv)
  41. return rv.error();
  42. if(rv->empty())
  43. {
  44. BOOST_URL_RETURN_EC(
  45. grammar::error::mismatch);
  46. }
  47. dn += rv->decoded_size();
  48. }
  49. // adjust nseg
  50. nseg = detail::path_segments(s, nseg);
  51. return segments_encoded_view(
  52. detail::path_ref(s, dn, nseg));
  53. }
  54. } // urls
  55. } // boost
  56. #endif