idna/processing.mbt1///|2/// Apply IDNA mapping to a domain name3///4/// A domain that needs no mapping is returned as is.5fn map_domain(6 domain : String,7 use_std3_ascii_rules~ : Bool,8) -> String raise IdnaError {9 map_text(domain, use_std3_ascii_rules~).unwrap_or(domain)10}1112///|13/// Apply IDNA mapping to `text`, or return `None` when it leaves `text` as it14/// is. The result is built only once a character is removed or replaced.15fn map_text(16 text : StringView,17 use_std3_ascii_rules~ : Bool,18) -> String? raise IdnaError {19 let mut output : StringBuilder? = None20 // UTF-16 offset of `c`, to copy the unchanged prefix on the first change.21 let mut offset = 022 for c in text {23 let code = c.to_int()24 if code < 0x80 {25 // IdnaMappingTable has every ASCII character valid but A-Z, which it26 // maps to a-z, so ASCII needs no lookup.27 let mapped = if code is ('A'..<='Z') {28 (code + 32).unsafe_to_char()29 } else {30 c31 }32 if output is Some(output) {33 output.write_char(mapped)34 } else if mapped != c {35 let builder = mapping_builder(text, offset)36 builder.write_char(mapped)37 output = Some(builder)38 }39 offset += 140 continue41 }42 let entry = @idna_data.lookup_idna_entry(c)43 let keep = match entry.status() {44 // Nontransitional processing keeps deviation characters as they are.45 Valid | Deviation => true46 Ignored | Mapped => false47 Disallowed => raise DisallowedCodePoint(cp=c.to_int())48 DisallowedSTD3Valid =>49 if use_std3_ascii_rules {50 raise DisallowedCodePoint(cp=c.to_int())51 } else {52 true53 }54 DisallowedSTD3Mapped =>55 if use_std3_ascii_rules {56 raise DisallowedCodePoint(cp=c.to_int())57 } else {58 false59 }60 }61 if output is Some(output) {62 if keep {63 output.write_char(c)64 } else {65 // An ignored character has an empty mapping.66 entry.write_mapping(output)67 }68 } else if !keep {69 let builder = mapping_builder(text, offset)70 entry.write_mapping(builder)71 output = Some(builder)72 }73 offset += if code > 0xFFFF { 2 } else { 1 }74 }75 if output is Some(output) {76 Some(output.to_string())77 } else {78 None79 }80}8182///|83/// A builder for the mapping of `text`, holding its first `offset` code84/// units, which the mapping leaves as they are.85fn mapping_builder(text : StringView, offset : Int32) -> StringBuilder {86 // The size hint counts bytes, two per UTF-16 code unit.87 let builder = StringBuilder(capacity=2 * text.length())88 builder.write_view(text[:offset])89 builder90}9192///|93/// Map and normalize one label, or return `None` when that leaves it as it94/// is. Every character of such a label has a valid status.95fn map_and_normalize_label(96 label : StringView,97 use_std3_ascii_rules~ : Bool,98) -> String? raise IdnaError {99 if map_text(label, use_std3_ascii_rules~) is Some(mapped) {100 Some(@normalization.nfc(mapped))101 } else if @normalization.is_normalized(label, @normalization.NFC) {102 None103 } else {104 Some(@normalization.nfc(label))105 }106}107108///|109/// Offset of the first label separator in `domain` at or after `start`, or110/// the length of `domain` when the last label starts at `start`.111fn label_end(domain : StringView, start : Int32) -> Int32 {112 // Separators are single UTF-16 code units, so slicing at them never113 // splits a surrogate pair.114 for i in start..<domain.length() {115 if domain[i] is ('.' | '\u{3002}' | '\u{FF0E}' | '\u{FF61}') {116 return i117 }118 }119 domain.length()120}121
///|
/// Apply IDNA mapping to a domain name
///
/// A domain that needs no mapping is returned as is.
fn map_domain(
domain : String,
use_std3_ascii_rules~ : Bool,
) -> String raise IdnaError {
map_text(domain, use_std3_ascii_rules~).unwrap_or(domain)
}
///|
/// Apply IDNA mapping to `text`, or return `None` when it leaves `text` as it
/// is. The result is built only once a character is removed or replaced.
fn map_text(
text : StringView,
use_std3_ascii_rules~ : Bool,
) -> String? raise IdnaError {
let mut output : StringBuilder? = None
// UTF-16 offset of `c`, to copy the unchanged prefix on the first change.
let mut offset = 0
for c in text {
let code = c.to_int()
if code < 0x80 {
// IdnaMappingTable has every ASCII character valid but A-Z, which it
// maps to a-z, so ASCII needs no lookup.
let mapped = if code is ('A'..<='Z') {
(code + 32).unsafe_to_char()
} else {
c
}
if output is Some(output) {
output.write_char(mapped)
} else if mapped != c {
let builder = mapping_builder(text, offset)
builder.write_char(mapped)
output = Some(builder)
}
offset += 1
continue
}
let entry = @idna_data.lookup_idna_entry(c)
let keep = match entry.status() {
// Nontransitional processing keeps deviation characters as they are.
Valid | Deviation => true
Ignored | Mapped => false
Disallowed => raise DisallowedCodePoint(cp=c.to_int())
DisallowedSTD3Valid =>
if use_std3_ascii_rules {
raise DisallowedCodePoint(cp=c.to_int())
} else {
true
}
DisallowedSTD3Mapped =>
if use_std3_ascii_rules {
raise DisallowedCodePoint(cp=c.to_int())
} else {
false
}
}
if output is Some(output) {
if keep {
output.write_char(c)
} else {
// An ignored character has an empty mapping.
entry.write_mapping(output)
}
} else if !keep {
let builder = mapping_builder(text, offset)
entry.write_mapping(builder)
output = Some(builder)
}
offset += if code > 0xFFFF { 2 } else { 1 }
}
if output is Some(output) {
Some(output.to_string())
} else {
None
}
}
///|
/// A builder for the mapping of `text`, holding its first `offset` code
/// units, which the mapping leaves as they are.
fn mapping_builder(text : StringView, offset : Int32) -> StringBuilder {
// The size hint counts bytes, two per UTF-16 code unit.
let builder = StringBuilder(capacity=2 * text.length())
builder.write_view(text[:offset])
builder
}
///|
/// Map and normalize one label, or return `None` when that leaves it as it
/// is. Every character of such a label has a valid status.
fn map_and_normalize_label(
label : StringView,
use_std3_ascii_rules~ : Bool,
) -> String? raise IdnaError {
if map_text(label, use_std3_ascii_rules~) is Some(mapped) {
Some(@normalization.nfc(mapped))
} else if @normalization.is_normalized(label, @normalization.NFC) {
None
} else {
Some(@normalization.nfc(label))
}
}
///|
/// Offset of the first label separator in `domain` at or after `start`, or
/// the length of `domain` when the last label starts at `start`.
fn label_end(domain : StringView, start : Int32) -> Int32 {
// Separators are single UTF-16 code units, so slicing at them never
// splits a surrogate pair.
for i in start..<domain.length() {
if domain[i] is ('.' | '\u{3002}' | '\u{FF0E}' | '\u{FF61}') {
return i
}
}
domain.length()
}