From 26d7a87459550011d8d9d1180891bc184c2ab2a3 Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Tue, 17 Dec 2024 18:22:38 -0500 Subject: [PATCH] uri resolve example --- doc/ref/corelib/utility/uri.md | 35 ++++++++++++++++++++++++++++++++ include/jsoncons/utility/uri.hpp | 19 ++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/doc/ref/corelib/utility/uri.md b/doc/ref/corelib/utility/uri.md index 44242b6b9..bb314c2da 100644 --- a/doc/ref/corelib/utility/uri.md +++ b/doc/ref/corelib/utility/uri.md @@ -116,6 +116,9 @@ Returns true if this URI has a query part, otherwise false. bool has_fragment() const noexcept; Returns true if this URI has a fragment part, otherwise false. + uri resolve(jsoncons::string_view reference) const; +Resolve `reference` as a URI relative to this URI. + uri resolve(const uri& reference) const; Resolve `reference` as a URI relative to this URI. @@ -126,6 +129,38 @@ Returns a URI string. Creates a `uri` by parsing the given string. If a parse error is encountered, returns a default constructed `uri` and sets `ec`. + friend std::ostream& operator<<(std::ostream& os, const uri& uri_); + ### Examples +```cpp +#include + +int main() +{ +#include + +int main() +{ + jsoncons::uri uri{ "https://github.com/danielaparker/jsoncons/" }; + + auto uri1 = uri.resolve("tree/master/doc/ref/corelib/utility/uri.md"); + std::cout << "(1) " << uri1 << "\n"; + + auto uri2 = uri1.resolve("../../../../../../blob/master/include/jsoncons/utility/uri.hpp"); + std::cout << "(2) " << uri2 << "\n"; + + auto uri3 = uri2.resolve("file:///~calendar"); + std::cout << "(3) " << uri3 << "\n"; +} +} +``` + +Output: + +``` +(1) https://github.com/danielaparker/jsoncons/tree/master/doc/ref/corelib/utility/uri.md +(2) https://github.com/danielaparker/jsoncons/blob/master/include/jsoncons/utility/uri.hpp +(3) file:///~calendar +``` diff --git a/include/jsoncons/utility/uri.hpp b/include/jsoncons/utility/uri.hpp index ec38e3ea1..c0ee572e3 100644 --- a/include/jsoncons/utility/uri.hpp +++ b/include/jsoncons/utility/uri.hpp @@ -525,6 +525,11 @@ namespace jsoncons { return !raw_fragment().empty(); } + uri resolve(string_view reference) const + { + return resolve(uri(reference)); + } + uri resolve(const uri& reference) const { // This implementation uses the psuedo-code given in @@ -1610,7 +1615,14 @@ namespace jsoncons { { valid = false; } - cur = pct_encoded.second ? pct_encoded.first : ++cur; + if (pct_encoded.second) + { + cur = pct_encoded.first; + } + else + { + ++cur; + } } return valid; } @@ -1657,6 +1669,11 @@ namespace jsoncons { } return valid; } + + friend std::ostream& operator<<(std::ostream& os, const uri& a_uri) + { + return os << a_uri.string(); + } }; } // namespace jsoncons