mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-07 00:29:15 +00:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
parent
950e819ee7
commit
93712b24bf
Notes:
github-actions[bot]
2024-11-10 11:51:52 +00:00
Author: https://github.com/trflynn89
Commit: 93712b24bf
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256
Reviewed-by: https://github.com/sideshowbarker
4547 changed files with 104 additions and 113 deletions
60
Libraries/LibUnicode/IDNA.cpp
Normal file
60
Libraries/LibUnicode/IDNA.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Simon Wanner <simon@skyrising.xyz>
|
||||
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibUnicode/ICU.h>
|
||||
#include <LibUnicode/IDNA.h>
|
||||
|
||||
#include <unicode/idna.h>
|
||||
|
||||
namespace Unicode::IDNA {
|
||||
|
||||
// https://www.unicode.org/reports/tr46/#ToASCII
|
||||
ErrorOr<String> to_ascii(Utf8View domain_name, ToAsciiOptions const& options)
|
||||
{
|
||||
u32 icu_options = UIDNA_DEFAULT;
|
||||
|
||||
if (options.check_bidi == CheckBidi::Yes)
|
||||
icu_options |= UIDNA_CHECK_BIDI;
|
||||
if (options.check_joiners == CheckJoiners::Yes)
|
||||
icu_options |= UIDNA_CHECK_CONTEXTJ;
|
||||
if (options.use_std3_ascii_rules == UseStd3AsciiRules::Yes)
|
||||
icu_options |= UIDNA_USE_STD3_RULES;
|
||||
if (options.transitional_processing == TransitionalProcessing::No)
|
||||
icu_options |= UIDNA_NONTRANSITIONAL_TO_ASCII | UIDNA_NONTRANSITIONAL_TO_UNICODE;
|
||||
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
|
||||
auto idna = adopt_own_if_nonnull(icu::IDNA::createUTS46Instance(icu_options, status));
|
||||
if (icu_failure(status))
|
||||
return Error::from_string_literal("Unable to create an IDNA instance");
|
||||
|
||||
StringBuilder builder { domain_name.as_string().length() };
|
||||
icu::StringByteSink sink { &builder };
|
||||
|
||||
icu::IDNAInfo info;
|
||||
idna->nameToASCII_UTF8(icu_string_piece(domain_name.as_string()), sink, info, status);
|
||||
|
||||
auto errors = info.getErrors();
|
||||
|
||||
if (options.check_hyphens == CheckHyphens::No) {
|
||||
errors &= ~UIDNA_ERROR_HYPHEN_3_4;
|
||||
errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
|
||||
errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
|
||||
}
|
||||
if (options.verify_dns_length == VerifyDnsLength::No) {
|
||||
errors &= ~UIDNA_ERROR_EMPTY_LABEL;
|
||||
errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
|
||||
errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
|
||||
}
|
||||
|
||||
if (icu_failure(status) || errors != 0)
|
||||
return Error::from_string_literal("Unable to convert domain to ASCII");
|
||||
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue