bidi/explicit.mbt1///|2/// X1-X10: Explicit Formatting Character Processing3///4/// This handles embeddings (LRE, RLE, LRO, RLO, PDF) and5/// isolates (LRI, RLI, FSI, PDI).67///|8/// Directional status stack entry9priv struct DirectionalStatus {10 level : Int3211 override_status : BidiClass // L, R, or ON (neutral = no override)12 isolate_status : Bool13}1415///|16/// Process explicit formatting characters (X1-X10)17/// Returns list of (isolate_initiator_index, pdi_index) pairs18fn process_explicit(19 types : Array[BidiClass],20 levels : Array[Int32],21 paragraph_level : Int32,22) -> Array[(Int32, Int32)] {23 // X1: Initialize directional status stack24 let stack : Array[DirectionalStatus] = [25 {26 level: paragraph_level,27 override_status: BidiClass::ON,28 isolate_status: false,29 },30 ]31 let mut overflow_isolate_count = 032 let mut overflow_embedding_count = 033 let mut valid_isolate_count = 03435 // Track isolate pairs for later run sequence computation36 let isolate_pairs : Array[(Int32, Int32)] = []37 let isolate_stack : Array[Int32] = [] // Stack of isolate initiator indices38 for i in 0..<types.length() {39 let bc = types[i]40 match bc {41 // X2: RLE - Right-to-Left Embedding42 RLE => {43 let new_level = next_odd_level(stack[stack.length() - 1].level)44 if new_level <= max_depth &&45 overflow_isolate_count == 0 &&46 overflow_embedding_count == 0 {47 stack.push({48 level: new_level,49 override_status: BidiClass::ON,50 isolate_status: false,51 })52 } else if overflow_isolate_count == 0 {53 overflow_embedding_count += 154 }55 // X9: Remove from output (set level, keep type for X9 removal)56 levels[i] = stack[stack.length() - 1].level57 types[i] = BidiClass::BN58 }5960 // X3: LRE - Left-to-Right Embedding61 LRE => {62 let new_level = next_even_level(stack[stack.length() - 1].level)63 if new_level <= max_depth &&64 overflow_isolate_count == 0 &&65 overflow_embedding_count == 0 {66 stack.push({67 level: new_level,68 override_status: BidiClass::ON,69 isolate_status: false,70 })71 } else if overflow_isolate_count == 0 {72 overflow_embedding_count += 173 }74 levels[i] = stack[stack.length() - 1].level75 types[i] = BidiClass::BN76 }7778 // X4: RLO - Right-to-Left Override79 RLO => {80 let new_level = next_odd_level(stack[stack.length() - 1].level)81 if new_level <= max_depth &&82 overflow_isolate_count == 0 &&83 overflow_embedding_count == 0 {84 stack.push({85 level: new_level,86 override_status: BidiClass::R,87 isolate_status: false,88 })89 } else if overflow_isolate_count == 0 {90 overflow_embedding_count += 191 }92 levels[i] = stack[stack.length() - 1].level93 types[i] = BidiClass::BN94 }9596 // X5: LRO - Left-to-Right Override97 LRO => {98 let new_level = next_even_level(stack[stack.length() - 1].level)99 if new_level <= max_depth &&100 overflow_isolate_count == 0 &&101 overflow_embedding_count == 0 {102 stack.push({103 level: new_level,104 override_status: BidiClass::L,105 isolate_status: false,106 })107 } else if overflow_isolate_count == 0 {108 overflow_embedding_count += 1109 }110 levels[i] = stack[stack.length() - 1].level111 types[i] = BidiClass::BN112 }113114 // X5a: RLI - Right-to-Left Isolate115 RLI => {116 levels[i] = stack[stack.length() - 1].level117 let ovr = stack[stack.length() - 1].override_status118 if !(ovr is ON) {119 types[i] = ovr120 }121 let new_level = next_odd_level(stack[stack.length() - 1].level)122 if new_level <= max_depth &&123 overflow_isolate_count == 0 &&124 overflow_embedding_count == 0 {125 valid_isolate_count += 1126 isolate_stack.push(i)127 stack.push({128 level: new_level,129 override_status: BidiClass::ON,130 isolate_status: true,131 })132 } else {133 overflow_isolate_count += 1134 }135 }136137 // X5b: LRI - Left-to-Right Isolate138 LRI => {139 levels[i] = stack[stack.length() - 1].level140 let ovr = stack[stack.length() - 1].override_status141 if !(ovr is ON) {142 types[i] = ovr143 }144 let new_level = next_even_level(stack[stack.length() - 1].level)145 if new_level <= max_depth &&146 overflow_isolate_count == 0 &&147 overflow_embedding_count == 0 {148 valid_isolate_count += 1149 isolate_stack.push(i)150 stack.push({151 level: new_level,152 override_status: BidiClass::ON,153 isolate_status: true,154 })155 } else {156 overflow_isolate_count += 1157 }158 }159160 // X5c: FSI - First Strong Isolate161 FSI => {162 levels[i] = stack[stack.length() - 1].level163 let ovr = stack[stack.length() - 1].override_status164 if !(ovr is ON) {165 types[i] = ovr166 }167 // Determine direction of isolate content168 let isolate_dir = determine_isolate_direction(types, i + 1)169 let new_level = if isolate_dir is RTL {170 next_odd_level(stack[stack.length() - 1].level)171 } else {172 next_even_level(stack[stack.length() - 1].level)173 }174 if new_level <= max_depth &&175 overflow_isolate_count == 0 &&176 overflow_embedding_count == 0 {177 valid_isolate_count += 1178 isolate_stack.push(i)179 stack.push({180 level: new_level,181 override_status: BidiClass::ON,182 isolate_status: true,183 })184 } else {185 overflow_isolate_count += 1186 }187 }188189 // X6a: PDI - Pop Directional Isolate190 PDI => {191 if overflow_isolate_count > 0 {192 overflow_isolate_count -= 1193 } else if valid_isolate_count > 0 {194 overflow_embedding_count = 0195 // Pop until isolate196 while stack.length() > 1 && !stack[stack.length() - 1].isolate_status {197 let _ = stack.pop()198 }199 // Pop the isolate entry itself200 if stack.length() > 1 {201 let _ = stack.pop()202 }203 valid_isolate_count -= 1204 // Record isolate pair205 if isolate_stack.pop() is Some(start) {206 isolate_pairs.push((start, i))207 }208 }209 levels[i] = stack[stack.length() - 1].level210 let ovr = stack[stack.length() - 1].override_status211 if !(ovr is ON) {212 types[i] = ovr213 }214 }215216 // X7: PDF - Pop Directional Formatting217 PDF => {218 if overflow_isolate_count > 0 {219 // Do nothing220 } else if overflow_embedding_count > 0 {221 overflow_embedding_count -= 1222 } else if stack.length() >= 2 &&223 !stack[stack.length() - 1].isolate_status {224 let _ = stack.pop()225 }226 levels[i] = stack[stack.length() - 1].level227 types[i] = BidiClass::BN228 }229230 // X6: For all other types231 _ => {232 levels[i] = stack[stack.length() - 1].level233 let ovr = stack[stack.length() - 1].override_status234 if !(ovr is ON) && !(bc is BN) {235 types[i] = ovr236 }237 }238 }239 }240 isolate_pairs241}242243///|244/// Ensure NSM levels follow the last non-X9 character.245fn adjust_nsm_levels(246 original_classes : Array[BidiClass],247 levels : Array[Int32],248) -> Unit {249 let mut last_idx = -1250 let mut saw_explicit = false251 for i, bc in original_classes {252 if bc is LRE ||253 bc is LRO ||254 bc is RLE ||255 bc is RLO ||256 bc is PDF ||257 bc.is_isolate_initiator() {258 saw_explicit = true259 }260 if bc is NSM && last_idx >= 0 {261 let last_bc = original_classes[last_idx]262 if !last_bc.is_isolate_initiator() && !(last_bc is PDI) && !saw_explicit {263 levels[i] = levels[last_idx]264 }265 }266 if !bc.is_x9_removed() {267 last_idx = i268 saw_explicit = false269 }270 }271}272273///|274/// Determine direction for FSI by finding first strong character275fn determine_isolate_direction(276 types : Array[BidiClass],277 start : Int32,278) -> Direction {279 let mut isolate_count = 0280 for i in start..<types.length() {281 let bc = types[i]282 if bc.is_isolate_initiator() {283 isolate_count += 1284 } else if bc is PDI {285 if isolate_count > 0 {286 isolate_count -= 1287 } else {288 // End of our isolate content289 break290 }291 } else if isolate_count == 0 {292 match bc {293 R | AL => return RTL294 L => return LTR295 _ => ()296 }297 }298 }299 LTR // Default300}301302///|303/// Get next odd level >= current304fn next_odd_level(level : Int32) -> Int32 {305 (level + 1) | 1306}307308///|309/// Get next even level >= current310fn next_even_level(level : Int32) -> Int32 {311 (level + 2) & (1).lnot()312}313314///|315/// Isolating run sequence - indices of characters that form a sequence316priv struct IsolatingRunSequence {317 indices : Array[Int32] // Character indices in this sequence318 sos : BidiClass // Start-of-sequence type (L or R)319 eos : BidiClass // End-of-sequence type (L or R)320 level : Int32 // Embedding level of the sequence321}322323///|324/// Compute isolating run sequences (X10)325fn compute_isolating_run_sequences(326 types : Array[BidiClass],327 levels : Array[Int32],328 original_classes : Array[BidiClass],329 paragraph_level : Int32,330 isolate_pairs : Array[(Int32, Int32)],331) -> Array[IsolatingRunSequence] {332 let len = types.length()333 if len == 0 {334 return []335 }336337 // Build a map from isolate initiator to PDI338 let pdi_of_isolate : Array[Int32] = Array::make(len, -1)339 for pair in isolate_pairs {340 pdi_of_isolate[pair.0] = pair.1341 }342 let sequences : Array[IsolatingRunSequence] = []343 let processed : Array[Bool] = Array::make(len, false)344345 // Find level runs and connect them via isolate pairs346 for i in 0..<len {347 if processed[i] {348 continue349 }350351 // Skip BN and explicit formatting characters352 let bc = types[i]353 if bc is BN {354 processed[i] = true355 continue356 }357358 // Start a new isolating run sequence359 let indices : Array[Int32] = []360 let seq_level = levels[i]361 let mut current = i362363 // Follow the run, linking through isolate pairs364 while current < len {365 // Add all characters at the same level in this run366 while current < len {367 if processed[current] {368 current += 1369 continue370 }371 if types[current] is BN {372 processed[current] = true373 current += 1374 continue375 }376 if levels[current] != seq_level {377 break378 }379 indices.push(current)380 processed[current] = true381 current += 1382 }383384 // Check if last added character is an isolate initiator385 if indices.length() > 0 {386 let last = indices[indices.length() - 1]387 let pdi_idx = pdi_of_isolate[last]388 if pdi_idx < 0 {389 break390 }391 // Continue sequence at the matching PDI run (include the PDI).392 current = pdi_idx393 } else {394 break395 }396 }397 if indices.length() == 0 {398 continue399 }400401 // Compute sos and eos402 let first_idx = indices[0]403 let last_idx = indices[indices.length() - 1]404405 // sos: higher of level before sequence and sequence level406 let level_before = if first_idx == 0 {407 paragraph_level408 } else {409 // Find previous character level (skip BN)410 let mut prev = first_idx - 1411 while prev > 0 && types[prev] is BN {412 prev -= 1413 }414 if prev < 0 || types[prev] is BN {415 paragraph_level416 } else if original_classes[prev] is B {417 paragraph_level418 } else {419 levels[prev]420 }421 }422 let sos_level = @cmp.maximum(level_before, seq_level)423 let sos : BidiClass = if sos_level % 2 == 0 {424 BidiClass::L425 } else {426 BidiClass::R427 }428429 // eos: higher of level after sequence and sequence level430 let level_after = if last_idx >= len - 1 {431 paragraph_level432 } else if original_classes[last_idx] is B {433 paragraph_level434 } else {435 // Find next character level (skip BN)436 let mut next = last_idx + 1437 while next < len && types[next] is BN {438 next += 1439 }440 // Check if this is an isolate initiator - look for matching PDI441 let last_type = types[last_idx]442 if last_type.is_isolate_initiator() {443 let pdi_idx = pdi_of_isolate[last_idx]444 if pdi_idx < 0 {445 paragraph_level446 } else {447 levels[pdi_idx]448 }449 } else if next >= len {450 paragraph_level451 } else if original_classes[next] is B {452 paragraph_level453 } else {454 levels[next]455 }456 }457 let eos_level = @cmp.maximum(level_after, seq_level)458 let eos : BidiClass = if eos_level % 2 == 0 {459 BidiClass::L460 } else {461 BidiClass::R462 }463 sequences.push({ indices, sos, eos, level: seq_level, })464 }465 sequences466}467468///|469/// The isolating run sequences of a paragraph without explicit formatting470/// characters: one sequence at the paragraph level holding every character471/// that X9 keeps, with sos and eos given by that level.472fn paragraph_run_sequence(473 types : Array[BidiClass],474 paragraph_level : Int32,475) -> Array[IsolatingRunSequence] {476 let indices : Array[Int32] = Array(capacity=types.length())477 for i, bc in types {478 if !(bc is BN) {479 indices.push(i)480 }481 }482 if indices.length() == 0 {483 return []484 }485 let direction : BidiClass = if paragraph_level % 2 == 0 {486 BidiClass::L487 } else {488 BidiClass::R489 }490 [{ indices, sos: direction, eos: direction, level: paragraph_level, }]491}492
///|
/// X1-X10: Explicit Formatting Character Processing
///
/// This handles embeddings (LRE, RLE, LRO, RLO, PDF) and
/// isolates (LRI, RLI, FSI, PDI).
///|
/// Directional status stack entry
priv struct DirectionalStatus {
level : Int32
override_status : BidiClass // L, R, or ON (neutral = no override)
isolate_status : Bool
}
///|
/// Process explicit formatting characters (X1-X10)
/// Returns list of (isolate_initiator_index, pdi_index) pairs
fn process_explicit(
types : Array[BidiClass],
levels : Array[Int32],
paragraph_level : Int32,
) -> Array[(Int32, Int32)] {
// X1: Initialize directional status stack
let stack : Array[DirectionalStatus] = [
{
level: paragraph_level,
override_status: BidiClass::ON,
isolate_status: false,
},
]
let mut overflow_isolate_count = 0
let mut overflow_embedding_count = 0
let mut valid_isolate_count = 0
// Track isolate pairs for later run sequence computation
let isolate_pairs : Array[(Int32, Int32)] = []
let isolate_stack : Array[Int32] = [] // Stack of isolate initiator indices
for i in 0..<types.length() {
let bc = types[i]
match bc {
// X2: RLE - Right-to-Left Embedding
RLE => {
let new_level = next_odd_level(stack[stack.length() - 1].level)
if new_level <= max_depth &&
overflow_isolate_count == 0 &&
overflow_embedding_count == 0 {
stack.push({
level: new_level,
override_status: BidiClass::ON,
isolate_status: false,
})
} else if overflow_isolate_count == 0 {
overflow_embedding_count += 1
}
// X9: Remove from output (set level, keep type for X9 removal)
levels[i] = stack[stack.length() - 1].level
types[i] = BidiClass::BN
}
// X3: LRE - Left-to-Right Embedding
LRE => {
let new_level = next_even_level(stack[stack.length() - 1].level)
if new_level <= max_depth &&
overflow_isolate_count == 0 &&
overflow_embedding_count == 0 {
stack.push({
level: new_level,
override_status: BidiClass::ON,
isolate_status: false,
})
} else if overflow_isolate_count == 0 {
overflow_embedding_count += 1
}
levels[i] = stack[stack.length() - 1].level
types[i] = BidiClass::BN
}
// X4: RLO - Right-to-Left Override
RLO => {
let new_level = next_odd_level(stack[stack.length() - 1].level)
if new_level <= max_depth &&
overflow_isolate_count == 0 &&
overflow_embedding_count == 0 {
stack.push({
level: new_level,
override_status: BidiClass::R,
isolate_status: false,
})
} else if overflow_isolate_count == 0 {
overflow_embedding_count += 1
}
levels[i] = stack[stack.length() - 1].level
types[i] = BidiClass::BN
}
// X5: LRO - Left-to-Right Override
LRO => {
let new_level = next_even_level(stack[stack.length() - 1].level)
if new_level <= max_depth &&
overflow_isolate_count == 0 &&
overflow_embedding_count == 0 {
stack.push({
level: new_level,
override_status: BidiClass::L,
isolate_status: false,
})
} else if overflow_isolate_count == 0 {
overflow_embedding_count += 1
}
levels[i] = stack[stack.length() - 1].level
types[i] = BidiClass::BN
}
// X5a: RLI - Right-to-Left Isolate
RLI => {
levels[i] = stack[stack.length() - 1].level
let ovr = stack[stack.length() - 1].override_status
if !(ovr is ON) {
types[i] = ovr
}
let new_level = next_odd_level(stack[stack.length() - 1].level)
if new_level <= max_depth &&
overflow_isolate_count == 0 &&
overflow_embedding_count == 0 {
valid_isolate_count += 1
isolate_stack.push(i)
stack.push({
level: new_level,
override_status: BidiClass::ON,
isolate_status: true,
})
} else {
overflow_isolate_count += 1
}
}
// X5b: LRI - Left-to-Right Isolate
LRI => {
levels[i] = stack[stack.length() - 1].level
let ovr = stack[stack.length() - 1].override_status
if !(ovr is ON) {
types[i] = ovr
}
let new_level = next_even_level(stack[stack.length() - 1].level)
if new_level <= max_depth &&
overflow_isolate_count == 0 &&
overflow_embedding_count == 0 {
valid_isolate_count += 1
isolate_stack.push(i)
stack.push({
level: new_level,
override_status: BidiClass::ON,
isolate_status: true,
})
} else {
overflow_isolate_count += 1
}
}
// X5c: FSI - First Strong Isolate
FSI => {
levels[i] = stack[stack.length() - 1].level
let ovr = stack[stack.length() - 1].override_status
if !(ovr is ON) {
types[i] = ovr
}
// Determine direction of isolate content
let isolate_dir = determine_isolate_direction(types, i + 1)
let new_level = if isolate_dir is RTL {
next_odd_level(stack[stack.length() - 1].level)
} else {
next_even_level(stack[stack.length() - 1].level)
}
if new_level <= max_depth &&
overflow_isolate_count == 0 &&
overflow_embedding_count == 0 {
valid_isolate_count += 1
isolate_stack.push(i)
stack.push({
level: new_level,
override_status: BidiClass::ON,
isolate_status: true,
})
} else {
overflow_isolate_count += 1
}
}
// X6a: PDI - Pop Directional Isolate
PDI => {
if overflow_isolate_count > 0 {
overflow_isolate_count -= 1
} else if valid_isolate_count > 0 {
overflow_embedding_count = 0
// Pop until isolate
while stack.length() > 1 && !stack[stack.length() - 1].isolate_status {
let _ = stack.pop()
}
// Pop the isolate entry itself
if stack.length() > 1 {
let _ = stack.pop()
}
valid_isolate_count -= 1
// Record isolate pair
if isolate_stack.pop() is Some(start) {
isolate_pairs.push((start, i))
}
}
levels[i] = stack[stack.length() - 1].level
let ovr = stack[stack.length() - 1].override_status
if !(ovr is ON) {
types[i] = ovr
}
}
// X7: PDF - Pop Directional Formatting
PDF => {
if overflow_isolate_count > 0 {
// Do nothing
} else if overflow_embedding_count > 0 {
overflow_embedding_count -= 1
} else if stack.length() >= 2 &&
!stack[stack.length() - 1].isolate_status {
let _ = stack.pop()
}
levels[i] = stack[stack.length() - 1].level
types[i] = BidiClass::BN
}
// X6: For all other types
_ => {
levels[i] = stack[stack.length() - 1].level
let ovr = stack[stack.length() - 1].override_status
if !(ovr is ON) && !(bc is BN) {
types[i] = ovr
}
}
}
}
isolate_pairs
}
///|
/// Ensure NSM levels follow the last non-X9 character.
fn adjust_nsm_levels(
original_classes : Array[BidiClass],
levels : Array[Int32],
) -> Unit {
let mut last_idx = -1
let mut saw_explicit = false
for i, bc in original_classes {
if bc is LRE ||
bc is LRO ||
bc is RLE ||
bc is RLO ||
bc is PDF ||
bc.is_isolate_initiator() {
saw_explicit = true
}
if bc is NSM && last_idx >= 0 {
let last_bc = original_classes[last_idx]
if !last_bc.is_isolate_initiator() && !(last_bc is PDI) && !saw_explicit {
levels[i] = levels[last_idx]
}
}
if !bc.is_x9_removed() {
last_idx = i
saw_explicit = false
}
}
}
///|
/// Determine direction for FSI by finding first strong character
fn determine_isolate_direction(
types : Array[BidiClass],
start : Int32,
) -> Direction {
let mut isolate_count = 0
for i in start..<types.length() {
let bc = types[i]
if bc.is_isolate_initiator() {
isolate_count += 1
} else if bc is PDI {
if isolate_count > 0 {
isolate_count -= 1
} else {
// End of our isolate content
break
}
} else if isolate_count == 0 {
match bc {
R | AL => return RTL
L => return LTR
_ => ()
}
}
}
LTR // Default
}
///|
/// Get next odd level >= current
fn next_odd_level(level : Int32) -> Int32 {
(level + 1) | 1
}
///|
/// Get next even level >= current
fn next_even_level(level : Int32) -> Int32 {
(level + 2) & (1).lnot()
}
///|
/// Isolating run sequence - indices of characters that form a sequence
priv struct IsolatingRunSequence {
indices : Array[Int32] // Character indices in this sequence
sos : BidiClass // Start-of-sequence type (L or R)
eos : BidiClass // End-of-sequence type (L or R)
level : Int32 // Embedding level of the sequence
}
///|
/// Compute isolating run sequences (X10)
fn compute_isolating_run_sequences(
types : Array[BidiClass],
levels : Array[Int32],
original_classes : Array[BidiClass],
paragraph_level : Int32,
isolate_pairs : Array[(Int32, Int32)],
) -> Array[IsolatingRunSequence] {
let len = types.length()
if len == 0 {
return []
}
// Build a map from isolate initiator to PDI
let pdi_of_isolate : Array[Int32] = Array::make(len, -1)
for pair in isolate_pairs {
pdi_of_isolate[pair.0] = pair.1
}
let sequences : Array[IsolatingRunSequence] = []
let processed : Array[Bool] = Array::make(len, false)
// Find level runs and connect them via isolate pairs
for i in 0..<len {
if processed[i] {
continue
}
// Skip BN and explicit formatting characters
let bc = types[i]
if bc is BN {
processed[i] = true
continue
}
// Start a new isolating run sequence
let indices : Array[Int32] = []
let seq_level = levels[i]
let mut current = i
// Follow the run, linking through isolate pairs
while current < len {
// Add all characters at the same level in this run
while current < len {
if processed[current] {
current += 1
continue
}
if types[current] is BN {
processed[current] = true
current += 1
continue
}
if levels[current] != seq_level {
break
}
indices.push(current)
processed[current] = true
current += 1
}
// Check if last added character is an isolate initiator
if indices.length() > 0 {
let last = indices[indices.length() - 1]
let pdi_idx = pdi_of_isolate[last]
if pdi_idx < 0 {
break
}
// Continue sequence at the matching PDI run (include the PDI).
current = pdi_idx
} else {
break
}
}
if indices.length() == 0 {
continue
}
// Compute sos and eos
let first_idx = indices[0]
let last_idx = indices[indices.length() - 1]
// sos: higher of level before sequence and sequence level
let level_before = if first_idx == 0 {
paragraph_level
} else {
// Find previous character level (skip BN)
let mut prev = first_idx - 1
while prev > 0 && types[prev] is BN {
prev -= 1
}
if prev < 0 || types[prev] is BN {
paragraph_level
} else if original_classes[prev] is B {
paragraph_level
} else {
levels[prev]
}
}
let sos_level = @cmp.maximum(level_before, seq_level)
let sos : BidiClass = if sos_level % 2 == 0 {
BidiClass::L
} else {
BidiClass::R
}
// eos: higher of level after sequence and sequence level
let level_after = if last_idx >= len - 1 {
paragraph_level
} else if original_classes[last_idx] is B {
paragraph_level
} else {
// Find next character level (skip BN)
let mut next = last_idx + 1
while next < len && types[next] is BN {
next += 1
}
// Check if this is an isolate initiator - look for matching PDI
let last_type = types[last_idx]
if last_type.is_isolate_initiator() {
let pdi_idx = pdi_of_isolate[last_idx]
if pdi_idx < 0 {
paragraph_level
} else {
levels[pdi_idx]
}
} else if next >= len {
paragraph_level
} else if original_classes[next] is B {
paragraph_level
} else {
levels[next]
}
}
let eos_level = @cmp.maximum(level_after, seq_level)
let eos : BidiClass = if eos_level % 2 == 0 {
BidiClass::L
} else {
BidiClass::R
}
sequences.push({ indices, sos, eos, level: seq_level, })
}
sequences
}
///|
/// The isolating run sequences of a paragraph without explicit formatting
/// characters: one sequence at the paragraph level holding every character
/// that X9 keeps, with sos and eos given by that level.
fn paragraph_run_sequence(
types : Array[BidiClass],
paragraph_level : Int32,
) -> Array[IsolatingRunSequence] {
let indices : Array[Int32] = Array(capacity=types.length())
for i, bc in types {
if !(bc is BN) {
indices.push(i)
}
}
if indices.length() == 0 {
return []
}
let direction : BidiClass = if paragraph_level % 2 == 0 {
BidiClass::L
} else {
BidiClass::R
}
[{ indices, sos: direction, eos: direction, level: paragraph_level, }]
}