1212
1313namespace ada {
1414
15+ enum class url_search_params_iter_type {
16+ KEYS,
17+ VALUES,
18+ ENTRIES,
19+ };
20+
21+ template <typename T, url_search_params_iter_type Type>
22+ struct url_search_params_iter ;
23+
24+ typedef std::pair<std::string_view, std::string_view> key_value_view_pair;
25+
26+ using url_search_params_keys_iter =
27+ url_search_params_iter<std::string_view, url_search_params_iter_type::KEYS>;
28+ using url_search_params_values_iter =
29+ url_search_params_iter<std::string_view,
30+ url_search_params_iter_type::VALUES>;
31+ using url_search_params_entries_iter =
32+ url_search_params_iter<key_value_view_pair,
33+ url_search_params_iter_type::ENTRIES>;
34+
1535/* *
1636 * @see https://url.spec.whatwg.org/#interface-urlsearchparams
1737 */
@@ -74,6 +94,42 @@ struct url_search_params {
7494 */
7595 inline std::string to_string ();
7696
97+ /* *
98+ * Returns a simple JS-style iterator over all of the keys in this
99+ * url_search_params. The keys in the iterator are not unique. The valid
100+ * lifespan of the iterator is tied to the url_search_params. The iterator
101+ * must be freed when you're done with it.
102+ * @see https://url.spec.whatwg.org/#interface-urlsearchparams
103+ */
104+ inline url_search_params_keys_iter get_keys ();
105+
106+ /* *
107+ * Returns a simple JS-style iterator over all of the values in this
108+ * url_search_params. The valid lifespan of the iterator is tied to the
109+ * url_search_params. The iterator must be freed when you're done with it.
110+ * @see https://url.spec.whatwg.org/#interface-urlsearchparams
111+ */
112+ inline url_search_params_values_iter get_values ();
113+
114+ /* *
115+ * Returns a simple JS-style iterator over all of the entries in this
116+ * url_search_params. The entries are pairs of keys and corresponding values.
117+ * The valid lifespan of the iterator is tied to the url_search_params. The
118+ * iterator must be freed when you're done with it.
119+ * @see https://url.spec.whatwg.org/#interface-urlsearchparams
120+ */
121+ inline url_search_params_entries_iter get_entries ();
122+
123+ /* *
124+ * C++ style conventional iterator support. const only because we
125+ * do not really want the params to be modified via the iterator.
126+ */
127+ inline const auto begin () const { return params.begin (); }
128+ inline const auto end () const { return params.end (); }
129+ inline const auto front () const { return params.front (); }
130+ inline const auto back () const { return params.back (); }
131+ inline const auto operator [](size_t index) const { return params[index]; }
132+
77133 private:
78134 typedef std::pair<std::string, std::string> key_value_pair;
79135 std::vector<key_value_pair> params{};
@@ -82,7 +138,43 @@ struct url_search_params {
82138 * @see https://url.spec.whatwg.org/#concept-urlencoded-parser
83139 */
84140 void initialize (std::string_view init);
141+
142+ template <typename T, url_search_params_iter_type Type>
143+ friend struct url_search_params_iter ;
85144}; // url_search_params
86145
146+ /* *
147+ * Implements a non-conventional iterator pattern that is closer in style to
148+ * JavaScript's definition of an iterator.
149+ *
150+ * @see https://webidl.spec.whatwg.org/#idl-iterable
151+ */
152+ template <typename T, url_search_params_iter_type Type>
153+ struct url_search_params_iter {
154+ inline url_search_params_iter () : params(EMPTY) {}
155+ url_search_params_iter (const url_search_params_iter &u) = default ;
156+ url_search_params_iter (url_search_params_iter &&u) noexcept = default ;
157+ url_search_params_iter &operator =(url_search_params_iter &&u) noexcept =
158+ default ;
159+ url_search_params_iter &operator =(const url_search_params_iter &u) = default ;
160+ ~url_search_params_iter () = default ;
161+
162+ /* *
163+ * Return the next item in the iterator or std::nullopt if done.
164+ */
165+ inline std::optional<T> next ();
166+
167+ inline bool has_next ();
168+
169+ private:
170+ static url_search_params EMPTY;
171+ inline url_search_params_iter (url_search_params ¶ms_) : params(params_) {}
172+
173+ url_search_params ¶ms;
174+ size_t pos = 0 ;
175+
176+ friend struct url_search_params ;
177+ };
178+
87179} // namespace ada
88180#endif
0 commit comments