| 1 |
// Copyright 2016 Glyn Matthews. |
| 2 |
// Distributed under the Boost Software License, Version 1.0. |
| 3 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#ifndef NETWORK_DETAIL_URI_GRAMMAR_INC |
| 7 |
#define NETWORK_DETAIL_URI_GRAMMAR_INC |
| 8 |
|
| 9 |
#include <network/string_view.hpp> |
| 10 |
#include <cstdlib> |
| 11 |
#include <locale> |
| 12 |
#include <cstring> |
| 13 |
#include <string> |
| 14 |
|
| 15 |
namespace network { |
| 16 |
namespace detail { |
| 17 |
inline bool isalnum(string_view::const_iterator &it, |
| 18 |
string_view::const_iterator last) { |
| 19 |
if (it != last) { |
| 20 |
if (std::isalnum(*it, std::locale::classic())) { |
| 21 |
++it; |
| 22 |
return true; |
| 23 |
} |
| 24 |
} |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
inline bool isdigit(string_view::const_iterator &it, |
| 29 |
string_view::const_iterator last) { |
| 30 |
if (it != last) { |
| 31 |
if (std::isdigit(*it, std::locale::classic())) { |
| 32 |
++it; |
| 33 |
return true; |
| 34 |
} |
| 35 |
} |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
inline bool is_in(string_view::const_iterator &it, |
| 40 |
string_view::const_iterator last, const char *chars) { |
| 41 |
if (it != last) { |
| 42 |
auto length = std::strlen(chars); |
| 43 |
for (std::size_t i = 0; i < length; ++i) { |
| 44 |
if (*it == chars[i]) { |
| 45 |
++it; |
| 46 |
return true; |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
inline bool is_sub_delim(string_view::const_iterator &it, |
| 54 |
string_view::const_iterator last) { |
| 55 |
return is_in(it, last, "!$&'()*+,;="); |
| 56 |
} |
| 57 |
|
| 58 |
inline bool is_ucschar(string_view::const_iterator &it, |
| 59 |
string_view::const_iterator last) { |
| 60 |
if (it == last) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
inline bool is_private(string_view::const_iterator &it, |
| 68 |
string_view::const_iterator last) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
inline bool is_unreserved(string_view::const_iterator &it, |
| 73 |
string_view::const_iterator last) { |
| 74 |
return isalnum(it, last) || is_in(it, last, "-._~"); |
| 75 |
} |
| 76 |
|
| 77 |
inline bool is_pct_encoded(string_view::const_iterator &it, |
| 78 |
string_view::const_iterator last) { |
| 79 |
if (it == last) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
string_view::const_iterator it_copy = it; |
| 84 |
|
| 85 |
if (*it_copy == '%') { |
| 86 |
++it_copy; |
| 87 |
if (it_copy == last) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (std::isxdigit(*it_copy, std::locale::classic())) { |
| 93 |
++it_copy; |
| 94 |
if (it_copy == last) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if (std::isxdigit(*it_copy, std::locale::classic())) { |
| 100 |
++it_copy; |
| 101 |
it = it_copy; |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
inline bool is_pchar(string_view::const_iterator &it, |
| 109 |
string_view::const_iterator last) { |
| 110 |
return is_unreserved(it, last) || is_pct_encoded(it, last) || |
| 111 |
is_sub_delim(it, last) || is_in(it, last, ":@") || |
| 112 |
is_ucschar(it, last); |
| 113 |
} |
| 114 |
|
| 115 |
inline bool is_valid_port(string_view::const_iterator it) { |
| 116 |
const char *port_first = &(*it); |
| 117 |
char *port_last = 0; |
| 118 |
unsigned long value = std::strtoul(port_first, &port_last, 10); |
| 119 |
return (value < std::numeric_limits<unsigned short>::max()); |
| 120 |
} |
| 121 |
} // namespace detail |
| 122 |
} // namespace network |
| 123 |
|
| 124 |
#endif // NETWORK_DETAIL_URI_GRAMMAR_INC |