aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2018-07-02 00:29:01 -0700
committerRichard Walters <rwalters@digitalstirling.com>2018-07-02 00:29:01 -0700
commita32396b98cad2abc6c3fbf15a2fe1a2eaa3c8e91 (patch)
treeb866bd1ab9c29f68c34e39fc70f8a6c50df885f0
parentddc380dc076d5335a34c837f41c8e5163aef3c44 (diff)
Refactoring
Extract methods that parse the query and fragment.
-rw-r--r--src/Uri.cpp76
1 files 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 {