Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comparison differentiates absent and empty components #645

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions include/boost/url/authority_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,152 @@ class BOOST_SYMBOL_VISIBLE
pct_string_view
encoded_host_and_port() const noexcept;

//--------------------------------------------
//
// Comparison
//
//--------------------------------------------

/** Return the result of comparing this with another authority

This function compares two authorities
according to Syntax-Based comparison
algorithm.

@par Exception Safety
Throws nothing.

@return -1 if `*this < other`, 0 if
`this == other`, and 1 if `this > other`.

@par Specification
@li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
>6.2.2 Syntax-Based Normalization (rfc3986)</a>
*/
BOOST_URL_DECL
int
compare(authority_view const& other) const noexcept;

/** Return the result of comparing two authorities
The authorities are compared component
by component as if they were first
normalized.

@par Complexity
Linear in `min( a0.size(), a1.size() )`

@par Exception Safety
Throws nothing
*/
friend
bool
operator==(
authority_view const& a0,
authority_view const& a1) noexcept
{
return a0.compare(a1) == 0;
}

/** Return the result of comparing two authorities
The authorities are compared component
by component as if they were first
normalized.

@par Complexity
Linear in `min( a0.size(), a1.size() )`

@par Exception Safety
Throws nothing
alandefreitas marked this conversation as resolved.
Show resolved Hide resolved
*/
friend
bool
operator!=(
authority_view const& a0,
authority_view const& a1) noexcept
{
return ! (a0 == a1);
}

/** Return the result of comparing two authorities
The authorities are compared component
by component as if they were first
normalized.

@par Complexity
Linear in `min( a0.size(), a1.size() )`

@par Exception Safety
Throws nothing
*/
friend
bool
operator<(
authority_view const& a0,
authority_view const& a1) noexcept
{
return a0.compare(a1) < 0;
}

/** Return the result of comparing two authorities
The authorities are compared component
by component as if they were first
normalized.

@par Complexity
Linear in `min( a0.size(), a1.size() )`

@par Exception Safety
Throws nothing
*/
friend
bool
operator<=(
authority_view const& a0,
authority_view const& a1) noexcept
{
return a0.compare(a1) <= 0;
}

/** Return the result of comparing two authorities
The authorities are compared component
by component as if they were first
normalized.

@par Complexity
Linear in `min( a0.size(), a1.size() )`

@par Exception Safety
Throws nothing
*/
friend
bool
operator>(
authority_view const& a0,
authority_view const& a1) noexcept
{
return a0.compare(a1) > 0;
}

/** Return the result of comparing two authorities
The authorities are compared component
by component as if they were first
normalized.

@par Complexity
Linear in `min( a0.size(), a1.size() )`

@par Exception Safety
Throws nothing
*/
friend
bool
operator>=(
authority_view const& a0,
authority_view const& a1) noexcept
{
return a0.compare(a1) >= 0;
}

//--------------------------------------------

// hidden friend
Expand Down
61 changes: 61 additions & 0 deletions include/boost/url/impl/authority_view.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,67 @@ parse_authority(
return grammar::parse(s, authority_rule);
}

//------------------------------------------------
//
// Comparisons
//
//------------------------------------------------

int
authority_view::
compare(const authority_view& other) const noexcept
{
auto comp = static_cast<int>(has_userinfo()) -
static_cast<int>(other.has_userinfo());
if ( comp != 0 )
return comp;

if (has_userinfo())
{
comp = detail::compare_encoded(
encoded_user(),
other.encoded_user());
if ( comp != 0 )
return comp;

comp = static_cast<int>(has_password()) -
static_cast<int>(other.has_password());
if ( comp != 0 )
return comp;

if (has_password())
{
comp = detail::compare_encoded(
encoded_password(),
other.encoded_password());
if ( comp != 0 )
return comp;
}
}

comp = detail::ci_compare_encoded(
encoded_host(),
other.encoded_host());
if ( comp != 0 )
return comp;

comp = static_cast<int>(has_port()) -
static_cast<int>(other.has_port());
if ( comp != 0 )
return comp;

if (has_port())
{
comp = detail::compare(
port(),
other.port());
if ( comp != 0 )
return comp;
}

return 0;
}

} // urls
} // boost

Expand Down
72 changes: 44 additions & 28 deletions include/boost/url/impl/url_view_base.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -640,54 +640,70 @@ int
url_view_base::
compare(const url_view_base& other) const noexcept
{
int comp = detail::ci_compare(
scheme(),
other.scheme());
int comp =
static_cast<int>(has_scheme()) -
static_cast<int>(other.has_scheme());
if ( comp != 0 )
return comp;

comp = detail::compare_encoded(
encoded_user(),
other.encoded_user());
if ( comp != 0 )
return comp;

comp = detail::compare_encoded(
encoded_password(),
other.encoded_password());
if ( comp != 0 )
return comp;
if (has_scheme())
{
comp = detail::ci_compare(
scheme(),
other.scheme());
if ( comp != 0 )
return comp;
}

comp = detail::ci_compare_encoded(
encoded_host(),
other.encoded_host());
comp =
static_cast<int>(has_authority()) -
static_cast<int>(other.has_authority());
if ( comp != 0 )
return comp;

comp = detail::compare(
port(),
other.port());
if ( comp != 0 )
return comp;
if (has_authority())
{
comp = authority().compare(other.authority());
if ( comp != 0 )
return comp;
}

comp = detail::segments_compare(
encoded_segments(),
other.encoded_segments());
if ( comp != 0 )
return comp;

comp = detail::compare_encoded(
encoded_query(),
other.encoded_query());
comp =
static_cast<int>(has_query()) -
static_cast<int>(other.has_query());
if ( comp != 0 )
return comp;

comp = detail::compare_encoded(
encoded_fragment(),
other.encoded_fragment());
if (has_query())
{
comp = detail::compare_encoded(
encoded_query(),
other.encoded_query());
if ( comp != 0 )
return comp;
}

comp =
static_cast<int>(has_fragment()) -
static_cast<int>(other.has_fragment());
if ( comp != 0 )
return comp;

if (has_fragment())
{
comp = detail::compare_encoded(
encoded_fragment(),
other.encoded_fragment());
if ( comp != 0 )
return comp;
}

return 0;
}

Expand Down
7 changes: 7 additions & 0 deletions test/unit/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,13 @@ struct url_test
// issue 579
check("https://www.boost.org/doc/../%69%6e%64%65%78%20file.html",
"https://www.boost.org/index%20file.html");
// issue 646
BOOST_TEST_NE(
url("https://@www.boost.org/"),
url("https://www.boost.org/"));
BOOST_TEST_NE(
url("https://:@www.boost.org/"),
url("https://@www.boost.org/"));
}

// normalize path
Expand Down