delim_rule.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_DELIM_RULE_HPP
  10. #define BOOST_URL_GRAMMAR_DELIM_RULE_HPP
  11. #include <boost/url/detail/config.hpp>
  12. #include <boost/core/detail/string_view.hpp>
  13. #include <boost/url/grammar/charset.hpp>
  14. #include <boost/url/grammar/error.hpp>
  15. #include <boost/url/grammar/type_traits.hpp>
  16. #include <type_traits>
  17. namespace boost {
  18. namespace urls {
  19. namespace grammar {
  20. /** Match a character literal
  21. This matches the specified character.
  22. The value is a reference to the character
  23. in the underlying buffer, expressed as a
  24. `core::string_view`. The function @ref squelch
  25. may be used to turn this into `void` instead.
  26. If there is no more input, the error code
  27. @ref error::need_more is returned.
  28. @par Value Type
  29. @code
  30. using value_type = core::string_view;
  31. @endcode
  32. @par Example
  33. Rules are used with the function @ref parse.
  34. @code
  35. system::result< core::string_view > rv = parse( ".", delim_rule('.') );
  36. @endcode
  37. @par BNF
  38. @code
  39. char = %00-FF
  40. @endcode
  41. @param ch The character to match
  42. @see
  43. @ref parse,
  44. @ref squelch.
  45. */
  46. #ifdef BOOST_URL_DOCS
  47. constexpr
  48. __implementation_defined__
  49. delim_rule( char ch ) noexcept;
  50. #else
  51. struct ch_delim_rule
  52. {
  53. using value_type = core::string_view;
  54. constexpr
  55. ch_delim_rule(char ch) noexcept
  56. : ch_(ch)
  57. {
  58. }
  59. BOOST_URL_DECL
  60. system::result<value_type>
  61. parse(
  62. char const*& it,
  63. char const* end) const noexcept;
  64. private:
  65. char ch_;
  66. };
  67. constexpr
  68. ch_delim_rule
  69. delim_rule( char ch ) noexcept
  70. {
  71. return ch_delim_rule(ch);
  72. }
  73. #endif
  74. //------------------------------------------------
  75. /** Match a single character from a character set
  76. This matches exactly one character which
  77. belongs to the specified character set.
  78. The value is a reference to the character
  79. in the underlying buffer, expressed as a
  80. `core::string_view`. The function @ref squelch
  81. may be used to turn this into `void` instead.
  82. If there is no more input, the error code
  83. @ref error::need_more is returned.
  84. @par Value Type
  85. @code
  86. using value_type = core::string_view;
  87. @endcode
  88. @par Example
  89. Rules are used with the function @ref parse.
  90. @code
  91. system::result< core::string_view > rv = parse( "X", delim_rule( alpha_chars ) );
  92. @endcode
  93. @param cs The character set to use.
  94. @see
  95. @ref alpha_chars,
  96. @ref parse,
  97. @ref squelch.
  98. */
  99. #ifdef BOOST_URL_DOCS
  100. template<class CharSet>
  101. constexpr
  102. __implementation_defined__
  103. delim_rule( CharSet const& cs ) noexcept;
  104. #else
  105. template<class CharSet>
  106. struct cs_delim_rule
  107. {
  108. using value_type = core::string_view;
  109. constexpr
  110. cs_delim_rule(
  111. CharSet const& cs) noexcept
  112. : cs_(cs)
  113. {
  114. }
  115. system::result<value_type>
  116. parse(
  117. char const*& it,
  118. char const* end) const noexcept
  119. {
  120. if(it == end)
  121. {
  122. // end
  123. BOOST_URL_RETURN_EC(
  124. error::need_more);
  125. }
  126. if(! cs_(*it))
  127. {
  128. // wrong character
  129. BOOST_URL_RETURN_EC(
  130. error::mismatch);
  131. }
  132. return core::string_view{
  133. it++, 1 };
  134. }
  135. private:
  136. CharSet cs_;
  137. };
  138. template<class CharSet>
  139. constexpr
  140. typename std::enable_if<
  141. ! std::is_convertible<
  142. CharSet, char>::value,
  143. cs_delim_rule<CharSet>>::type
  144. delim_rule(
  145. CharSet const& cs) noexcept
  146. {
  147. // If you get a compile error here it
  148. // means that your type does not meet
  149. // the requirements for a CharSet.
  150. // Please consult the documentation.
  151. static_assert(
  152. is_charset<CharSet>::value,
  153. "CharSet requirements not met");
  154. return cs_delim_rule<CharSet>(cs);
  155. }
  156. #endif
  157. } // grammar
  158. } // urls
  159. } // boost
  160. #endif