Skip to content

Commit

Permalink
syntax-based normalization
Browse files Browse the repository at this point in the history
fix #8, fix boostorg#65
  • Loading branch information
alandefreitas committed Mar 4, 2022
1 parent 38cff53 commit 4fb7a05
Show file tree
Hide file tree
Showing 15 changed files with 1,183 additions and 13 deletions.
100 changes: 100 additions & 0 deletions include/boost/url/detail/impl/pct_encoding.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,106 @@ key_equal_encoded(
}
}

int
pct_decode_compare_unchecked(
string_view lhs,
string_view rhs) noexcept
{
auto consume_one = []
(string_view::iterator& it,
char &c,
std::size_t& n)
{
if(*it != '%')
{
c = *it;
++it;
}
else
{
pct_decode_unchecked(
&c,
&c + 1,
string_view(it, 3));
it += 3;
}
++n;
};

std::size_t lhs_n = 0;
std::size_t rhs_n = 0;
auto lhs_it = lhs.begin();
auto rhs_it = rhs.begin();
char lhs_c = '\0';
char rhs_c = '\0';
while (lhs_it < lhs.end() &&
rhs_it < rhs.end())
{
consume_one(lhs_it, lhs_c, lhs_n);
consume_one(rhs_it, rhs_c, rhs_n);
if ( lhs_c < rhs_c )
return -1;
else if ( rhs_c < lhs_c )
return +1;
}
if ( lhs_n == rhs_n )
return 0;
if ( lhs_n < rhs_n )
return -1;
return +1;
}

int
pct_decode_icompare_unchecked(
string_view lhs,
string_view rhs) noexcept
{
auto consume_one = []
(string_view::iterator& it,
char &c,
std::size_t& n)
{
if(*it != '%')
{
c = grammar::ascii_tolower(*it);
++it;
}
else
{
pct_decode_unchecked(
&c,
&c + 1,
string_view(it, 3));
c = grammar::ascii_tolower(c);
it += 3;
}
++n;
};

std::size_t lhs_n = 0;
std::size_t rhs_n = 0;
auto lhs_it = lhs.begin();
auto rhs_it = rhs.begin();
char lhs_c = '\0';
char rhs_c = '\0';
while (lhs_it < lhs.end() &&
rhs_it < lhs.end())
{
consume_one(lhs_it, lhs_c, lhs_n);
consume_one(rhs_it, rhs_c, rhs_n);
if ( lhs_c < rhs_c )
return -1;
else if ( rhs_c < lhs_c )
return +1;
}
if ( lhs_n == rhs_n )
return 0;
if ( lhs_n < rhs_n )
return -1;
return +1;
}


} // detail
} // urls
} // boost
Expand Down
43 changes: 43 additions & 0 deletions include/boost/url/detail/impl/remove_dot_segments.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Copyright (c) 2022 alandefreitas (alandefreitas@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/CPPAlliance/url
//

#ifndef BOOST_URL_DETAIL_IMPL_REMOVE_DOT_SEGMENTS_HPP
#define BOOST_URL_DETAIL_IMPL_REMOVE_DOT_SEGMENTS_HPP

namespace boost {
namespace urls {
namespace detail {

template<class Allocator>
std::basic_string<char,
std::char_traits<char>,
Allocator>
remove_dot_segments(
string_view s,
Allocator const& a,
bool remove_leading_dot_dot)
{
using string_type = std::basic_string<
char,
std::char_traits<char>,
Allocator>;
string_type output(s.size(), '_', a);
char* it = &output[0];
char* end = it + output.size();
std::size_t n = remove_dot_segments(
it, end, s, remove_leading_dot_dot);
output.resize(n);
return output;
}

}
}
}

#endif
Loading

0 comments on commit 4fb7a05

Please sign in to comment.