literal_rule.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifdef BOOST_URL_DOCS
  37. constexpr
  38. __implementation_defined__
  39. literal_rule( char const* s );
  40. #else
  41. class literal_rule
  42. {
  43. char const* s_ = nullptr;
  44. std::size_t n_ = 0;
  45. constexpr
  46. static
  47. std::size_t
  48. len(char const* s) noexcept
  49. {
  50. return *s
  51. ? 1 + len(s + 1)
  52. : 0;
  53. }
  54. public:
  55. using value_type = core::string_view;
  56. constexpr
  57. explicit
  58. literal_rule(
  59. char const* s) noexcept
  60. : s_(s)
  61. , n_(len(s))
  62. {
  63. }
  64. BOOST_URL_DECL
  65. system::result<value_type>
  66. parse(
  67. char const*& it,
  68. char const* end) const noexcept;
  69. };
  70. #endif
  71. } // grammar
  72. } // urls
  73. } // boost
  74. #endif