Skip to content

Replaced the accessors that return optional string_view objects with two separate functions. #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/detail/uri_resolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ uri::string_type remove_dot_segments(uri::string_view path) {
uri::string_type merge_paths(const uri& base, const uri& reference) {
uri::string_type result;

if (!base.path() || base.path()->empty()) {
if (!base.has_path() || base.path().empty()) {
result = "/";
} else {
const auto& base_path = base.path().value();
const auto& base_path = base.path();
auto last_slash = network_boost::find_last(base_path, "/");
result.append(std::begin(base_path), std::end(last_slash));
}
if (reference.path()) {
result.append(reference.path()->to_string());
if (reference.has_path()) {
result.append(reference.path().to_string());
}
return remove_dot_segments(std::move(result));
}
Expand Down
117 changes: 87 additions & 30 deletions src/network/uri/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class uri {

public:
/**
* \brief Constructor.
* \brief Default constructor.
*/
uri();

Expand Down Expand Up @@ -205,78 +205,135 @@ class uri {
* underlying sequence.
* \return An iterator starting at the first element.
*/
const_iterator begin() const;
const_iterator begin() const noexcept;

/**
* \brief Returns an iterator at the end + 1th element in the
* underlying sequence.
* \return An iterator starting at the end + 1th element.
*/
const_iterator end() const;
const_iterator end() const noexcept;

/**
* \brief Tests whether this URI has a scheme component.
* \return \c true if the URI has a scheme, \c false otherwise.
*/
bool has_scheme() const noexcept;

/**
* \brief Returns the URI scheme.
* \return The scheme, if it exists, or nullopt.
* \return The scheme.
* \pre has_scheme()
*/
string_view scheme() const noexcept;

/**
* \brief Tests whether this URI has a user info component.
* \return \c true if the URI has a user info, \c false otherwise.
*/
optional<string_view> scheme() const;
bool has_user_info() const noexcept;

/**
* \brief Returns the URI user info.
* \return The user info, if it exists, or nullopt.
* \return The user info.
* \pre has_user_info()
*/
optional<string_view> user_info() const;
string_view user_info() const noexcept;

/**
* \brief Tests whether this URI has a host component.
* \return \c true if the URI has a host, \c false otherwise.
*/
bool has_host() const noexcept;

/**
* \brief Returns the URI host.
* \return The host, if it exists, or nullopt.
* \return The host.
* \pre has_host()
*/
optional<string_view> host() const;
string_view host() const noexcept;

/**
* \brief Tests whether this URI has a port component.
* \return \c true if the URI has a port, \c false otherwise.
*/
bool has_port() const noexcept;

/**
* \brief Returns the URI port.
* \return The port, if it exists, or nullopt.
* \return The port.
* \pre has_port()
*/
optional<string_view> port() const;
string_view port() const noexcept;

/**
* \brief Returns the URI port as an integer.
* \return The port, if it exists, or nullopt.
* \return The port number.
* \pre has_port()
*/
template <typename IntT>
optional<IntT> port(typename std::is_integral<IntT>::type * = 0) const {
if (auto p = port()) {
try {
return std::stoi(string_type(std::begin(*p), std::end(*p)));
} catch (std::exception &) {
return optional<IntT>();
}
IntT port(typename std::is_integral<IntT>::type * = 0) const {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: your comment the port accessor:

Remember that this already an overload. "string_view port() const" also exists. I provided another accessor that does the conversion to an integer type. This second function might either return an optional, or throw an exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. I think throwing here is fine.

assert(has_port());

auto p = port();
try {
return std::stoi(port().to_string());
}
catch (std::exception &) {
assert(!"Unable to parse port number.");
}
return optional<IntT>();
}

/**
* \brief Tests whether this URI has a path component.
* \return \c true if the URI has a path, \c false otherwise.
*/
bool has_path() const noexcept;

/**
* \brief Returns the URI path.
* \return The path, if it exists, or nullopt.
* \return The path.
* \pre has_path()
*/
string_view path() const noexcept;

/**
* \brief Tests whether this URI has a query component.
* \return \c true if the URI has a query, \c false otherwise.
*/
optional<string_view> path() const;
bool has_query() const noexcept;

/**
* \brief Returns the URI query.
* \return The query, if it exists, or nullopt.
* \return The query.
* \pre has_query()
*/
optional<string_view> query() const;
string_view query() const noexcept;

/**
* \brief Tests whether this URI has a fragment component.
* \return \c true if the URI has a fragment, \c false otherwise.
*/
bool has_fragment() const noexcept;

/**
* \brief Returns the URI fragment.
* \return The fragment, if it exists, or nullopt.
* \return The fragment.
* \pre has_fragment()
*/
string_view fragment() const noexcept;

/**
* \brief Tests whether this URI has a valid authority.
* \return \c true if the URI has an authority, \c false otherwise.
*/
optional<string_view> fragment() const;
bool has_authority() const noexcept;

/**
* \brief Returns the URI authority.
* \return The authority, if it exists, or nullopt.
* \return The authority.
*/
optional<string_view> authority() const;
string_view authority() const noexcept;

#if !defined(NETWORK_URI_MSVC)
/**
Expand Down Expand Up @@ -333,14 +390,14 @@ class uri {
* \brief Checks if the uri is absolute, i.e. it has a scheme.
* \returns \c true if it is absolute, \c false if it is relative.
*/
bool is_absolute() const;
bool is_absolute() const noexcept;

/**
* \brief Checks if the uri is opaque, i.e. if it doesn't have an
* authority.
* \returns \c true if it is opaque, \c false if it is hierarchical.
*/
bool is_opaque() const;
bool is_opaque() const noexcept;

/**
* \brief Normalizes a uri object at a given level in the
Expand Down
Loading