aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2018-07-02 21:21:48 -0700
committerRichard Walters <rwalters@digitalstirling.com>2018-07-02 21:21:48 -0700
commit1af6861f8db57b54ba19b80964eb00c5f7340bfb (patch)
treefe836a46b281a41cbee62a3519a80c73264f2b8a
parent6974150a2c6b3b4e0fa278b08de8b2647d2c95ed (diff)
Add capability to compare Uri objects.
* Code the neat example in section 6.2.2 of the RFC. * Add equality/inequality operators for Uri.
-rw-r--r--include/Uri/Uri.hpp24
-rw-r--r--src/Uri.cpp22
-rw-r--r--test/src/UriTests.cpp11
3 files changed, 57 insertions, 0 deletions
diff --git a/include/Uri/Uri.hpp b/include/Uri/Uri.hpp
index afdb631..b64f0da 100644
--- a/include/Uri/Uri.hpp
+++ b/include/Uri/Uri.hpp
@@ -37,6 +37,30 @@ namespace Uri {
Uri();
/**
+ * This is the equality comparison operator for the class.
+ *
+ * @param[in] other
+ * This is the other URI to which to compare this URI.
+ *
+ * @return
+ * An indication of whether or not the two URIs are
+ * equal is returned.
+ */
+ bool operator==(const Uri& other) const;
+
+ /**
+ * This is the inequality comparison operator for the class.
+ *
+ * @param[in] other
+ * This is the other URI to which to compare this URI.
+ *
+ * @return
+ * An indication of whether or not the two URIs are
+ * not equal is returned.
+ */
+ bool operator!=(const Uri& other) const;
+
+ /**
* This method builds the URI from the elements parsed
* from the given string rendering of a URI.
*
diff --git a/src/Uri.cpp b/src/Uri.cpp
index 0153e5f..35631cf 100644
--- a/src/Uri.cpp
+++ b/src/Uri.cpp
@@ -586,6 +586,28 @@ namespace Uri {
{
}
+ bool Uri::operator==(const Uri& other) const {
+ return (
+ (impl_->scheme == other.impl_->scheme)
+ && (impl_->userInfo == other.impl_->userInfo)
+ && (impl_->host == other.impl_->host)
+ && (
+ (!impl_->hasPort && !other.impl_->hasPort)
+ || (
+ (impl_->hasPort && other.impl_->hasPort)
+ && (impl_->port == other.impl_->port)
+ )
+ )
+ && (impl_->path == other.impl_->path)
+ && (impl_->query == other.impl_->query)
+ && (impl_->fragment == other.impl_->fragment)
+ );
+ }
+
+ bool Uri::operator!=(const Uri& other) const {
+ return !(*this == other);
+ }
+
bool Uri::ParseFromString(const std::string& uriString) {
// First parse the scheme.
// Limit our search so we don't scan into the authority
diff --git a/test/src/UriTests.cpp b/test/src/UriTests.cpp
index cbc8b8a..4275de6 100644
--- a/test/src/UriTests.cpp
+++ b/test/src/UriTests.cpp
@@ -603,3 +603,14 @@ TEST(UriTests, NormalizePath) {
++index;
}
}
+
+TEST(UriTests, ConstructNormalizeAndCompareEquivalentUris) {
+ // This was inspired by section 6.2.2
+ // of RFC 3986 (https://tools.ietf.org/html/rfc3986).
+ Uri::Uri uri1, uri2;
+ ASSERT_TRUE(uri1.ParseFromString("example://a/b/c/%7Bfoo%7D"));
+ ASSERT_TRUE(uri2.ParseFromString("eXAMPLE://a/./b/../b/%63/%7bfoo%7d"));
+ ASSERT_NE(uri1, uri2);
+ uri2.NormalizePath();
+ ASSERT_EQ(uri1, uri2);
+}