unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

casefold/caseless_matching.mbt
1///|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