Skip to content

Commit 39e114a

Browse files
committed
feat: add ada::idna methods to c api
1 parent 725532a commit 39e114a

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

include/ada_c.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,8 @@ bool ada_has_search(ada_url result);
9797
// returns a pointer to the internal url_aggregator::url_components
9898
const ada_url_components* ada_get_components(ada_url result);
9999

100+
// idna methods
101+
ada_owned_string ada_idna_to_unicode(const char* input, size_t length);
102+
ada_owned_string ada_idna_to_ascii(const char* input, size_t length);
103+
100104
#endif // ADA_C_H

src/ada_c.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,23 @@ const ada_url_components* ada_get_components(ada_url result) noexcept {
369369
}
370370
return reinterpret_cast<const ada_url_components*>(&r->get_components());
371371
}
372+
373+
ada_owned_string ada_idna_to_unicode(const char* input, size_t length) {
374+
std::string out = ada::idna::to_unicode(std::string_view(input, length));
375+
ada_owned_string owned{};
376+
owned.length = out.length();
377+
owned.data = new char[owned.length];
378+
memcpy((void*)owned.data, out.data(), owned.length);
379+
return owned;
380+
}
381+
382+
ada_owned_string ada_idna_to_ascii(const char* input, size_t length) {
383+
std::string out = ada::idna::to_ascii(std::string_view(input, length));
384+
ada_owned_string owned{};
385+
owned.length = out.size();
386+
owned.data = new char[owned.length];
387+
memcpy((void*)owned.data, out.data(), owned.length);
388+
return owned;
389+
}
390+
372391
} // extern "C"

tests/ada_c.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,19 @@ TEST(ada_c, ada_url_components) {
135135

136136
SUCCEED();
137137
}
138+
139+
TEST(ada_c, ada_idna) {
140+
std::string_view ascii_input = "straße.de";
141+
std::string_view unicode_input = "xn--strae-oqa.de";
142+
ada_owned_string ascii =
143+
ada_idna_to_ascii(ascii_input.data(), ascii_input.length());
144+
ASSERT_EQ(std::string_view(ascii.data, ascii.length), unicode_input);
145+
146+
ada_owned_string unicode =
147+
ada_idna_to_unicode(unicode_input.data(), unicode_input.length());
148+
ASSERT_EQ(std::string_view(unicode.data, unicode.length), ascii_input);
149+
150+
ada_free_owned_string(ascii);
151+
ada_free_owned_string(unicode);
152+
SUCCEED();
153+
}

0 commit comments

Comments
 (0)