call out non-ASCII
[ghsmtp.git] / Domain.hpp
blob6b3ceb616a6c58fddcab64d91ce99e4d38e3319d
1 #ifndef DOMAIN_DOT_HPP
2 #define DOMAIN_DOT_HPP
4 #include "IP.hpp"
6 #include <iostream>
7 #include <string>
8 #include <string_view>
10 #include "iequal.hpp"
12 class Domain {
13 public:
14 Domain() = default;
16 explicit Domain(std::string_view dom) { set(dom); }
18 Domain& operator=(std::string_view dom)
20 set(dom);
21 return *this;
24 void set(std::string_view dom);
26 inline void clear();
28 bool empty() const { return ascii_.empty(); }
30 inline static std::string_view remove_trailing_dot(std::string_view a);
31 inline static bool match(std::string_view a, std::string_view b);
33 bool operator==(std::string_view rhs) const { return match(ascii_, rhs); }
34 bool operator!=(std::string_view rhs) const { return !(*this == rhs); }
36 bool operator==(Domain const& rhs) const { return match(ascii_, rhs.ascii_); }
37 bool operator!=(Domain const& rhs) const { return !(*this == rhs); }
39 bool is_address_literal() const { return is_address_literal_; }
40 bool is_unicode() const { return utf8() != ascii(); }
42 std::string const& ascii() const { return ascii_; }
43 std::string const& utf8() const { return utf8_; }
45 static bool validate(std::string_view dom);
47 private:
48 std::string ascii_;
49 std::string utf8_;
51 bool is_address_literal_{false};
54 inline void Domain::clear()
56 ascii_.clear();
57 utf8_.clear();
58 is_address_literal_ = false;
61 inline std::string_view Domain::remove_trailing_dot(std::string_view a)
63 if (a.length() && (a.back() == '.')) {
64 a.remove_suffix(1);
66 return a;
69 inline bool Domain::match(std::string_view a, std::string_view b)
71 return iequal(remove_trailing_dot(a), remove_trailing_dot(b));
74 inline std::ostream& operator<<(std::ostream& os, Domain const& dom)
76 if (dom.is_unicode())
77 return os << '{' << dom.ascii() << ',' << dom.utf8() << '}';
78 return os << dom.ascii();
81 namespace std {
82 template <>
83 struct hash<Domain> {
84 std::size_t operator()(Domain const& k) const
86 return hash<std::string>()(k.ascii());
89 } // namespace std
91 #endif // DOMAIN_DOT_HPP