Skip to content

Commit

Permalink
Make append_percent_encoded_byte function not a template
Browse files Browse the repository at this point in the history
Change parameter types:

* UINCHAR -> unsigned char

* std::basic_string<OUTCHAR> -> std::string
  • Loading branch information
rmisev committed Oct 25, 2023
1 parent 28bc95e commit 8e7f64f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
21 changes: 11 additions & 10 deletions include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -2182,10 +2182,10 @@ inline validation_errc url_parser::url_parse(url_serializer& urls, const CharT*
// invalid utf-8/16/32 sequences will be replaced with kUnicodeReplacementCharacter
detail::append_utf8_percent_encoded_char(pointer, end_of_query, str_query);
} else {
// Just append the 7-bit character, possibly escaping it.
// Just append the 7-bit character, possibly percent encoding it
const auto uc = static_cast<unsigned char>(uch);
if (!detail::is_char_in_set(uc, query_cpset))
detail::append_percent_encoded_byte(uch, str_query);
detail::append_percent_encoded_byte(uc, str_query);
else
str_query.push_back(uc);
++pointer;
Expand Down Expand Up @@ -2217,13 +2217,13 @@ inline validation_errc url_parser::url_parse(url_serializer& urls, const CharT*
// invalid utf-8/16/32 sequences will be replaced with kUnicodeReplacementCharacter
detail::append_utf8_percent_encoded_char(pointer, last, str_frag);
} else {
// Just append the 7-bit character, possibly escaping it.
// Just append the 7-bit character, possibly percent encoding it
const auto uc = static_cast<unsigned char>(uch);
if (detail::is_char_in_set(uc, fragment_no_encode_set)) {
str_frag.push_back(uc);
} else {
// other characters are escaped
detail::append_percent_encoded_byte(uch, str_frag);
// other characters are percent encoded
detail::append_percent_encoded_byte(uc, str_frag);
}
++pointer;
}
Expand Down Expand Up @@ -2331,7 +2331,7 @@ inline bool url_parser::do_path_segment(const CharT* pointer, const CharT* last,
// invalid utf-8/16/32 sequences will be replaced with 0xfffd
success &= detail::append_utf8_percent_encoded_char(pointer, last, output);
} else {
// Just append the 7-bit character, possibly escaping it.
// Just append the 7-bit character, possibly percent encoding it
const auto uc = static_cast<unsigned char>(uch);
if (!detail::is_char_in_set(uc, path_no_encode_set))
detail::append_percent_encoded_byte(uc, output);
Expand Down Expand Up @@ -2360,11 +2360,12 @@ inline bool url_parser::do_simple_path(const CharT* pointer, const CharT* last,
// invalid utf-8/16/32 sequences will be replaced with 0xfffd
success &= detail::append_utf8_percent_encoded_char(pointer, last, output);
} else {
// Just append the 7-bit character, escaping C0 control chars:
if (uch <= 0x1f)
detail::append_percent_encoded_byte(uch, output);
// Just append the 7-bit character, percent encoding C0 control chars
const auto uc = static_cast<unsigned char>(uch);
if (uc <= 0x1f)
detail::append_percent_encoded_byte(uc, output);
else
output.push_back(static_cast<unsigned char>(uch));
output.push_back(uc);
++pointer;
}
}
Expand Down
9 changes: 5 additions & 4 deletions include/upa/url_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,12 @@ inline validation_errc host_parser::parse_opaque_host(const CharT* first, const
// invalid utf-8/16/32 sequences will be replaced with 0xfffd
detail::append_utf8_percent_encoded_char(pointer, last, str_host);
} else {
// Just append the 7-bit character, escaping C0 control chars:
if (uch <= 0x1f)
detail::append_percent_encoded_byte(uch, str_host);
// Just append the 7-bit character, percent encoding C0 control chars
const auto uc = static_cast<unsigned char>(uch);
if (uc <= 0x1f)
detail::append_percent_encoded_byte(uc, str_host);
else
str_host.push_back(static_cast<unsigned char>(uch));
str_host.push_back(uc);
++pointer;
}
}
Expand Down
21 changes: 10 additions & 11 deletions include/upa/url_percent_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,10 @@ inline bool decode_hex_to_byte(const CharT*& first, const CharT* last, unsigned
// Percent-encodes byte and appends to string
// See: https://url.spec.whatwg.org/#percent-encode

template<typename UINCHAR, typename OUTCHAR>
inline void append_percent_encoded_byte(UINCHAR ch, std::basic_string<OUTCHAR>& output) {
inline void append_percent_encoded_byte(unsigned char uc, std::string& output) {
output.push_back('%');
output.push_back(kHexCharLookup[(ch >> 4) & 0xf]);
output.push_back(kHexCharLookup[ch & 0xf]);
output.push_back(kHexCharLookup[uc >> 4]);
output.push_back(kHexCharLookup[uc & 0xf]);
}

// Reads one character from string (first, last), converts to UTF-8, then
Expand All @@ -479,18 +478,18 @@ void append_utf8_percent_encoded(const CharT* first, const CharT* last, const co
using UCharT = typename std::make_unsigned<CharT>::type;

for (auto it = first; it < last; ) {
const auto ch = static_cast<UCharT>(*it);
if (ch >= 0x80) {
const auto uch = static_cast<UCharT>(*it);
if (uch >= 0x80) {
// invalid utf-8/16/32 sequences will be replaced with kUnicodeReplacementCharacter
append_utf8_percent_encoded_char(it, last, output);
} else {
// Just append the 7-bit character, possibly percent encoding it.
const auto uch = static_cast<unsigned char>(ch);
if (is_char_in_set(uch, cpset)) {
output.push_back(uch);
// Just append the 7-bit character, possibly percent encoding it
const auto uc = static_cast<unsigned char>(uch);
if (is_char_in_set(uc, cpset)) {
output.push_back(uc);
} else {
// other characters are percent encoded
append_percent_encoded_byte(uch, output);
append_percent_encoded_byte(uc, output);
}
++it;
}
Expand Down

0 comments on commit 8e7f64f

Please sign in to comment.