unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

bidi/weak.mbt
1///|2/// W1-W7: Weak Type Resolution3///4/// These rules resolve weak types (EN, ES, ET, AN, CS, NSM, BN)5/// within an isolating run sequence.67///|8/// Resolve weak types within an isolating run sequence9///10/// The rules apply one after another over the whole sequence, each seeing11/// the types the previous rule left. Two passes reproduce that: the first12/// applies W1-W3, the second W4-W7, and each keeps the earlier-rule types it13/// still needs in locals instead of re-reading them.14fn resolve_weak_types(15  types : Array[BidiClass],16  _levels : Array[Int32],17  seq : IsolatingRunSequence,18) -> Unit {19  let indices = seq.indices20  let count = indices.length()21  if count == 0 {22    return23  }2425  // W1: NSM takes the type of the previous character (or sos), or ON after26  //     an isolate initiator or PDI.27  // W2: EN becomes AN when the most recent strong type is AL.28  // W3: AL becomes R.29  // The type after W1 of the previous character, which W1 reads.30  let mut previous_w1 = seq.sos31  // The most recent L, R or AL after W1, which W2 reads.32  let mut last_strong = seq.sos33  for idx in indices {34    let mut bc = types[idx]35    if bc is NSM {36      bc = if previous_w1.is_isolate_initiator() || previous_w1 is PDI {37        BidiClass::ON38      } else {39        previous_w140      }41    }42    if !(bc is BN) {43      previous_w1 = bc44    }45    match bc {46      L => last_strong = BidiClass::L47      R => last_strong = BidiClass::R48      AL => {49        last_strong = BidiClass::AL50        bc = BidiClass::R51      }52      EN => if last_strong is AL { bc = BidiClass::AN }53      _ => ()54    }55    types[idx] = bc56  }5758  // W4: A single ES between two ENs becomes EN; a single CS between two59  //     ENs becomes EN, and between two ANs becomes AN.60  // W5: A sequence of ETs adjacent to EN becomes EN.61  // W6: Remaining ES, ET and CS become ON.62  // W7: EN becomes L when the most recent strong type is L.63  // The type after W4 of the previous character, which W4 reads.64  let mut previous_w4 = BidiClass::ON65  // Whether the previous character is EN after W5.66  let mut previous_is_en = false67  // Start of a run of ETs that no EN precedes; an EN right after the run68  // still turns it into EN.69  let mut et_run_start = -170  // The most recent L or R, which W7 reads. W3 has removed AL.71  last_strong = seq.sos72  for i in 0..<count {73    let idx = indices[i]74    let mut bc = types[idx]75    if (bc is ES || bc is CS) && i > 0 && i < count - 1 {76      // The next character has not been through W4 yet, as in a W4-only pass.77      let next_bc = types[indices[i + 1]]78      if previous_w4 is EN && next_bc is EN {79        bc = BidiClass::EN80      } else if bc is CS && previous_w4 is AN && next_bc is AN {81        bc = BidiClass::AN82      }83    }84    previous_w4 = bc85    types[idx] = match bc {86      ET =>87        if previous_is_en {88          if last_strong is L {89            BidiClass::L90          } else {91            BidiClass::EN92          }93        } else {94          if et_run_start < 0 {95            et_run_start = i96          }97          BidiClass::ON98        }99      EN => {100        let resolved = if last_strong is L {101          BidiClass::L102        } else {103          BidiClass::EN104        }105        if et_run_start >= 0 {106          for j in et_run_start..<i {107            types[indices[j]] = resolved108          }109          et_run_start = -1110        }111        previous_is_en = true112        resolved113      }114      _ => {115        previous_is_en = false116        et_run_start = -1117        match bc {118          ES | CS => BidiClass::ON119          L => {120            last_strong = BidiClass::L121            bc122          }123          R => {124            last_strong = BidiClass::R125            bc126          }127          _ => bc128        }129      }130    }131  }132}133