encode.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_ENCODE_HPP
  11. #define BOOST_URL_ENCODE_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/encoding_opts.hpp>
  14. #include <boost/core/detail/string_view.hpp>
  15. #include <boost/url/grammar/all_chars.hpp>
  16. #include <boost/url/grammar/string_token.hpp>
  17. #include <boost/url/grammar/type_traits.hpp>
  18. #include <boost/url/grammar/charset.hpp>
  19. namespace boost {
  20. namespace urls {
  21. /** Return the buffer size needed for percent-encoding
  22. This function returns the exact number
  23. of bytes necessary to store the result
  24. of applying percent-encoding to the
  25. string using the given options and
  26. character set.
  27. No encoding is actually performed.
  28. @par Example
  29. @code
  30. assert( encoded_size( "My Stuff", pchars ) == 10 );
  31. @endcode
  32. @par Exception Safety
  33. Throws nothing.
  34. @return The number of bytes needed,
  35. excluding any null terminator.
  36. @param s The string to measure.
  37. @param allowed The set of characters
  38. that is not percent-encoded.
  39. @param opt The options for encoding. If
  40. this parameter is omitted, the default
  41. options are be used.
  42. @par Specification
  43. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1"
  44. >2.1. Percent-Encoding (rfc3986)</a>
  45. @see
  46. @ref encode,
  47. @ref encoding_opts,
  48. @ref make_pct_string_view.
  49. */
  50. template<BOOST_URL_CONSTRAINT(grammar::CharSet) CS>
  51. std::size_t
  52. encoded_size(
  53. core::string_view s,
  54. CS const& allowed,
  55. encoding_opts opt = {}) noexcept;
  56. //------------------------------------------------
  57. /** Apply percent-encoding to a string
  58. This function applies percent-encoding
  59. to the string using the given options and
  60. character set. The destination buffer
  61. provided by the caller is used to store
  62. the result, which may be truncated if
  63. there is insufficient space.
  64. @par Example
  65. @code
  66. char buf[100];
  67. assert( encode( buf, sizeof(buf), "Program Files", pchars ) == 15 );
  68. @endcode
  69. @par Exception Safety
  70. Throws nothing.
  71. @return The number of characters written
  72. to the destination buffer.
  73. @param dest The destination buffer
  74. to write to.
  75. @param size The number of writable
  76. characters pointed to by `dest`.
  77. If this is less than `encoded_size(s)`,
  78. the result is truncated.
  79. @param s The string to encode.
  80. @param allowed The set of characters
  81. that is not percent-encoded.
  82. @param opt The options for encoding. If
  83. this parameter is omitted, the default
  84. options are used.
  85. @par Specification
  86. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1"
  87. >2.1. Percent-Encoding (rfc3986)</a>
  88. @see
  89. @ref encode,
  90. @ref encoded_size,
  91. @ref make_pct_string_view.
  92. */
  93. template<BOOST_URL_CONSTRAINT(grammar::CharSet) CS>
  94. std::size_t
  95. encode(
  96. char* dest,
  97. std::size_t size,
  98. core::string_view s,
  99. CS const& allowed,
  100. encoding_opts opt = {});
  101. template<BOOST_URL_CONSTRAINT(grammar::CharSet) CS>
  102. std::size_t
  103. encode_unsafe(
  104. char* dest,
  105. std::size_t size,
  106. core::string_view s,
  107. CS const& allowed,
  108. encoding_opts opt);
  109. //------------------------------------------------
  110. /** Return a percent-encoded string
  111. This function applies percent-encoding
  112. to the string using the given options and
  113. character set, and returns the result as
  114. a string when called with default arguments.
  115. @par Example
  116. @code
  117. encoding_opts opt;
  118. opt.space_as_plus = true;
  119. std::string s = encode( "My Stuff", opt, pchars );
  120. assert( s == "My+Stuff" );
  121. @endcode
  122. @par Exception Safety
  123. Calls to allocate may throw.
  124. @return The string
  125. @param s The string to encode.
  126. @param allowed The set of characters
  127. that is not percent-encoded.
  128. @param opt The options for encoding. If
  129. this parameter is omitted, the default
  130. options are used.
  131. @param token A string token.
  132. @par Specification
  133. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1"
  134. >2.1. Percent-Encoding (rfc3986)</a>
  135. @see
  136. @ref encode,
  137. @ref encoded_size,
  138. @ref encoding_opts,
  139. */
  140. template<
  141. BOOST_URL_STRTOK_TPARAM,
  142. BOOST_URL_CONSTRAINT(grammar::CharSet) CS>
  143. BOOST_URL_STRTOK_RETURN
  144. encode(
  145. core::string_view s,
  146. CS const& allowed,
  147. encoding_opts opt = {},
  148. StringToken&& token = {}) noexcept;
  149. } // urls
  150. } // boost
  151. #include <boost/url/impl/encode.hpp>
  152. #endif