idna/ascii_fast_path.mbt1///|2/// Convert a domain that is already plain ASCII without running the general3/// UTS #46 algorithm.4///5/// Returns the converted domain when every label is valid as written, so6/// that ToASCII and ToUnicode both reduce to lowercasing. Returns `None`7/// when the general algorithm must run: a non-ASCII character, a possible8/// A-label (`xn--`), or anything that might be an error. Errors are always9/// left to the general algorithm, so both paths report them identically.10fn ascii_fast_path(11 domain : String,12 use_std3_ascii_rules~ : Bool,13 check_hyphens~ : Bool,14 verify_dns_length~ : Bool,15) -> String? {16 let length = domain.length()17 if verify_dns_length && length > 253 {18 return None19 }20 let mut label_start = 021 let mut has_uppercase = false22 for i in 0..<length {23 match domain[i] {24 'a'..<='z' | '0'..<='9' | '-' => ()25 'A'..<='Z' => has_uppercase = true26 '.' => {27 guard is_plain_ascii_label(28 domain,29 label_start,30 i,31 check_hyphens~,32 verify_dns_length~,33 ) else {34 return None35 }36 label_start = i + 137 }38 // Every other ASCII character is valid unless STD3 rules apply.39 unit => if use_std3_ascii_rules || unit >= 0x80 { return None }40 }41 }42 if label_start == length {43 // An empty final label is a trailing dot, which needs a label before it44 // and is rejected by the DNS length check.45 if label_start == 0 || verify_dns_length {46 return None47 }48 } else if !is_plain_ascii_label(49 domain,50 label_start,51 length,52 check_hyphens~,53 verify_dns_length~,54 ) {55 return None56 }57 Some(if has_uppercase { domain.to_lower() } else { domain })58}5960///|61/// Check the label `domain[start:end]`, whose characters are known to be62/// valid ASCII, against the rules that depend on position and length.63fn is_plain_ascii_label(64 domain : String,65 start : Int32,66 end : Int32,67 check_hyphens~ : Bool,68 verify_dns_length~ : Bool,69) -> Bool {70 let length = end - start71 if length == 0 || (verify_dns_length && length > 63) {72 return false73 }74 if length >= 4 && domain[start + 2] == '-' && domain[start + 3] == '-' {75 // Hyphens in the third and fourth positions are either a violation or76 // the prefix of an A-label, which has to be decoded.77 if check_hyphens ||78 (domain[start] is ('x' | 'X') && domain[start + 1] is ('n' | 'N')) {79 return false80 }81 }82 !check_hyphens || (domain[start] != '-' && domain[end - 1] != '-')83}84
///|
/// Convert a domain that is already plain ASCII without running the general
/// UTS #46 algorithm.
///
/// Returns the converted domain when every label is valid as written, so
/// that ToASCII and ToUnicode both reduce to lowercasing. Returns `None`
/// when the general algorithm must run: a non-ASCII character, a possible
/// A-label (`xn--`), or anything that might be an error. Errors are always
/// left to the general algorithm, so both paths report them identically.
fn ascii_fast_path(
domain : String,
use_std3_ascii_rules~ : Bool,
check_hyphens~ : Bool,
verify_dns_length~ : Bool,
) -> String? {
let length = domain.length()
if verify_dns_length && length > 253 {
return None
}
let mut label_start = 0
let mut has_uppercase = false
for i in 0..<length {
match domain[i] {
'a'..<='z' | '0'..<='9' | '-' => ()
'A'..<='Z' => has_uppercase = true
'.' => {
guard is_plain_ascii_label(
domain,
label_start,
i,
check_hyphens~,
verify_dns_length~,
) else {
return None
}
label_start = i + 1
}
// Every other ASCII character is valid unless STD3 rules apply.
unit => if use_std3_ascii_rules || unit >= 0x80 { return None }
}
}
if label_start == length {
// An empty final label is a trailing dot, which needs a label before it
// and is rejected by the DNS length check.
if label_start == 0 || verify_dns_length {
return None
}
} else if !is_plain_ascii_label(
domain,
label_start,
length,
check_hyphens~,
verify_dns_length~,
) {
return None
}
Some(if has_uppercase { domain.to_lower() } else { domain })
}
///|
/// Check the label `domain[start:end]`, whose characters are known to be
/// valid ASCII, against the rules that depend on position and length.
fn is_plain_ascii_label(
domain : String,
start : Int32,
end : Int32,
check_hyphens~ : Bool,
verify_dns_length~ : Bool,
) -> Bool {
let length = end - start
if length == 0 || (verify_dns_length && length > 63) {
return false
}
if length >= 4 && domain[start + 2] == '-' && domain[start + 3] == '-' {
// Hyphens in the third and fourth positions are either a violation or
// the prefix of an A-label, which has to be decoded.
if check_hyphens ||
(domain[start] is ('x' | 'X') && domain[start + 1] is ('n' | 'N')) {
return false
}
}
!check_hyphens || (domain[start] != '-' && domain[end - 1] != '-')
}