encode.hpp 4.6 KB

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