literal_rule.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef BOOST_URL_GRAMMAR_LITERAL_RULE_HPP
  10. #define BOOST_URL_GRAMMAR_LITERAL_RULE_HPP
  11. #include <boost/url/detail/config.hpp>
  12. #include <boost/url/error_types.hpp>
  13. #include <boost/core/detail/string_view.hpp>
  14. #include <cstdlib>
  15. namespace boost {
  16. namespace urls {
  17. namespace grammar {
  18. /** Match a string literal exactly
  19. If there is no more input, or if the
  20. end of the input is reached, and a prefix
  21. of the literal matches exactly, the error
  22. returned is @ref error::need_more.
  23. @par Value Type
  24. @code
  25. using value_type = core::string_view;
  26. @endcode
  27. @par Example
  28. Rules are used with the function @ref parse.
  29. @code
  30. system::result< core::string_view > rv = parse( "HTTP", literal_rule( "HTTP" ) );
  31. @endcode
  32. @see
  33. @ref delim_rule,
  34. @ref parse.
  35. */
  36. class literal_rule
  37. {
  38. char const* s_ = nullptr;
  39. std::size_t n_ = 0;
  40. constexpr
  41. static
  42. std::size_t
  43. len(char const* s) noexcept
  44. {
  45. return *s
  46. ? 1 + len(s + 1)
  47. : 0;
  48. }
  49. public:
  50. using value_type = core::string_view;
  51. constexpr
  52. explicit
  53. literal_rule(
  54. char const* s) noexcept
  55. : s_(s)
  56. , n_(len(s))
  57. {
  58. }
  59. BOOST_URL_DECL
  60. system::result<value_type>
  61. parse(
  62. char const*& it,
  63. char const* end) const noexcept;
  64. };
  65. } // grammar
  66. } // urls
  67. } // boost
  68. #endif