casefold/caseless_matching.mbt1///|2/// Default caseless equality (D144), without normalization.3pub fn caseless_match(a : StringView, b : StringView) -> Bool {4 // Folding maps each code point on its own, so equal folded prefixes leave5 // only the rest to compare. Walk both while every code point folds to a6 // single one, and fold the rest only from the first longer mapping.7 let a_length = a.length()8 let b_length = b.length()9 let mut i = 010 let mut j = 011 while i < a_length && j < b_length {12 // Equal code units outside surrogate pairs need no decoding.13 // SAFETY: i < a_length and j < b_length.14 let unit = a.unsafe_get(i)15 if unit == b.unsafe_get(j) && !unit.is_leading_surrogate() {16 i += 117 j += 118 continue19 }20 let ca = code_point_at(a, i, a_length)21 let cb = code_point_at(b, j, b_length)22 if ca != cb {23 guard simple_fold(ca) is Some(fa) && simple_fold(cb) is Some(fb) else {24 break25 }26 if fa != fb {27 return false28 }29 }30 i += utf16_width(ca)31 j += utf16_width(cb)32 }33 if i == a_length && j == b_length {34 return true35 }36 // SAFETY: i and j are code point boundaries within their views.37 case_fold(a.unsafe_view(start=i, end=a_length)) ==38 case_fold(b.unsafe_view(start=j, end=b_length))39}4041///|42/// Case-insensitive equality under canonical equivalence (D145).43pub fn canonical_caseless_match(a : StringView, b : StringView) -> Bool {44 canonical_case_fold(a) == canonical_case_fold(b)45}4647///|48/// Identifier caseless equality (D147): NFKC_Casefold(NFD(a)) equals49/// NFKC_Casefold(NFD(b)). The initial NFD handles canonical equivalents with50/// iota subscripts. This does not validate identifiers or detect confusables.51pub fn identifier_caseless_match(a : StringView, b : StringView) -> Bool {52 nfkc_casefold(@normalization.nfd(a)) == nfkc_casefold(@normalization.nfd(b))53}54
///|
/// Default caseless equality (D144), without normalization.
pub fn caseless_match(a : StringView, b : StringView) -> Bool {
// Folding maps each code point on its own, so equal folded prefixes leave
// only the rest to compare. Walk both while every code point folds to a
// single one, and fold the rest only from the first longer mapping.
let a_length = a.length()
let b_length = b.length()
let mut i = 0
let mut j = 0
while i < a_length && j < b_length {
// Equal code units outside surrogate pairs need no decoding.
// SAFETY: i < a_length and j < b_length.
let unit = a.unsafe_get(i)
if unit == b.unsafe_get(j) && !unit.is_leading_surrogate() {
i += 1
j += 1
continue
}
let ca = code_point_at(a, i, a_length)
let cb = code_point_at(b, j, b_length)
if ca != cb {
guard simple_fold(ca) is Some(fa) && simple_fold(cb) is Some(fb) else {
break
}
if fa != fb {
return false
}
}
i += utf16_width(ca)
j += utf16_width(cb)
}
if i == a_length && j == b_length {
return true
}
// SAFETY: i and j are code point boundaries within their views.
case_fold(a.unsafe_view(start=i, end=a_length)) ==
case_fold(b.unsafe_view(start=j, end=b_length))
}
///|
/// Case-insensitive equality under canonical equivalence (D145).
pub fn canonical_caseless_match(a : StringView, b : StringView) -> Bool {
canonical_case_fold(a) == canonical_case_fold(b)
}
///|
/// Identifier caseless equality (D147): NFKC_Casefold(NFD(a)) equals
/// NFKC_Casefold(NFD(b)). The initial NFD handles canonical equivalents with
/// iota subscripts. This does not validate identifiers or detect confusables.
pub fn identifier_caseless_match(a : StringView, b : StringView) -> Bool {
nfkc_casefold(@normalization.nfd(a)) == nfkc_casefold(@normalization.nfd(b))
}