Commit ab7b9114 authored by Dominik Charousset's avatar Dominik Charousset

Add can_convert utility function to URI

parent 025ee58e
......@@ -121,6 +121,11 @@ public:
int compare(string_view x) const noexcept;
// -- parsing ----------------------------------------------------------------
/// Returns whether `parse` would produce a valid URI.
static bool can_parse(string_view str) noexcept;
// -- friend functions -------------------------------------------------------
friend CAF_CORE_EXPORT error inspect(caf::serializer& dst, uri& x);
......
......@@ -98,6 +98,65 @@ int uri::compare(string_view x) const noexcept {
return string_view{str()}.compare(x);
}
// -- parsing ------------------------------------------------------------------
namespace {
class nop_builder {
public:
template <class T>
nop_builder& scheme(T&&) {
return *this;
}
template <class T>
nop_builder& userinfo(T&&) {
return *this;
}
template <class T>
nop_builder& host(T&&) {
return *this;
}
template <class T>
nop_builder& port(T&&) {
return *this;
}
template <class T>
nop_builder& path(T&&) {
return *this;
}
template <class T>
nop_builder& query(T&&) {
return *this;
}
template <class T>
nop_builder& fragment(T&&) {
return *this;
}
};
} // namespace
bool uri::can_parse(string_view str) noexcept {
string_parser_state ps{str.begin(), str.end()};
nop_builder builder;
if (ps.consume('<')) {
detail::parser::read_uri(ps, builder);
if (ps.code > pec::trailing_character)
return false;
if (!ps.consume('>'))
return false;
} else {
detail::parser::read_uri(ps, builder);
}
return ps.code == pec::success;
}
// -- friend functions ---------------------------------------------------------
error inspect(caf::serializer& dst, uri& x) {
......
......@@ -209,6 +209,7 @@ uri operator "" _u(const char* cstr, size_t cstr_len) {
bool operator "" _i(const char* cstr, size_t cstr_len) {
uri result;
string_view str{cstr, cstr_len};
CAF_CHECK(!uri::can_parse(str));
auto err = parse(str, result);
return err != none;
}
......@@ -278,7 +279,11 @@ CAF_TEST(builder construction) {
CAF_CHECK_EQUAL(escaped, "hi%20there://it%27s@me%2F/file%201#%5B42%5D");
}
#define ROUNDTRIP(str) CAF_CHECK_EQUAL(str##_u, str)
#define ROUNDTRIP(str) \
do { \
CAF_CHECK(uri::can_parse(str)); \
CAF_CHECK_EQUAL(str##_u, str); \
} while (false)
CAF_TEST(from string) {
// all combinations of components
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment