libwordring
whatwg/html/html_atom.hpp
1 #pragma once
2 
3 #include <wordring/whatwg/html/parsing/atom_defs.hpp>
4 #include <wordring/whatwg/html/parsing/atom_tbl.hpp>
5 
6 #include <wordring/whatwg/infra/unicode.hpp>
7 
8 #include <string>
9 #include <string_view>
10 #include <type_traits>
11 
12 namespace wordring::whatwg::html
13 {
27  template <typename String, typename Name>
29  {
30  template <typename String1, typename Name1>
31  friend bool operator==(basic_html_atom<String1, Name1> const&, basic_html_atom<String1, Name1> const&);
32 
33  template <typename String1, typename Name1>
34  friend bool operator!=(basic_html_atom<String1, Name1> const&, basic_html_atom<String1, Name1> const&);
35 
36  template <typename String1, typename Name1>
37  friend bool operator==(basic_html_atom<String1, Name1> const&, Name1);
38 
39  template <typename String1, typename Name1>
40  friend bool operator!=(basic_html_atom<String1, Name1> const&, Name1);
41 
42  template <typename String1, typename Name1>
43  friend bool operator==(Name1, basic_html_atom<String1, Name1> const&);
44 
45  template <typename String1, typename Name1>
46  friend bool operator!=(Name1, basic_html_atom<String1, Name1> const&);
47 
48  template <typename String1, typename Name1>
49  friend bool operator==(basic_html_atom<String1, Name1> const&, String1 const&);
50 
51  template <typename String1, typename Name1>
52  friend bool operator!=(basic_html_atom<String1, Name1> const&, String1 const&);
53 
54  template <typename String1, typename Name1>
55  friend bool operator==(String1 const&, basic_html_atom<String1, Name1> const&);
56 
57  template <typename String1, typename Name1>
58  friend bool operator!=(String1 const&, basic_html_atom<String1, Name1> const&);
59 
60  public:
61  using string_type = String;
62  using name_type = Name;
63 
64  public:
66  : m_string()
67  , m_i(static_cast<name_type>(0))
68  {
69  }
70 
71  basic_html_atom(string_type const& s)
72  : m_string(s)
73  , m_i(static_cast<name_type>(0))
74  {
75  using namespace wordring::whatwg::html::parsing;
76 
77  if constexpr (std::is_same_v<name_type, ns_name>)
78  {
79  auto it = ns_uri_atom_tbl.find(encoding_cast<std::u32string>(s));
80  if (it != ns_uri_atom_tbl.end()) m_i = it->second;
81  }
82  else if constexpr (std::is_same_v<name_type, tag_name>)
83  {
84  auto it = tag_atom_tbl.find(encoding_cast<std::u32string>(s));
85  if (it != tag_atom_tbl.end()) m_i = it->second;
86  }
87  else if constexpr (std::is_same_v<name_type, attribute_name>)
88  {
89  auto it = attribute_atom_tbl.find(encoding_cast<std::u32string>(s));
90  if (it != attribute_atom_tbl.end()) m_i = it->second;
91  }
92  }
93 
94  basic_html_atom(name_type i)
95  : m_string()
96  , m_i(i)
97  {
98  }
99 
105  operator string_type() const
106  {
107  using namespace wordring::whatwg::html::parsing;
108 
109  if (!m_string.empty()) return m_string;
110 
111  std::u32string s;
112  std::uint32_t i = static_cast<std::uint32_t>(m_i);
113 
114  if constexpr (std::is_same_v<name_type, ns_name>) s = ns_uri_tbl[i];
115  else if constexpr (std::is_same_v<name_type, tag_name>) s = tag_name_tbl[i];
116  else s = attribute_name_tbl[i];
117 
118  return encoding_cast<string_type>(s);
119  }
120 
121  operator name_type() const { return m_i; }
122 
123  protected:
124  string_type m_string;
125  name_type m_i;
126  };
127 
128  template <typename String1, typename Name1>
129  inline bool operator==(basic_html_atom<String1, Name1> const& lhs, basic_html_atom<String1, Name1> const& rhs)
130  {
131  if (lhs.m_i != static_cast<Name1>(0) && rhs.m_i != static_cast<Name1>(0)) return lhs.m_i == rhs.m_i;
132  return static_cast<String1>(lhs) == static_cast<String1>(rhs);
133  }
134 
135  template <typename String1, typename Name1>
136  inline bool operator==(basic_html_atom<String1, Name1> const& lhs, Name1 i)
137  {
138  return lhs == basic_html_atom<String1, Name1>(i);
139  }
140 
141  template <typename String1, typename Name1>
142  inline bool operator==(Name1 i, basic_html_atom<String1, Name1> const& rhs)
143  {
144  return basic_html_atom<String1, Name1>(i) == rhs;
145  }
146 
147  template <typename String1, typename Name1>
148  inline bool operator==(basic_html_atom<String1, Name1> const& lhs, String1 const& s)
149  {
150  return lhs == basic_html_atom<String1, Name1>(s);
151  }
152 
153  template <typename String1, typename Name1>
154  inline bool operator==(String1 const& s, basic_html_atom<String1, Name1> const& rhs)
155  {
156  return basic_html_atom<String1, Name1>(s) == rhs;
157  }
158 
159  template <typename String1, typename Name1>
160  inline bool operator!=(basic_html_atom<String1, Name1> const& lhs, basic_html_atom<String1, Name1> const& rhs)
161  {
162  return !(lhs == rhs);
163  }
164 
165  template <typename String1, typename Name1>
166  inline bool operator!=(basic_html_atom<String1, Name1> const& lhs, Name1 i)
167  {
168  return !(lhs == i);
169  }
170 
171 
172  template <typename String1, typename Name1>
173  inline bool operator!=(Name1 i, basic_html_atom<String1, Name1> const& rhs)
174  {
175  return !(i == rhs);
176  }
177 
178  template <typename String1, typename Name1>
179  inline bool operator!=(basic_html_atom<String1, Name1> const& lhs, String1 const& s)
180  {
181  return !(lhs == s);
182  }
183 
184  template <typename String1, typename Name1>
185  inline bool operator!=(String1 const& s, basic_html_atom<String1, Name1> const& rhs)
186  {
187  return !(s == rhs);
188  }
189 
190 }
wordring::whatwg::html
wordring::whatwg::html::basic_html_atom
列挙体と文字列の相互変換を行うクラス
Definition: whatwg/html/html_atom.hpp:28
wordring::whatwg::html::parsing