5#ifndef UPA_PUBLIC_SUFFIX_LIST_H
6#define UPA_PUBLIC_SUFFIX_LIST_H
19# include <string_view>
20# include <unordered_map>
36template <
class StrT, enable_if_str_arg_t<StrT> = 0>
38 const auto inp = make_str_arg(str_host);
39 const auto* first = inp.begin();
40 const auto* last = inp.end();
41 const auto* ptr = first;
42 for (; index > 0; --index) {
45 return static_cast<std::size_t
>(ptr - first);
46 const auto c = util::to_unsigned(*ptr);
52 const auto cp = url_utf::read_utf_char(ptr, last).value;
54 if (cp == 0x3002 || cp == 0xFF0E || cp == 0xFF61)
59 return static_cast<std::size_t
>(ptr - first);
93 static constexpr std::uint8_t DIFF_MASK = 0x03;
94 static constexpr std::uint8_t IS_RULE = 0x04;
95 static constexpr std::uint8_t IS_ICANN = 0x08;
96 static constexpr std::uint8_t IS_PRIVATE = 0x10;
110 [[nodiscard]]
constexpr operator bool() const noexcept {
114 [[nodiscard]]
constexpr bool is_icann() const noexcept {
115 return (
code_ & IS_ICANN) != 0;
119 return (
code_ & IS_PRIVATE) != 0;
123 return (
code_ & DIFF_MASK) == 3;
127 return (
code_ & IS_RULE) != 0;
143 inline bool load(
const std::filesystem::path& filename) {
144 std::ifstream finp(filename, std::ios::in | std::ios::binary);
145 return finp &&
load(finp);
151 UPA_API
bool load(std::istream& input_stream);
202 template <
class StrT, enable_if_str_arg_t<StrT> = 0>
203 [[nodiscard]]
inline std::string
get_suffix(
const StrT& str_host,
241 return get_host_suffix_info(host.
name(), opt);
253 template <
class StrT, enable_if_str_arg_t<StrT> = 0>
306 return get_host_suffix_view(host.name(), opt);
325 template <
class StrT, enable_if_str_arg_t<StrT> = 0>
327 -> std::basic_string_view<typename str_arg<str_arg_char_t<StrT>>::value_type> {
329 const auto arg = make_str_arg(str_host);
332 const auto res = get_host_suffix_info(host.
name(), opt);
335 return { arg.data() + pos, arg.size() - pos };
357 UPA_API
result get_host_suffix_info(
std::string_view hostname,
option opt) const;
359 inline
std::string_view get_host_suffix_view(
std::string_view hostname,
option opt)
const {
360 const auto res = get_host_suffix_info(hostname, opt);
362 return hostname.substr(res.first_label_pos);
368#ifdef __cpp_lib_generic_unordered_lookup
371 using hash_type = std::hash<std::string_view>;
372 using is_transparent = void;
374 inline std::size_t operator()(
const char* str)
const {
return hash_type{}(str); }
375 inline std::size_t operator()(std::string_view str)
const {
return hash_type{}(str); }
376 inline std::size_t operator()(std::string
const& str)
const {
return hash_type{}(str); }
378 using map_type = std::unordered_map<std::string, label_item, string_hash, std::equal_to<>>;
380 using map_type = std::unordered_map<std::string, label_item>;
383 std::uint8_t code = 0;
384 std::unique_ptr<map_type> children;
386 inline bool operator==(
const label_item& b)
const {
387 return code == b.code && (
388 (!children && !b.children) ||
389 (children && b.children && *children == *b.children));
400 :
public std::true_type {};
UPA_API public_suffix_list(public_suffix_list &&) noexcept
result get_suffix_info(const StrT &str_host, option opt=option::public_suffix) const
Get public suffix or registrable domain information.
result get_suffix_info(const url_host &host, option opt=option::public_suffix) const
Get public suffix or registrable domain information.
std::string_view get_suffix_view(const url_host &host, option opt=option::public_suffix) const
Get public suffix or registrable domain as string view.
UPA_API public_suffix_list()
UPA_API ~public_suffix_list()
result get_suffix_info(const url &url, option opt=option::public_suffix) const
Get public suffix or registrable domain information.
UPA_API void push_line(push_context &ctx, std::string_view line)
Push a line of the Public Suffix List file.
UPA_API bool operator==(const public_suffix_list &other) const
Compares *this with other.
std::string get_suffix(const StrT &str_host, option opt=option::public_suffix) const
Get public suffix or registrable domain.
UPA_API bool finalize(push_context &ctx)
Finalizes when all chunks are processed.
option
Options for public_suffix_list::get_suffix, public_suffix_list::get_suffix_info and public_suffix_lis...
@ public_suffix
to get public suffix
@ registrable_domain
to get registrable domain
UPA_API void push(push_context &ctx, std::string_view buff)
Push a chunk of the Public Suffix List file.
bool load(const std::filesystem::path &filename)
Load Public Suffix List from file.
auto get_suffix_view(const StrT &str_host, option opt=option::public_suffix) const -> std::basic_string_view< typename str_arg< str_arg_char_t< StrT > >::value_type >
Get public suffix or registrable domain as string view.
UPA_API bool load(std::istream &input_stream)
Load Public Suffix List from stream.
std::string_view get_suffix_view(const url &url, option opt=option::public_suffix) const
Get public suffix or registrable domain as string view.
std::string_view name() const
bool hostname(const StrT &str)
The hostname setter.
HostType host_type() const noexcept
The host_type getter.
constexpr std::size_t get_label_pos_by_index(const StrT &str_host, std::size_t index)
Get label position in hostname by it's index.
Push context for public_suffix_list::push_line, public_suffix_list::push and public_suffix_list::fina...
Return value type of the public_suffix_list::get_suffix_info function.
constexpr bool is_icann() const noexcept
std::size_t first_label_pos
First label position of public suffix or registrable domain in hostname.
constexpr bool wildcard_rule() const noexcept
constexpr bool is_private() const noexcept
constexpr bool is_rules_match() const noexcept
std::size_t first_label_ind
First label index of public suffix or registrable domain in hostname.