delim_rule.hpp 4.1 KB

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