unicode-idna

    UTS #46 IDNA processing for MoonBit

    unicode
    idna
    uts46
    domain
    Download zip
    Version
    0.8.1
    License
    Apache-2.0
    Last updated
    yesterday
    Downloads
    17K

    #unicode-idna

    Unicode IDNA processing for MoonBit, with UTS #46 and optional IDNA2008 label validation. The generated tables target Unicode 18.0.0.

    moon add moonbit-community/unicode-idna

    to_ascii and to_unicode support IDNA2008 label validation with UTS #46 input processing. These options enable the IDNA2008 character and ContextO checks:

    • check_idna2008=true restricts the resulting Unicode labels to the IDNA2008 PVALID, CONTEXTJ, and CONTEXTO categories. Other code points raise DisallowedCodePoint(cp~).
    • check_contexto=true independently validates the RFC 5892 ContextO rules. Violations raise InvalidContextO(cp~).

    Both options default to false. They also check Unicode labels decoded from Punycode. Enable both alongside the default checks for IDNA2008 label validation. The check_joiners option checks CONTEXTJ rules and defaults to true.

    let ascii = try! @idna.to_ascii(
    "ÖBB.at",
    check_idna2008=true,
    check_contexto=true,
    )
    // "xn--bb-eka.at"

    Validation applies after UTS #46 mapping and NFC normalization. In the example, ÖBB.at is mapped to öbb.at, which passes IDNA2008 validation. Symbols such as ☕ are rejected by check_idna2008 even though UTS #46 allows them.

    See the repository root README for the remaining options and examples.

    IdnaError

    pub(all) suberror IdnaError {
    PunycodeError(
    PunycodeError
    )
    DisallowedCodePoint(cp~ : Int)
    InvalidLabel(String)
    InvalidHyphen
    LabelTooLong
    DomainTooLong
    EmptyLabel
    InvalidBidi
    InvalidJoiner
    InvalidContextO(cp~ : Int)
    InvalidAcePrefix
    LeadingCombiningMark
    }

    Error types for IDNA processing
    impl Show for IdnaError

    IdnaError::output

    fn IdnaError::output(self : IdnaError, logger : &Logger) -> Unit

    IdnaError::to_string

    fn IdnaError::to_string(self : IdnaError) -> String

    to_ascii

    fn to_ascii(domain : String, use_std3_ascii_rules? : Bool, check_hyphens? : Bool, check_bidi? : Bool, check_joiners? : Bool, check_idna2008? : Bool, check_contexto? : Bool, verify_dns_length? : Bool) -> String raise IdnaError

    Converts a domain name to ASCII for DNS lookup.

    Applies UTS #46 mapping and NFC normalization before validating Unicode labels, including labels decoded from Punycode.

    • use_std3_ascii_rules (default true): restricts ASCII label characters to letters, digits, and hyphens.
    • check_hyphens (default true): rejects leading or trailing hyphens and labels with hyphens in both the third and fourth positions.
    • check_bidi (default true): applies the RFC 5893 Bidi rule.
    • check_joiners (default true): checks the RFC 5892 CONTEXTJ rules for ZWNJ (U+200C) and ZWJ (U+200D).
    • check_idna2008 (default false): restricts Unicode label characters to the IDNA2008 PVALID, CONTEXTJ, and CONTEXTO categories.
    • check_contexto (default false): checks the RFC 5892 CONTEXTO rules.
    • verify_dns_length (default true): checks ASCII label and domain lengths against DNS limits.

    to_unicode

    fn to_unicode(domain : String, use_std3_ascii_rules? : Bool, check_hyphens? : Bool, check_bidi? : Bool, check_joiners? : Bool, check_idna2008? : Bool, check_contexto? : Bool) -> String raise IdnaError

    Converts a domain name to Unicode for display.

    Applies UTS #46 mapping and NFC normalization before validating Unicode labels, including labels decoded from Punycode. DNS lengths are not checked.

    • use_std3_ascii_rules (default true): restricts ASCII label characters to letters, digits, and hyphens.
    • check_hyphens (default true): rejects leading or trailing hyphens and labels with hyphens in both the third and fourth positions.
    • check_bidi (default true): applies the RFC 5893 Bidi rule.
    • check_joiners (default true): checks the RFC 5892 CONTEXTJ rules for ZWNJ (U+200C) and ZWJ (U+200D).
    • check_idna2008 (default false): restricts Unicode label characters to the IDNA2008 PVALID, CONTEXTJ, and CONTEXTO categories.
    • check_contexto (default false): checks the RFC 5892 CONTEXTO rules.