The IdnaRules package contains a list of IDNA compatability rules for IDNA2008 as defined in UTS #46.
Note that valid codepoint ranges are not included for optimization.
Rule lists are built by the update_rules.py script.
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))
}codepoint >= rule.codepointStart && codepoint <= rule.codepointEndIf 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))
}