Contents

dns-inspector/idnarules-swift

The `IdnaRules` package contains a list of IDNA compatability rules for IDNA2008 as defined in [UTS #46](https://unicode.org/reports/tr46/#IDNA_Mapping_Table).

Usage

Find potential rules for a given codepoint

Rules are split up by nearby codepoints for faster lookup, however this means that the find_idna_rules method will return rules that don't apply to the codepoint. You will need to check if they apply afterwards.

import IdnaRules

var ruleCount = Int(0)
guard let ptr = IdnaRules.find_idna_rules(codepoint, &ruleCount) else {
    return []
}
if ruleCount <= 0 {
    return []
}
return ptr.withMemoryRebound(to: IDNARule.self, capacity: ruleCount) {
    Array(UnsafeBufferPointer(start: $0, count: ruleCount))
}

Check if a rule applies to a codepoint

codepoint >= rule.codepointStart && codepoint <= rule.codepointEnd

Get replacement codepoints

If the rule action is mapped, then you need to repalce the codepoint with other values.

if rule.replace_len == 0 {
    return []
}
return rule.replace_with.withMemoryRebound(to: UInt32.self, capacity: rule.replace_len) {
    Array(UnsafeBufferPointer(start: $0, count: rule.replace_len))
}

Package Metadata

Repository: dns-inspector/idnarules-swift

Default branch: main

README: README.md