Repository navigation
perf(runtime): shrink ada and URL/console code; optional system ICU for IDNA #606
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a566350
perf(runtime): keep ada's symbols out of the framework's exports
edusperoni 3d99153
feat(runtime): optional system ICU for ada's Unicode tables
edusperoni 769d816
perf(runtime): parse each URL once and drop std::regex from console
edusperoni e7fc785
test(runtime): cover URL bases, canParse, IDN hosts and non-ASCII URL…
edusperoni 28ac889
fix(runtime): URL.canParse treats an undefined base as absent
edusperoni e41ea9b
fix(runtime): keep ada's non-ASCII domain length limit in the system …
edusperoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #include "system_idna.h" | ||
|
|
||
| #if NS_ADA_SYSTEM_IDNA | ||
| #include <unicode/uchar.h> | ||
| #include <unicode/uidna.h> | ||
|
|
||
| #include "ada.h" | ||
|
|
||
| // Autolinks libicucore so that turning the flag on is the only switch. | ||
| __asm__(".linker_option \"-licucore\""); | ||
|
|
||
| namespace tns { | ||
|
|
||
| static const UIDNA* Uts46() { | ||
| static const UIDNA* idna = [] { | ||
| UErrorCode err = U_ZERO_ERROR; | ||
| // Explicit nontransitional bits: UIDNA_DEFAULT changed meaning in ICU 76. | ||
| return uidna_openUTS46(UIDNA_CHECK_BIDI | UIDNA_CHECK_CONTEXTJ | | ||
| UIDNA_NONTRANSITIONAL_TO_ASCII | | ||
| UIDNA_NONTRANSITIONAL_TO_UNICODE, | ||
| &err); | ||
| }(); | ||
| return idna; | ||
| } | ||
|
|
||
| std::string SystemIdnaToAscii(std::string_view input) { | ||
| // WHATWG "domain to ASCII" with beStrict=false lowercases an all-ASCII | ||
| // domain whatever ToASCII would say; ICU rejects e.g. invalid "xn--" labels. | ||
| bool ascii = true; | ||
| for (unsigned char c : input) { | ||
| ascii &= c < 0x80; | ||
| } | ||
| if (ascii) { | ||
| std::string lower(input); | ||
| for (char& c : lower) { | ||
| if (c >= 'A' && c <= 'Z') { | ||
| c += 'a' - 'A'; | ||
| } | ||
| } | ||
| return lower; | ||
| } | ||
| // ada bounds the non-ASCII inputs it converts; keep the same limit. | ||
| if (input.size() > ada::idna::max_domain_input_bytes) { | ||
| return {}; | ||
| } | ||
|
|
||
| // CheckHyphens and VerifyDnsLength are off for URLs, and ICU has no options | ||
| // for them, so their errors are not failures. | ||
| constexpr uint32_t kIgnoredErrors = | ||
| UIDNA_ERROR_EMPTY_LABEL | UIDNA_ERROR_LABEL_TOO_LONG | | ||
| UIDNA_ERROR_DOMAIN_NAME_TOO_LONG | UIDNA_ERROR_LEADING_HYPHEN | | ||
| UIDNA_ERROR_TRAILING_HYPHEN | UIDNA_ERROR_HYPHEN_3_4; | ||
|
|
||
| const UIDNA* idna = Uts46(); | ||
| if (idna == nullptr) { | ||
| return {}; | ||
| } | ||
| std::string out(input.size() * 2 + 64, '\0'); | ||
| for (int attempt = 0; attempt < 2; attempt++) { | ||
| UIDNAInfo info = UIDNA_INFO_INITIALIZER; | ||
| UErrorCode err = U_ZERO_ERROR; | ||
| int32_t length = uidna_nameToASCII_UTF8( | ||
| idna, input.data(), static_cast<int32_t>(input.size()), out.data(), | ||
| static_cast<int32_t>(out.size()), &info, &err); | ||
| if (err == U_BUFFER_OVERFLOW_ERROR) { | ||
| out.resize(length); | ||
| continue; | ||
| } | ||
| if (U_FAILURE(err) || (info.errors & ~kIgnoredErrors) != 0 || | ||
| length == 0) { | ||
| return {}; | ||
| } | ||
| out.resize(length); | ||
| return out; | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| bool SystemIsNameCodePoint(char32_t code_point, bool first) { | ||
| return u_hasBinaryProperty(static_cast<UChar32>(code_point), | ||
| first ? UCHAR_ID_START : UCHAR_ID_CONTINUE); | ||
| } | ||
|
|
||
| } // namespace tns | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #ifndef NS_ADA_SYSTEM_IDNA_H | ||
| #define NS_ADA_SYSTEM_IDNA_H | ||
|
|
||
| // 1: ada uses the system ICU (libicucore) for UTS #46 host conversion and for | ||
| // URLPattern's identifier code point check, so its own compressed Unicode | ||
| // tables dead-strip (~80 KB). Results follow the OS's Unicode data: iOS | ||
| // 13-17.3 ship ICU 64-72 (Unicode 12.1-15.0), which rejects newer characters | ||
| // and maps U+1E9E to "ss" where current UTS #46 keeps "ß". | ||
| // | ||
| // ada.cpp is vendored: after upgrading it, re-add the include of this header | ||
| // below `#include "ada.h"` and the NS_ADA_SYSTEM_IDNA branches in | ||
| // `ada::unicode::to_ascii` and `ada::idna::valid_name_code_point`. Both must | ||
| // be switched, or the tables stay linked. | ||
| #ifndef NS_ADA_SYSTEM_IDNA | ||
| #define NS_ADA_SYSTEM_IDNA 0 | ||
| #endif | ||
|
|
||
| #if NS_ADA_SYSTEM_IDNA | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace tns { | ||
|
|
||
| // Same contract as ada::idna::to_ascii: an empty string means failure. | ||
| __attribute__((visibility("hidden"))) std::string SystemIdnaToAscii( | ||
| std::string_view input); | ||
|
|
||
| // The table lookup of ada::idna::valid_name_code_point: ID_Start when | ||
| // `first`, otherwise ID_Continue. | ||
| __attribute__((visibility("hidden"))) bool SystemIsNameCodePoint( | ||
| char32_t code_point, bool first); | ||
|
|
||
| } // namespace tns | ||
| #endif | ||
|
|
||
| #endif // NS_ADA_SYSTEM_IDNA_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.