segmentation/line_break_rules.mbt1///|2/// Apply UAX #14 revision 57 in rule order at a significant boundary.3/// LB1, LB9, and LB10 are resolved in prepare_units; LB2/LB3 in the cursor.4/// Context summaries make each decision constant-time, including long runs5/// of spaces, numeric punctuation, combining marks, and regional indicators.6fn boundary(units : Array[BreakUnit], index : Int32) -> BreakOpportunity? {7 let left = units[index - 1]8 let right = units[index]9 let a = left.class_10 let b = right.class_11 let before = class_at(units, index - 2)12 let after = class_at(units, index + 1)13 // LB4-LB6: mandatory breaks, with CR LF kept together.14 if a == @ucd.CR && b == @ucd.LF {15 return None16 }17 if a is (BK | CR | LF | NL) {18 return Some(Mandatory)19 }20 if b is (BK | CR | LF | NL) {21 return None22 }23 // LB7-LB8a: explicit spaces and zero-width controls precede LB9.24 if b is (SP | ZW) {25 return None26 }27 let base_index = left.space_base28 let base = class_at(units, base_index)29 if base == @ucd.ZW {30 return Some(Allowed)31 }32 if left.ends_with_zwj {33 return None34 }35 // LB11-LB12a.36 if a == @ucd.WJ || b == @ucd.WJ || a == @ucd.GL {37 return None38 }39 if b == @ucd.GL && !(a is (SP | HY | HH)) {40 return None41 }42 // LB13-LB14.43 if b is (CL | CP | EX | SY) {44 return None45 }46 if base == @ucd.OP {47 return None48 }49 // LB15a: initial quote after start, a hard break, or permitted context.50 if base == @ucd.QU &&51 units[base_index].initial_quote &&52 class_at(units, base_index - 1)53 is (XX | BK | CR | LF | NL | OP | QU | GL | SP | ZW) {54 return None55 }56 // LB15b: final quote before end or the listed following context.57 if b == @ucd.QU &&58 right.final_quote &&59 after60 is (XX61 | SP62 | GL63 | WJ64 | CL65 | QU66 | CP67 | EX68 | IS69 | SY70 | BK71 | CR72 | LF73 | NL74 | ZW) {75 return None76 }77 // LB15c-LB15d.78 if a == @ucd.SP && b == @ucd.IS && after == @ucd.NU {79 return Some(Allowed)80 }81 if b == @ucd.IS {82 return None83 }84 // LB16-LB18.85 if base is (CL | CP) && b == @ucd.NS {86 return None87 }88 if base == @ucd.B2 && b == @ucd.B2 {89 return None90 }91 if a == @ucd.SP {92 return Some(Allowed)93 }94 // LB19-LB19a: unresolved quotes and their East Asian surroundings.95 if (b == @ucd.QU && !right.initial_quote) ||96 (a == @ucd.QU && !left.final_quote) {97 return None98 }99 if b == @ucd.QU &&100 (101 !left.east_asian ||102 !units.get(index + 1).map(u => u.east_asian).unwrap_or(false)103 ) {104 return None105 }106 if a == @ucd.QU &&107 (108 !right.east_asian ||109 !units.get(index - 2).map(u => u.east_asian).unwrap_or(false)110 ) {111 return None112 }113 // LB20-LB20a.114 if a == @ucd.CB || b == @ucd.CB {115 return Some(Allowed)116 }117 if a is (HY | HH) &&118 b is (AL | HL) &&119 before is (XX | BK | CR | LF | NL | SP | ZW | CB | GL) {120 return None121 }122 // LB21-LB22.123 if b is (BA | HH | HY | NS) || a == @ucd.BB {124 return None125 }126 if before == @ucd.HL && a is (HY | HH) && b != @ucd.HL {127 return None128 }129 if a == @ucd.SY && b == @ucd.HL {130 return None131 }132 if b == @ucd.IN {133 return None134 }135 // LB23-LB24.136 if (a is (AL | HL) && b == @ucd.NU) || (a == @ucd.NU && b is (AL | HL)) {137 return None138 }139 if (a == @ucd.PR && b is (ID | EB | EM)) ||140 (a is (ID | EB | EM) && b == @ucd.PO) {141 return None142 }143 if (a is (PR | PO) && b is (AL | HL)) || (a is (AL | HL) && b is (PR | PO)) {144 return None145 }146 // LB25: numeric prefixes/suffixes, parentheses, decimals and separators.147 if b is (PO | PR) &&148 (149 left.numeric ||150 (151 a is (CL | CP) &&152 units.get(index - 2).map(u => u.numeric).unwrap_or(false)153 )154 ) {155 return None156 }157 if a is (PO | PR) &&158 (159 b == @ucd.NU ||160 (161 b == @ucd.OP &&162 (163 after == @ucd.NU ||164 (after == @ucd.IS && class_at(units, index + 2) == @ucd.NU)165 )166 )167 ) {168 return None169 }170 if b == @ucd.NU && (a is (HY | IS) || left.numeric) {171 return None172 }173 // LB26-LB27: Korean syllable blocks.174 if (a == @ucd.JL && b is (JL | JV | H2 | H3)) ||175 (a is (JV | H2) && b is (JV | JT)) ||176 (a is (JT | H3) && b == @ucd.JT) {177 return None178 }179 if (a is (JL | JV | JT | H2 | H3) && b == @ucd.PO) ||180 (a == @ucd.PR && b is (JL | JV | JT | H2 | H3)) {181 return None182 }183 // LB28-LB28a: alphabetics and Brahmic orthographic syllables.184 if a is (AL | HL) && b is (AL | HL) {185 return None186 }187 if a == @ucd.AP && right.aksara() {188 return None189 }190 if left.aksara() && b is (VF | VI) {191 return None192 }193 if a == @ucd.VI &&194 (b == @ucd.AK || right.code_point == 0x25CC) &&195 units.get(index - 2).map(u => u.aksara()).unwrap_or(false) {196 return None197 }198 if left.aksara() && right.aksara() && after == @ucd.VF {199 return None200 }201 // LB29-LB30.202 if a == @ucd.IS && b is (AL | HL) {203 return None204 }205 if (a is (AL | HL | NU) && b == @ucd.OP && !right.east_asian) ||206 (a == @ucd.CP && !left.east_asian && b is (AL | HL | NU)) {207 return None208 }209 // LB30a-LB30b.210 if a == @ucd.RI && b == @ucd.RI && left.regional_count % 2 == 1 {211 return None212 }213 if b == @ucd.EM && (a == @ucd.EB || left.potential_emoji) {214 return None215 }216 // LB31.217 Some(Allowed)218}219
///|
/// Apply UAX #14 revision 57 in rule order at a significant boundary.
/// LB1, LB9, and LB10 are resolved in prepare_units; LB2/LB3 in the cursor.
/// Context summaries make each decision constant-time, including long runs
/// of spaces, numeric punctuation, combining marks, and regional indicators.
fn boundary(units : Array[BreakUnit], index : Int32) -> BreakOpportunity? {
let left = units[index - 1]
let right = units[index]
let a = left.class_
let b = right.class_
let before = class_at(units, index - 2)
let after = class_at(units, index + 1)
// LB4-LB6: mandatory breaks, with CR LF kept together.
if a == @ucd.CR && b == @ucd.LF {
return None
}
if a is (BK | CR | LF | NL) {
return Some(Mandatory)
}
if b is (BK | CR | LF | NL) {
return None
}
// LB7-LB8a: explicit spaces and zero-width controls precede LB9.
if b is (SP | ZW) {
return None
}
let base_index = left.space_base
let base = class_at(units, base_index)
if base == @ucd.ZW {
return Some(Allowed)
}
if left.ends_with_zwj {
return None
}
// LB11-LB12a.
if a == @ucd.WJ || b == @ucd.WJ || a == @ucd.GL {
return None
}
if b == @ucd.GL && !(a is (SP | HY | HH)) {
return None
}
// LB13-LB14.
if b is (CL | CP | EX | SY) {
return None
}
if base == @ucd.OP {
return None
}
// LB15a: initial quote after start, a hard break, or permitted context.
if base == @ucd.QU &&
units[base_index].initial_quote &&
class_at(units, base_index - 1)
is (XX | BK | CR | LF | NL | OP | QU | GL | SP | ZW) {
return None
}
// LB15b: final quote before end or the listed following context.
if b == @ucd.QU &&
right.final_quote &&
after
is (XX
| SP
| GL
| WJ
| CL
| QU
| CP
| EX
| IS
| SY
| BK
| CR
| LF
| NL
| ZW) {
return None
}
// LB15c-LB15d.
if a == @ucd.SP && b == @ucd.IS && after == @ucd.NU {
return Some(Allowed)
}
if b == @ucd.IS {
return None
}
// LB16-LB18.
if base is (CL | CP) && b == @ucd.NS {
return None
}
if base == @ucd.B2 && b == @ucd.B2 {
return None
}
if a == @ucd.SP {
return Some(Allowed)
}
// LB19-LB19a: unresolved quotes and their East Asian surroundings.
if (b == @ucd.QU && !right.initial_quote) ||
(a == @ucd.QU && !left.final_quote) {
return None
}
if b == @ucd.QU &&
(
!left.east_asian ||
!units.get(index + 1).map(u => u.east_asian).unwrap_or(false)
) {
return None
}
if a == @ucd.QU &&
(
!right.east_asian ||
!units.get(index - 2).map(u => u.east_asian).unwrap_or(false)
) {
return None
}
// LB20-LB20a.
if a == @ucd.CB || b == @ucd.CB {
return Some(Allowed)
}
if a is (HY | HH) &&
b is (AL | HL) &&
before is (XX | BK | CR | LF | NL | SP | ZW | CB | GL) {
return None
}
// LB21-LB22.
if b is (BA | HH | HY | NS) || a == @ucd.BB {
return None
}
if before == @ucd.HL && a is (HY | HH) && b != @ucd.HL {
return None
}
if a == @ucd.SY && b == @ucd.HL {
return None
}
if b == @ucd.IN {
return None
}
// LB23-LB24.
if (a is (AL | HL) && b == @ucd.NU) || (a == @ucd.NU && b is (AL | HL)) {
return None
}
if (a == @ucd.PR && b is (ID | EB | EM)) ||
(a is (ID | EB | EM) && b == @ucd.PO) {
return None
}
if (a is (PR | PO) && b is (AL | HL)) || (a is (AL | HL) && b is (PR | PO)) {
return None
}
// LB25: numeric prefixes/suffixes, parentheses, decimals and separators.
if b is (PO | PR) &&
(
left.numeric ||
(
a is (CL | CP) &&
units.get(index - 2).map(u => u.numeric).unwrap_or(false)
)
) {
return None
}
if a is (PO | PR) &&
(
b == @ucd.NU ||
(
b == @ucd.OP &&
(
after == @ucd.NU ||
(after == @ucd.IS && class_at(units, index + 2) == @ucd.NU)
)
)
) {
return None
}
if b == @ucd.NU && (a is (HY | IS) || left.numeric) {
return None
}
// LB26-LB27: Korean syllable blocks.
if (a == @ucd.JL && b is (JL | JV | H2 | H3)) ||
(a is (JV | H2) && b is (JV | JT)) ||
(a is (JT | H3) && b == @ucd.JT) {
return None
}
if (a is (JL | JV | JT | H2 | H3) && b == @ucd.PO) ||
(a == @ucd.PR && b is (JL | JV | JT | H2 | H3)) {
return None
}
// LB28-LB28a: alphabetics and Brahmic orthographic syllables.
if a is (AL | HL) && b is (AL | HL) {
return None
}
if a == @ucd.AP && right.aksara() {
return None
}
if left.aksara() && b is (VF | VI) {
return None
}
if a == @ucd.VI &&
(b == @ucd.AK || right.code_point == 0x25CC) &&
units.get(index - 2).map(u => u.aksara()).unwrap_or(false) {
return None
}
if left.aksara() && right.aksara() && after == @ucd.VF {
return None
}
// LB29-LB30.
if a == @ucd.IS && b is (AL | HL) {
return None
}
if (a is (AL | HL | NU) && b == @ucd.OP && !right.east_asian) ||
(a == @ucd.CP && !left.east_asian && b is (AL | HL | NU)) {
return None
}
// LB30a-LB30b.
if a == @ucd.RI && b == @ucd.RI && left.regional_count % 2 == 1 {
return None
}
if b == @ucd.EM && (a == @ucd.EB || left.potential_emoji) {
return None
}
// LB31.
Some(Allowed)
}