Skip to content

Commit

Permalink
hash support
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Mar 15, 2022
1 parent a5d2c5c commit e943cb1
Show file tree
Hide file tree
Showing 9 changed files with 611 additions and 176 deletions.
9 changes: 9 additions & 0 deletions include/boost/url/detail/config.hpp
Expand Up @@ -75,4 +75,13 @@
return &loc; }()))
#endif

// detect 32/64 bit
#if UINTPTR_MAX == UINT64_MAX
# define BOOST_URL_ARCH 64
#elif UINTPTR_MAX == UINT32_MAX
# define BOOST_URL_ARCH 32
#else
# error Unknown or unsupported architecture, please open an issue
#endif

#endif
166 changes: 99 additions & 67 deletions include/boost/url/detail/impl/normalize.ipp
Expand Up @@ -20,119 +20,139 @@ namespace boost {
namespace urls {
namespace detail {

void
pop_encoded_front(
string_view& s,
char& c,
std::size_t& n) noexcept
{
if(s.front() != '%')
{
c = s.front();
s.remove_prefix(1);
}
else
{
pct_decode_unchecked(
&c,
&c + 1,
s.substr(0, 3));
s.remove_prefix(3);
}
++n;
}

int
compare_encoded(
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 n0 = 0;
std::size_t n1 = 0;
auto it0 = lhs.begin();
auto it1 = rhs.begin();
auto end0 = lhs.end();
auto end1 = rhs.end();
char c0 = 0;
char c1 = 0;
while(
it0 < end0 &&
it1 < end1)
!lhs.empty() &&
!rhs.empty())
{
consume_one(it0, c0, n0);
consume_one(it1, c1, n1);
pop_encoded_front(lhs, c0, n0);
pop_encoded_front(rhs, c1, n1);
if (c0 < c1)
return -1;
if (c1 < c0)
return 1;
}
n0 += pct_decode_bytes_unchecked(
string_view(it0, end0 - it0));
n1 += pct_decode_bytes_unchecked(
string_view(it1, end1 - it1));
n0 += pct_decode_bytes_unchecked(lhs);
n1 += pct_decode_bytes_unchecked(rhs);
if (n0 == n1)
return 0;
if (n0 < n1)
return -1;
return 1;
}

void
digest_encoded(
string_view s,
fnv_1a& hasher) noexcept
{
char c = 0;
std::size_t n = 0;
while(!s.empty())
{
pop_encoded_front(s, c, n);
hasher.put(c);
}
}

int
ci_compare_encoded(
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 n0 = 0;
std::size_t n1 = 0;
auto it0 = lhs.begin();
auto it1 = rhs.begin();
auto end0 = lhs.end();
auto end1 = rhs.end();
char c0 = 0;
char c1 = 0;
while (
it0 < end0 &&
it1 < end1)
!lhs.empty() &&
!rhs.empty())
{
consume_one(it0, c0, n0);
consume_one(it1, c1, n1);
pop_encoded_front(lhs, c0, n0);
pop_encoded_front(rhs, c1, n1);
c0 = grammar::ascii_tolower(c0);
c1 = grammar::ascii_tolower(c1);
if (c0 < c1)
return -1;
if (c1 < c0)
return 1;
}
n0 += pct_decode_bytes_unchecked(
string_view(it0, end0 - it0));
n1 += pct_decode_bytes_unchecked(
string_view(it1, end1 - it1));
n0 += pct_decode_bytes_unchecked(lhs);
n1 += pct_decode_bytes_unchecked(rhs);
if (n0 == n1)
return 0;
if (n0 < n1)
return -1;
return 1;
}

void
ci_digest_encoded(
string_view s,
fnv_1a& hasher) noexcept
{
char c = 0;
std::size_t n = 0;
while(!s.empty())
{
pop_encoded_front(s, c, n);
c = grammar::ascii_tolower(c);
hasher.put(c);
}
}

int
compare(
string_view lhs,
string_view rhs) noexcept
{
auto rlen = (std::min)(lhs.size(), rhs.size());
for (std::size_t i = 0; i < rlen; ++i)
{
char c0 = lhs[i];
char c1 = rhs[i];
if (c0 < c1)
return -1;
if (c1 < c0)
return 1;
}
if ( lhs.size() == rhs.size() )
return 0;
if ( lhs.size() < rhs.size() )
return -1;
return 1;
}

int
ci_compare(
string_view lhs,
Expand All @@ -155,6 +175,18 @@ ci_compare(
return 1;
}

void
ci_digest(
string_view s,
fnv_1a& hasher) noexcept
{
for (char c: s)
{
c = grammar::ascii_tolower(c);
hasher.put(c);
}
}

std::size_t
path_starts_with(
string_view lhs,
Expand Down

0 comments on commit e943cb1

Please sign in to comment.