From a32396b98cad2abc6c3fbf15a2fe1a2eaa3c8e91 Mon Sep 17 00:00:00 2001
From: Richard Walters <rwalters@digitalstirling.com>
Date: Mon, 2 Jul 2018 00:29:01 -0700
Subject: Refactoring

Extract methods that parse the query and fragment.
---
 src/Uri.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 58 insertions(+), 18 deletions(-)

diff --git a/src/Uri.cpp b/src/Uri.cpp
index 7580723..77e561a 100644
--- a/src/Uri.cpp
+++ b/src/Uri.cpp
@@ -521,6 +521,62 @@ namespace Uri {
             }
             return true;
         }
+
+        /**
+         * This method takes the part of a URI string that has just
+         * the query element with its delimiter, and breaks off
+         * and decodes the query.
+         *
+         * @param[in] queryWithDelimiter
+         *     This is the part of a URI string that has just
+         *     the query element with its delimiter.
+         *
+         * @return
+         *     An indication of whether or not the method succeeded
+         *     is returned.
+         */
+        bool ParseQuery(const std::string& queryWithDelimiter) {
+            if (queryWithDelimiter.empty()) {
+                query.clear();
+            } else {
+                query = queryWithDelimiter.substr(1);
+            }
+            return DecodeQueryOrFragment(query);
+        }
+
+        /**
+         * This method takes the part of a URI string that has just
+         * the query and/or fragment elements, and breaks off
+         * and decodes the fragment part, returning the rest,
+         * which will be either empty or have the query with the
+         * query delimiter still attached.
+         *
+         * @param[in] queryAndOrFragment
+         *     This is the part of a URI string that has just
+         *     the query and/or fragment elements.
+         *
+         * @param[out] rest
+         *     This is where to store the rest of the input string
+         *     after removing any fragment and fragment delimiter.
+         *
+         * @return
+         *     An indication of whether or not the method succeeded
+         *     is returned.
+         */
+        bool ParseFragment(
+            const std::string& queryAndOrFragment,
+            std::string& rest
+        ) {
+            const auto fragmentDelimiter = queryAndOrFragment.find('#');
+            if (fragmentDelimiter == std::string::npos) {
+                fragment.clear();
+                rest = queryAndOrFragment;
+            } else {
+                fragment = queryAndOrFragment.substr(fragmentDelimiter + 1);
+                rest = queryAndOrFragment.substr(0, fragmentDelimiter);
+            }
+            return DecodeQueryOrFragment(fragment);
+        }
     };
 
     Uri::~Uri() = default;
@@ -594,28 +650,12 @@ namespace Uri {
         }
 
         // Next, parse the fragment if there is one.
-        const auto fragmentDelimiter = queryAndOrFragment.find('#');
-        if (fragmentDelimiter == std::string::npos) {
-            impl_->fragment.clear();
-            rest = queryAndOrFragment;
-        } else {
-            impl_->fragment = queryAndOrFragment.substr(fragmentDelimiter + 1);
-            rest = queryAndOrFragment.substr(0, fragmentDelimiter);
-        }
-        if (!DecodeQueryOrFragment(impl_->fragment)) {
+        if (!impl_->ParseFragment(queryAndOrFragment, rest)) {
             return false;
         }
 
         // Finally, if anything is left, it's the query.
-        if (rest.empty()) {
-            impl_->query.clear();
-        } else {
-            impl_->query = rest.substr(1);
-        }
-        if (!DecodeQueryOrFragment(impl_->query)) {
-            return false;
-        }
-        return true;
+        return impl_->ParseQuery(rest);
     }
 
     std::string Uri::GetScheme() const {
-- 
cgit v1.2.3