41 (
'0' <= c && c <=
'9' ) ||
42 (
'a' <= c && c <=
'z' ) ||
43 (
'A' <= c && c <=
'Z' ) ||
66 (
'0' <= c && c <=
'9' ) ||
67 (
'a' <= c && c <=
'z' ) ||
68 (
'A' <= c && c <=
'Z' ) ||
101 return nullptr != std::strchr(
103 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
104 "abcdefghijklmnopqrstuvwxyz"
126 static constexpr bool
130 (
'0' <= c && c <=
'9' ) ||
131 (
'a' <= c && c <=
'z' ) ||
132 (
'A' <= c && c <=
'Z' ) ||
193 (
'0' <= c && c <=
'9' ) ||
194 (
'a' <= c && c <=
'f' ) ||
195 (
'A' <= c && c <=
'F' );
203 if(
'0' <= c1 && c1 <=
'9' )
208 result = 10 + c1 -
'a';
213 if(
'0' <= c2 && c2 <=
'9' )
218 result += 10 + c2 -
'a';
234 typename Chars_Collector >
241 Chars_Collector && collector )
243 std::size_t chars_to_handle = data.size();
244 const char * d = data.data();
247 bool expect_next_utf8_byte =
false;
249 const auto current_pos = [&d, &data]()
noexcept {
return d - data.data(); };
251 while( 0 < chars_to_handle )
254 if( expect_next_utf8_byte &&
'%' != c )
258 "next byte from UTF-8 sequence expected at {}" ),
264 if( chars_to_handle >= 3 &&
269 if( !utf8_checker.
process_byte(
static_cast<std::uint8_t
>(ch) ) )
273 "invalid UTF-8 sequence detected at {}" ),
278 chars_to_handle -= 3;
281 expect_next_utf8_byte = !utf8_checker.
finalized();
282 if( !expect_next_utf8_byte )
283 utf8_checker.
reset();
290 "invalid escape sequence at pos {}" ),
301 else if( Traits::ordinary_char( c ) )
312 "invalid non-escaped char with code {:#02X} at pos: {}" ),
319 if( expect_next_utf8_byte )
332template<
typename Traits = restinio_default_unescape_traits >
338 const auto escaped_chars_count =
static_cast<std::size_t
>(
342 [](
auto c ){ return !Traits::ordinary_char(c); } ));
344 if( 0 == escaped_chars_count )
347 result.assign( data.data(), data.size() );
352 result.reserve( data.size() + 2*escaped_chars_count );
355 if( Traits::ordinary_char( c ) )
367template<
typename Traits = restinio_default_unescape_traits >
373 result.reserve( data.size() );
375 auto r = impl::do_unescape_percent_encoding<Traits>(
377 [&result](
char ch ) { result += ch; } );
379 throw exception_t{ r.error().giveout_description() };
396template<
typename Traits = restinio_default_unescape_traits >
402 result.reserve( data.size() );
404 auto r = impl::do_unescape_percent_encoding<Traits>(
406 [&result](
char ch ) { result += ch; } );
408 return make_unexpected( std::move(r.error()) );
410 return std::move(result);
413template<
typename Traits = restinio_default_unescape_traits >
418 std::size_t result_size = 0u;
421 auto r = impl::do_unescape_percent_encoding<Traits>(
423 [&result_size, &dest](
char ch ) {
428 throw exception_t{ r.error().giveout_description() };
445template<
typename Traits = restinio_default_unescape_traits >
450 std::size_t result_size = 0u;
453 auto r = impl::do_unescape_percent_encoding<Traits>(
455 [&result_size, &dest](
char ch ) {
460 return make_unexpected( std::move(r.error()) );
467namespace uri_normalization
470namespace unreserved_chars
507 typename One_Byte_Handler,
508 typename Three_Byte_Handler >
512 One_Byte_Handler && one_byte_handler,
513 Three_Byte_Handler && three_byte_handler )
517 std::size_t chars_to_handle = what.size();
518 const char * d = what.data();
521 bool expect_next_utf8_byte =
false;
523 const auto current_pos = [&d, &what]()
noexcept {
return d - what.data(); };
525 while( 0 < chars_to_handle )
527 if( expect_next_utf8_byte &&
'%' != *d )
531 "next byte from UTF-8 sequence expected at {}" ),
538 one_byte_handler( *d );
542 else if( chars_to_handle >= 3 &&
543 is_hexdigit( d[ 1 ] ) && is_hexdigit( d[ 2 ] ) )
545 const char ch = extract_escaped_char( d[ 1 ], d[ 2 ] );
546 if( !utf8_checker.
process_byte(
static_cast<std::uint8_t
>(ch) ) )
550 "invalid UTF-8 sequence detected at {}" ),
554 bool keep_three_bytes =
true;
558 expect_next_utf8_byte =
false;
561 utf8_checker.
reset();
565 const char ascii_char =
static_cast<char>(symbol);
569 one_byte_handler( ascii_char );
570 keep_three_bytes =
false;
576 expect_next_utf8_byte =
true;
579 if( keep_three_bytes )
582 three_byte_handler( d[ 0 ], d[ 1 ], d[ 2 ] );
585 chars_to_handle -= 3;
598 if( expect_next_utf8_byte )
622 std::size_t calculated_capacity = 0u;
625 [&calculated_capacity](
char )
noexcept {
626 ++calculated_capacity;
628 [&calculated_capacity]( char, char, char )
noexcept {
629 calculated_capacity += 3u;
632 return calculated_capacity;
659 [&dest](
char ch )
noexcept {
662 [&dest](
char ch1,
char ch2,
char ch3 )
noexcept {
Exception class for all exceptions thrown by RESTinio.
Type that indicates a failure of unescaping of percent-encoded symbols.
RESTINIO_NODISCARD const std::string & description() const noexcept
Get a reference to the description of the failure.
std::string m_description
Description of a failure.
RESTINIO_NODISCARD std::string giveout_description() noexcept
Get out the value of the description of the failure.
unescape_percent_encoding_failure_t(std::string description)
Helper class for checking UTF-8 byte sequence during parsing URI or incoming byte stream.
RESTINIO_NODISCARD bool finalized() const noexcept
RESTINIO_NODISCARD bool process_byte(std::uint8_t byte) noexcept
RESTINIO_NODISCARD std::uint32_t current_symbol() const noexcept
#define RESTINIO_NODISCARD
A special wrapper around fmtlib include files.
#define RESTINIO_FMT_FORMAT_STRING(s)
RESTINIO_NODISCARD expected_t< unescape_percent_encoding_success_t, unescape_percent_encoding_failure_t > do_unescape_percent_encoding(const string_view_t data, Chars_Collector &&collector)
The actual implementation of unescape-percent-encoding procedure.
char extract_escaped_char(char c1, char c2)
RESTINIO_NODISCARD constexpr bool is_unreserved_char(const char ch) noexcept
Is this symbol a part of unreserved set?
void run_normalization_algo(string_view_t what, One_Byte_Handler &&one_byte_handler, Three_Byte_Handler &&three_byte_handler)
Internal helper to perform the main logic of enumeration of symbols in URI.
void normalize_to(string_view_t what, char *dest)
Perform normalization of URI value.
RESTINIO_NODISCARD std::size_t estimate_required_capacity(string_view_t what)
Calculate the size of a buffer to hold normalized value of a URI.
RESTINIO_NODISCARD expected_t< std::string, unescape_percent_encoding_failure_t > try_unescape_percent_encoding(const string_view_t data)
Helper function for unescaping percent-encoded string.
RESTINIO_NODISCARD expected_t< std::size_t, unescape_percent_encoding_failure_t > try_inplace_unescape_percent_encoding(char *data, std::size_t size)
Helper function for unescaping percent-encoded string inplace.
RESTINIO_NODISCARD std::size_t inplace_unescape_percent_encoding(char *data, std::size_t size)
RESTINIO_NODISCARD std::string unescape_percent_encoding(const string_view_t data)
RESTINIO_NODISCARD std::string escape_percent_encoding(const string_view_t data)
Percent encoding.
nonstd::string_view string_view_t
nonstd::expected< T, E > expected_t
The traits for escaping and unexcaping symbols in JavaScript-compatible mode.
static constexpr bool ordinary_char(char c) noexcept
Traits for escaping and unescaping symbols in a query string in very relaxed mode.
static bool ordinary_char(char c) noexcept
The default traits for escaping and unexcaping symbols in a query string.
static constexpr bool ordinary_char(char c) noexcept
Type that indicates that unescaping of percent-encoded symbols completed successfully.
An implementation of checker for UTF-8 sequences.