Skip to content

feat: add ada::idna methods to c api #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/emscripten.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
- name: Build
run: cmake --build buildwasm
- name: Test
run: ctest --test-dir buildwasm
run: ctest --test-dir buildwasm
4 changes: 4 additions & 0 deletions include/ada_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ bool ada_has_search(ada_url result);
// returns a pointer to the internal url_aggregator::url_components
const ada_url_components* ada_get_components(ada_url result);

// idna methods
ada_owned_string ada_idna_to_unicode(const char* input, size_t length);
ada_owned_string ada_idna_to_ascii(const char* input, size_t length);

#endif // ADA_C_H
19 changes: 19 additions & 0 deletions src/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,23 @@ const ada_url_components* ada_get_components(ada_url result) noexcept {
}
return reinterpret_cast<const ada_url_components*>(&r->get_components());
}

ada_owned_string ada_idna_to_unicode(const char* input, size_t length) {
std::string out = ada::idna::to_unicode(std::string_view(input, length));
ada_owned_string owned{};
owned.length = out.length();
owned.data = new char[owned.length];
memcpy((void*)owned.data, out.data(), owned.length);
return owned;
}

ada_owned_string ada_idna_to_ascii(const char* input, size_t length) {
std::string out = ada::idna::to_ascii(std::string_view(input, length));
ada_owned_string owned{};
owned.length = out.size();
owned.data = new char[owned.length];
memcpy((void*)owned.data, out.data(), owned.length);
return owned;
}

} // extern "C"
16 changes: 16 additions & 0 deletions tests/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,19 @@ TEST(ada_c, ada_url_components) {

SUCCEED();
}

TEST(ada_c, ada_idna) {
std::string_view ascii_input = "straße.de";
std::string_view unicode_input = "xn--strae-oqa.de";
ada_owned_string ascii =
ada_idna_to_ascii(ascii_input.data(), ascii_input.length());
ASSERT_EQ(std::string_view(ascii.data, ascii.length), unicode_input);

ada_owned_string unicode =
ada_idna_to_unicode(unicode_input.data(), unicode_input.length());
ASSERT_EQ(std::string_view(unicode.data, unicode.length), ascii_input);

ada_free_owned_string(ascii);
ada_free_owned_string(unicode);
SUCCEED();
}
6 changes: 4 additions & 2 deletions tests/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
link_libraries(ada)
add_executable(wasm wasm.cpp)
set_target_properties(wasm PROPERTIES LINK_FLAGS "-Os -s WASM=1 -s ENVIRONMENT=node -s EXPORT_NAME=loadWASM -s MODULARIZE=1 --bind --no-entry")
# Node
add_executable(wasm-node wasm.cpp)
set_target_properties(wasm-node PROPERTIES LINK_FLAGS "-Os -s ENVIRONMENT=node -s EXPORT_NAME=loadWASM -s MODULARIZE=1 --bind")

configure_file(test.js.in test.js)

find_program(NODEJS_BINARY NAMES node nodejs)
Expand Down
6 changes: 2 additions & 4 deletions tests/wasm/test.js.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const child_process = require('child_process');
const assert = require('assert');
const assert = require('node:assert');
const test = require('node:test');
const wasm = require('${CMAKE_CURRENT_BINARY_DIR}/wasm');
const wasm = require('${CMAKE_CURRENT_BINARY_DIR}/wasm-node');

function toJS(obj) {
const result = {};
Expand Down Expand Up @@ -29,7 +28,6 @@ const expected = {

test('wasm', async () => {
const { parse } = await wasm();
console.log();
assert.deepStrictEqual(toJS(parse('https://google.com/?q=Yagiz#Nizipli')), expected);
});

2 changes: 1 addition & 1 deletion tools/release/lib/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_release(

except Exception as exp:
raise Exception(
f"create_release: Error creating release/tag {tag}: {str(exp)}"
f"create_release: Error creating release/tag {tag}: {exp!s}"
) from exp


Expand Down