Skip to content

Commit cdf84fb

Browse files
committed
Add the PSL's push function that supports bytes as input
1 parent 22d96c4 commit cdf84fb

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/upa_url_bind.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ inline std::string_view to_string_view(nb::str str) {
2929
return {};
3030
}
3131

32+
inline std::string_view to_string_view(nb::bytes bytes) {
33+
return { bytes.c_str(), bytes.size() };
34+
}
35+
3236
inline nb::str to_str(std::string_view sv) {
3337
return nb::str{ sv.data(), sv.length() };
3438
}
@@ -41,6 +45,9 @@ class public_suffix_list_py : public upa::public_suffix_list {
4145
inline void push(std::string_view buff) {
4246
upa::public_suffix_list::push(ctx_, buff);
4347
}
48+
inline void push_bytes(nb::bytes bytes) {
49+
upa::public_suffix_list::push(ctx_, to_string_view(bytes));
50+
}
4451
inline bool finalize() {
4552
return upa::public_suffix_list::finalize(ctx_);
4653
}
@@ -201,6 +208,7 @@ NB_MODULE(upa_url, m) {
201208
.def(nb::init<>())
202209
.def("push_line", &public_suffix_list_py::push_line, nb::arg("line"))
203210
.def("push", &public_suffix_list_py::push, nb::arg("buff"))
211+
.def("push", &public_suffix_list_py::push_bytes, nb::arg("buff"))
204212
.def("finalize", &public_suffix_list_py::finalize)
205213

206214
// Load Public Suffix List from file

tests/test_psl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def test_psl_push(self):
1616
psl = PSL()
1717
psl.push('githu')
1818
psl.push('b.io')
19+
psl.push(b'\ngov.lt')
1920
self.assertTrue(psl.finalize())
21+
self.assertEqual(psl.public_suffix('data.gov.lt'), 'gov.lt')
2022
self.assertEqual(psl.public_suffix('upa-url.github.io.'), 'github.io.')
2123
self.assertEqual(psl.public_suffix('abc.io.'), 'io.')
2224

0 commit comments

Comments
 (0)