Skip to content

Commit beb37d4

Browse files
committed
Merge pull request #76 from glynos/accessor_changes
Replaced the accessors that return optional string_view objects with two separate functions.
2 parents 09b2fac + ea12b13 commit beb37d4

File tree

7 files changed

+469
-348
lines changed

7 files changed

+469
-348
lines changed

src/detail/uri_resolve.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ uri::string_type remove_dot_segments(uri::string_view path) {
6565
uri::string_type merge_paths(const uri& base, const uri& reference) {
6666
uri::string_type result;
6767

68-
if (!base.path() || base.path()->empty()) {
68+
if (!base.has_path() || base.path().empty()) {
6969
result = "/";
7070
} else {
71-
const auto& base_path = base.path().value();
71+
const auto& base_path = base.path();
7272
auto last_slash = network_boost::find_last(base_path, "/");
7373
result.append(std::begin(base_path), std::end(last_slash));
7474
}
75-
if (reference.path()) {
76-
result.append(reference.path()->to_string());
75+
if (reference.has_path()) {
76+
result.append(reference.path().to_string());
7777
}
7878
return remove_dot_segments(std::move(result));
7979
}

src/network/uri/uri.hpp

+87-30
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class uri {
119119

120120
public:
121121
/**
122-
* \brief Constructor.
122+
* \brief Default constructor.
123123
*/
124124
uri();
125125

@@ -205,78 +205,135 @@ class uri {
205205
* underlying sequence.
206206
* \return An iterator starting at the first element.
207207
*/
208-
const_iterator begin() const;
208+
const_iterator begin() const noexcept;
209209

210210
/**
211211
* \brief Returns an iterator at the end + 1th element in the
212212
* underlying sequence.
213213
* \return An iterator starting at the end + 1th element.
214214
*/
215-
const_iterator end() const;
215+
const_iterator end() const noexcept;
216+
217+
/**
218+
* \brief Tests whether this URI has a scheme component.
219+
* \return \c true if the URI has a scheme, \c false otherwise.
220+
*/
221+
bool has_scheme() const noexcept;
216222

217223
/**
218224
* \brief Returns the URI scheme.
219-
* \return The scheme, if it exists, or nullopt.
225+
* \return The scheme.
226+
* \pre has_scheme()
227+
*/
228+
string_view scheme() const noexcept;
229+
230+
/**
231+
* \brief Tests whether this URI has a user info component.
232+
* \return \c true if the URI has a user info, \c false otherwise.
220233
*/
221-
optional<string_view> scheme() const;
234+
bool has_user_info() const noexcept;
222235

223236
/**
224237
* \brief Returns the URI user info.
225-
* \return The user info, if it exists, or nullopt.
238+
* \return The user info.
239+
* \pre has_user_info()
226240
*/
227-
optional<string_view> user_info() const;
241+
string_view user_info() const noexcept;
242+
243+
/**
244+
* \brief Tests whether this URI has a host component.
245+
* \return \c true if the URI has a host, \c false otherwise.
246+
*/
247+
bool has_host() const noexcept;
228248

229249
/**
230250
* \brief Returns the URI host.
231-
* \return The host, if it exists, or nullopt.
251+
* \return The host.
252+
* \pre has_host()
232253
*/
233-
optional<string_view> host() const;
254+
string_view host() const noexcept;
255+
256+
/**
257+
* \brief Tests whether this URI has a port component.
258+
* \return \c true if the URI has a port, \c false otherwise.
259+
*/
260+
bool has_port() const noexcept;
234261

235262
/**
236263
* \brief Returns the URI port.
237-
* \return The port, if it exists, or nullopt.
264+
* \return The port.
265+
* \pre has_port()
238266
*/
239-
optional<string_view> port() const;
267+
string_view port() const noexcept;
240268

241269
/**
242270
* \brief Returns the URI port as an integer.
243-
* \return The port, if it exists, or nullopt.
271+
* \return The port number.
272+
* \pre has_port()
244273
*/
245274
template <typename IntT>
246-
optional<IntT> port(typename std::is_integral<IntT>::type * = 0) const {
247-
if (auto p = port()) {
248-
try {
249-
return std::stoi(string_type(std::begin(*p), std::end(*p)));
250-
} catch (std::exception &) {
251-
return optional<IntT>();
252-
}
275+
IntT port(typename std::is_integral<IntT>::type * = 0) const {
276+
assert(has_port());
277+
278+
auto p = port();
279+
try {
280+
return std::stoi(port().to_string());
281+
}
282+
catch (std::exception &) {
283+
assert(!"Unable to parse port number.");
253284
}
254-
return optional<IntT>();
255285
}
256286

287+
/**
288+
* \brief Tests whether this URI has a path component.
289+
* \return \c true if the URI has a path, \c false otherwise.
290+
*/
291+
bool has_path() const noexcept;
292+
257293
/**
258294
* \brief Returns the URI path.
259-
* \return The path, if it exists, or nullopt.
295+
* \return The path.
296+
* \pre has_path()
297+
*/
298+
string_view path() const noexcept;
299+
300+
/**
301+
* \brief Tests whether this URI has a query component.
302+
* \return \c true if the URI has a query, \c false otherwise.
260303
*/
261-
optional<string_view> path() const;
304+
bool has_query() const noexcept;
262305

263306
/**
264307
* \brief Returns the URI query.
265-
* \return The query, if it exists, or nullopt.
308+
* \return The query.
309+
* \pre has_query()
266310
*/
267-
optional<string_view> query() const;
311+
string_view query() const noexcept;
312+
313+
/**
314+
* \brief Tests whether this URI has a fragment component.
315+
* \return \c true if the URI has a fragment, \c false otherwise.
316+
*/
317+
bool has_fragment() const noexcept;
268318

269319
/**
270320
* \brief Returns the URI fragment.
271-
* \return The fragment, if it exists, or nullopt.
321+
* \return The fragment.
322+
* \pre has_fragment()
323+
*/
324+
string_view fragment() const noexcept;
325+
326+
/**
327+
* \brief Tests whether this URI has a valid authority.
328+
* \return \c true if the URI has an authority, \c false otherwise.
272329
*/
273-
optional<string_view> fragment() const;
330+
bool has_authority() const noexcept;
274331

275332
/**
276333
* \brief Returns the URI authority.
277-
* \return The authority, if it exists, or nullopt.
334+
* \return The authority.
278335
*/
279-
optional<string_view> authority() const;
336+
string_view authority() const noexcept;
280337

281338
#if !defined(NETWORK_URI_MSVC)
282339
/**
@@ -333,14 +390,14 @@ class uri {
333390
* \brief Checks if the uri is absolute, i.e. it has a scheme.
334391
* \returns \c true if it is absolute, \c false if it is relative.
335392
*/
336-
bool is_absolute() const;
393+
bool is_absolute() const noexcept;
337394

338395
/**
339396
* \brief Checks if the uri is opaque, i.e. if it doesn't have an
340397
* authority.
341398
* \returns \c true if it is opaque, \c false if it is hierarchical.
342399
*/
343-
bool is_opaque() const;
400+
bool is_opaque() const noexcept;
344401

345402
/**
346403
* \brief Normalizes a uri object at a given level in the

0 commit comments

Comments
 (0)