unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

normalization/canonical_order.mbt
1///|2/// Canonical Ordering Algorithm3/// Sorts combining marks by Canonical Combining Class (CCC)4/// Uses stable sort to preserve order of marks with same CCC56///|7/// Reorder characters according to Canonical Combining Class8/// Starters (CCC=0) remain in place, combining marks are sorted by CCC9fn canonical_order(chars : Array[Char]) -> Unit {10  let len = chars.length()11  if len < 2 {12    return13  }14  let mut i = 015  while i < len {16    let ccc = @ccc.lookup_ccc(chars[i])1718    // Skip starters19    if ccc == 0 {20      i = i + 121      continue22    }2324    // Find the start of this combining mark sequence25    // (first non-starter after a starter or beginning)26    let start = i2728    // Find the end of the combining mark sequence29    while i < len && @ccc.lookup_ccc(chars[i]) != 0 {30      i = i + 131    }3233    // Sort the combining marks by CCC using stable insertion sort34    // Insertion sort is ideal here because:35    // 1. Combining mark sequences are typically short (1-4 marks)36    // 2. It's stable (preserves order of same-CCC marks)37    // 3. Simple and efficient for small arrays38    if i > start + 1 {39      stable_sort_by_ccc(chars, start, i)40    }41  }42}4344///|45/// Stable insertion sort by CCC for a range [start, end)46fn stable_sort_by_ccc(arr : Array[Char], start : Int32, end : Int32) -> Unit {47  for i in (start + 1)..<end {48    let key = arr[i]49    let key_ccc = @ccc.lookup_ccc(key)50    let mut j = i - 15152    // Move elements with greater CCC to the right53    while j >= start && @ccc.lookup_ccc(arr[j]) > key_ccc {54      arr[j + 1] = arr[j]55      j = j - 156    }57    arr[j + 1] = key58  }59}60