Skip to content

Commit

Permalink
uri resolve example
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 17, 2024
1 parent 9b9f641 commit 26d7a87
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
35 changes: 35 additions & 0 deletions doc/ref/corelib/utility/uri.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 <jsoncons/utility/uri.hpp>

int main()
{
#include <jsoncons/utility/uri.hpp>

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
```

19 changes: 18 additions & 1 deletion include/jsoncons/utility/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 26d7a87

Please sign in to comment.