1// Line break opportunities, by UAX #14's rules (Unicode 16.0) with2// LB1's default resolution of classes.34import std::vector;56import tables;7import properties;89// What lies between two units.10type Break = enum {11 Prohibited,12 Allowed,13 Mandatory,14};1516// A line break opportunity: before the byte offset.17pub type LineBreak = struct {18 pub at: u64,19 pub mandatory: bool,20};2122// A base character with the combining marks LB9 joins to it.23type Unit = struct {24 class: u32, // LB10 applied25 value: u32, // the base's properties26 c: u32, // the base27 zwj: bool, // the unit ends with a ZWJ28 at: u64, // its first byte29};3031// Every opportunity in the text, in order, the end of the text last as a32// mandatory one (LB3).33pub fn line_breaks(text: &[u8]) -> Vector<LineBreak> {34 var out = Vector<LineBreak>::empty();35 if (text.length() == 0) {36 return move out;37 }38 const units = units_of(&text);39 for ([1..units.length()]) |k| {40 match (break_before(&units.view(), k)) {41 ::Allowed {42 out.push(LineBreak { .at = units.get(k).at, .mandatory = false });43 }44 ::Mandatory {45 out.push(LineBreak { .at = units.get(k).at, .mandatory = true });46 }47 ::Prohibited {}48 }49 }50 out.push(LineBreak { .at = text.length(), .mandatory = true });51 return move out;52}5354fn units_of(text: &[u8]) -> Vector<Unit> {55 var units = Vector<Unit>::with_capacity(text.length());56 var at: u64 = 0;57 while (at < text.length()) {58 const d = decode(&text, at);59 const value = properties(d.c);60 const class = if (line_class(value) == LB_SA) LB_AL else line_class(value); // LB161 if (class == LB_CM || class == LB_ZWJ) {62 if (units.length() > 0) {63 var last = &var units.get_var(units.length() - 1);64 if (!is_any(last.class, BREAKS_OR_SPACE)) {65 last.zwj = class == LB_ZWJ; // LB966 at += d.length;67 continue;68 }69 }70 // LB10: as U+004171 units.push(Unit {72 .class = LB_AL,73 .value = properties('A'),74 .c = 'A',75 .zwj = class == LB_ZWJ,76 .at = at77 });78 } else {79 units.push(Unit { .class = class, .value = value, .c = d.c, .zwj = false, .at = at });80 }81 at += d.length;82 }83 return move units;84}8586// Where a unit's class is asked before the text's start or past its end:87// UAX #14's sot and eot.88const EDGE: u32 = 63;8990// Sets of line break classes, a bit each.91// mandatory breaks, spaces and ZW (LB4-LB8)92const BREAKS_OR_SPACE: u64 = classes(&[LB_BK, LB_CR, LB_LF, LB_NL, LB_SP, LB_ZW]);93// LB12a94const SPACE_BA_HY: u64 = classes(&[LB_SP, LB_BA, LB_HY]);95// LB1396const CLOSING: u64 = classes(&[LB_CL, LB_CP, LB_EX, LB_SY]);97// LB15a98const BEFORE_INITIAL_QUOTE: u64 = classes(&[99 EDGE,100 LB_BK,101 LB_CR,102 LB_LF,103 LB_NL,104 LB_OP,105 LB_QU,106 LB_GL,107 LB_SP,108 LB_ZW109]);110// LB15b111const AFTER_FINAL_QUOTE: u64 = classes(&[112 LB_SP,113 LB_GL,114 LB_WJ,115 LB_CL,116 LB_QU,117 LB_CP,118 LB_EX,119 LB_IS,120 LB_SY,121 LB_BK,122 LB_CR,123 LB_LF,124 LB_NL,125 LB_ZW,126 EDGE127]);128// LB28a129const BEFORE_DOTTED_CIRCLE: u64 = classes(&[130 EDGE,131 LB_BK,132 LB_CR,133 LB_LF,134 LB_NL,135 LB_SP,136 LB_ZW,137 LB_CB,138 LB_GL139]);140// LB23a141const IDEOGRAPHIC: u64 = classes(&[LB_ID, LB_EB, LB_EM]);142// LB26143const AFTER_JL: u64 = classes(&[LB_JL, LB_JV, LB_H2, LB_H3]);144// LB27145const KOREAN: u64 = classes(&[LB_JL, LB_JV, LB_JT, LB_H2, LB_H3]);146147fn classes(members: &[u32]) -> u64 {148 var set: u64 = 0;149 for (members) |c| {150 set |= (1 to u64) << (c to u64);151 }152 return set;153}154155fn is_any(class: u32, set: u64) -> bool => class < 64 && ((set >> (class to u64)) & 1) != 0;156157fn east_asian(u: &Unit) -> bool {158 return (u.value & EAST_ASIAN) != 0;159}160161// The class of the unit at k, EDGE past either end.162fn class_at(units: &[Unit], k: i64) -> u32 {163 if (k < 0 || k >= units.length() to i64) {164 return EDGE;165 }166 return units[k to u64].class;167}168169// The unit before k skipping spaces back from k - 1, or -1.170fn before_spaces(units: &[Unit], k: u64) -> i64 {171 var j = k to i64 - 1;172 while (j >= 0 && units[j to u64].class == LB_SP) {173 j -= 1;174 }175 return j;176}177178// Whether the units ending at j are NU (SY | IS)*.179fn numeric_before(units: &[Unit], j: i64) -> bool {180 var i = j;181 while (i >= 0 && (units[i to u64].class == LB_SY || units[i to u64].class == LB_IS)) {182 i -= 1;183 }184 return i >= 0 && units[i to u64].class == LB_NU;185}186187fn break_before(units: &[Unit], k: u64) -> Break {188 const a = units[k - 1].class;189 const b = units[k].class;190 const ua = &units[k - 1];191 const ub = &units[k];192 const after = class_at(&units, k to i64 + 1);193 if (a == LB_BK) {194 return ::Mandatory; // LB4195 }196 if (a == LB_CR && b == LB_LF) {197 return ::Prohibited; // LB5198 }199 if (a == LB_CR || a == LB_LF || a == LB_NL) {200 return ::Mandatory;201 }202 if (is_any(b, BREAKS_OR_SPACE)) {203 return ::Prohibited; // LB6, LB7204 }205 const j = before_spaces(&units, k);206 if (j >= 0 && units[j to u64].class == LB_ZW) {207 return ::Allowed; // LB8208 }209 if (ua.zwj) {210 return ::Prohibited; // LB8a211 }212 if (a == LB_WJ || b == LB_WJ || a == LB_GL) {213 return ::Prohibited; // LB11, LB12214 }215 if (b == LB_GL && !is_any(a, SPACE_BA_HY)) {216 return ::Prohibited; // LB12a217 }218 if (is_any(b, CLOSING)) {219 return ::Prohibited; // LB13220 }221 const before = class_at(&units, j);222 if (before == LB_OP) {223 return ::Prohibited; // LB14224 }225 if (before == LB_QU && (units[j to u64].value & INITIAL) != 0) {226 const outer = class_at(&units, j - 1);227 if (is_any(outer, BEFORE_INITIAL_QUOTE)) {228 return ::Prohibited; // LB15a229 }230 }231 if (b == LB_QU && (ub.value & FINAL) != 0) {232 if (is_any(after, AFTER_FINAL_QUOTE)) {233 return ::Prohibited; // LB15b234 }235 }236 if (a == LB_SP && b == LB_IS && after == LB_NU) {237 return ::Allowed; // LB15c238 }239 if (b == LB_IS) {240 return ::Prohibited; // LB15d241 }242 if (b == LB_NS && (before == LB_CL || before == LB_CP)) {243 return ::Prohibited; // LB16244 }245 if (b == LB_B2 && before == LB_B2) {246 return ::Prohibited; // LB17247 }248 if (a == LB_SP) {249 return ::Allowed; // LB18250 }251 if (b == LB_QU && (ub.value & INITIAL) == 0) {252 return ::Prohibited; // LB19253 }254 if (a == LB_QU && (ua.value & FINAL) == 0) {255 return ::Prohibited;256 }257 if (b == LB_QU) {258 // LB19a259 if (!east_asian(&ua)) {260 return ::Prohibited;261 }262 if (after == EDGE || !east_asian(&units[k + 1])) {263 return ::Prohibited;264 }265 }266 if (a == LB_QU) {267 if (!east_asian(&ub)) {268 return ::Prohibited;269 }270 if (k < 2 || !east_asian(&units[k - 2])) {271 return ::Prohibited;272 }273 }274 if (a == LB_CB || b == LB_CB) {275 return ::Allowed; // LB20276 }277 if ((a == LB_HY || ua.c == 0x2010) && b == LB_AL) {278 const outer = class_at(&units, k to i64 - 2);279 if (is_any(outer, BEFORE_DOTTED_CIRCLE)) {280 return ::Prohibited; // LB20a281 }282 }283 if (b == LB_BA || b == LB_HY || b == LB_NS || a == LB_BB) {284 return ::Prohibited; // LB21285 }286 if (287 (a == LB_HY || (a == LB_BA && !east_asian(&ua)))288 && class_at(&units, k to i64 - 2) == LB_HL289 && b != LB_HL290 ) {291 return ::Prohibited; // LB21a292 }293 if (a == LB_SY && b == LB_HL) {294 return ::Prohibited; // LB21b295 }296 if (b == LB_IN) {297 return ::Prohibited; // LB22298 }299 const letter_a = a == LB_AL || a == LB_HL;300 const letter_b = b == LB_AL || b == LB_HL;301 if ((letter_a && b == LB_NU) || (a == LB_NU && letter_b)) {302 return ::Prohibited; // LB23303 }304 if (305 (a == LB_PR && is_any(b, IDEOGRAPHIC))306 || (is_any(a, IDEOGRAPHIC) && b == LB_PO)307 ) {308 return ::Prohibited; // LB23a309 }310 if (((a == LB_PR || a == LB_PO) && letter_b) || (letter_a && (b == LB_PR || b == LB_PO))) {311 return ::Prohibited; // LB24312 }313 // LB25314 if (b == LB_PO || b == LB_PR) {315 if ((a == LB_CL || a == LB_CP) && numeric_before(&units, k to i64 - 2)) {316 return ::Prohibited;317 }318 if (numeric_before(&units, k to i64 - 1)) {319 return ::Prohibited;320 }321 }322 if (a == LB_PO || a == LB_PR) {323 if (b == LB_NU) {324 return ::Prohibited;325 }326 if (327 b == LB_OP328 && (after == LB_NU || (after == LB_IS && class_at(&units, k to i64 + 2) == LB_NU))329 ) {330 return ::Prohibited;331 }332 }333 if ((a == LB_HY || a == LB_IS) && b == LB_NU) {334 return ::Prohibited;335 }336 if (b == LB_NU && numeric_before(&units, k to i64 - 1)) {337 return ::Prohibited;338 }339 // LB26, LB27340 if (a == LB_JL && is_any(b, AFTER_JL)) {341 return ::Prohibited;342 }343 if ((a == LB_JV || a == LB_H2) && (b == LB_JV || b == LB_JT)) {344 return ::Prohibited;345 }346 if ((a == LB_JT || a == LB_H3) && b == LB_JT) {347 return ::Prohibited;348 }349 if (is_any(a, KOREAN) && b == LB_PO) {350 return ::Prohibited;351 }352 if (a == LB_PR && is_any(b, KOREAN)) {353 return ::Prohibited;354 }355 if (letter_a && letter_b) {356 return ::Prohibited; // LB28357 }358 // LB28a359 const aksara_a = a == LB_AK || a == LB_AS || ua.c == 0x25CC;360 const aksara_b = b == LB_AK || b == LB_AS || ub.c == 0x25CC;361 if (a == LB_AP && aksara_b) {362 return ::Prohibited;363 }364 if (aksara_a && (b == LB_VF || b == LB_VI)) {365 return ::Prohibited;366 }367 if (a == LB_VI && (b == LB_AK || ub.c == 0x25CC) && k >= 2) {368 const outer = &units[k - 2];369 if (outer.class == LB_AK || outer.class == LB_AS || outer.c == 0x25CC) {370 return ::Prohibited;371 }372 }373 if (aksara_a && aksara_b && after == LB_VF) {374 return ::Prohibited;375 }376 if (a == LB_IS && letter_b) {377 return ::Prohibited; // LB29378 }379 if ((letter_a || a == LB_NU) && b == LB_OP && !east_asian(&ub)) {380 return ::Prohibited; // LB30381 }382 if (a == LB_CP && !east_asian(&ua) && (letter_b || b == LB_NU)) {383 return ::Prohibited;384 }385 if (a == LB_RI && b == LB_RI) {386 // LB30a: an odd count of regional indicators ends at a387 var count: u64 = 0;388 var i = k to i64 - 1;389 while (i >= 0 && units[i to u64].class == LB_RI) {390 count += 1;391 i -= 1;392 }393 if (count % 2 == 1) {394 return ::Prohibited;395 }396 }397 if (398 b == LB_EM399 && (a == LB_EB || ((ua.value & PICTOGRAPHIC) != 0 && (ua.value & UNASSIGNED) != 0))400 ) {401 return ::Prohibited; // LB30b402 }403 return ::Allowed; // LB31404}