Debian Codegraph @

debian/rust-vendor/icu_segmenter/src/line/v1.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use super::{LineBreakStrictness, LineBreakWordOption, ResolvedLineBreakOptions};
6 use crate::complex::*;
7 use crate::provider::*;
8 use crate::scaffold::*;
9 use alloc::string::String;
10 use alloc::vec;
11 use alloc::vec::Vec;
12 use core::char;
13
14 #[doc(hidden)]
15 impl RuleBreakData<'_> {
16 pub const LINE_PROPERTY_AI: u8 = 1;
17 pub const LINE_PROPERTY_AL: u8 = 3;
18 pub const LINE_PROPERTY_BA: u8 = 8;
19 pub const LINE_PROPERTY_BK: u8 = 10;
20 pub const LINE_PROPERTY_CJ: u8 = 12;
21 pub const LINE_PROPERTY_CM: u8 = 14;
22 pub const LINE_PROPERTY_CR: u8 = 16;
23 pub const LINE_PROPERTY_EX: u8 = 19;
24 pub const LINE_PROPERTY_H2: u8 = 21;
25 pub const LINE_PROPERTY_H3: u8 = 22;
26 pub const LINE_PROPERTY_HY: u8 = 24;
27 pub const LINE_PROPERTY_ID: u8 = 25;
28 pub const LINE_PROPERTY_IN: u8 = 27;
29 pub const LINE_PROPERTY_JL: u8 = 29;
30 pub const LINE_PROPERTY_JT: u8 = 30;
31 pub const LINE_PROPERTY_JV: u8 = 31;
32 pub const LINE_PROPERTY_LF: u8 = 32;
33 pub const LINE_PROPERTY_NL: u8 = 33;
34 pub const LINE_PROPERTY_NS: u8 = 34;
35 pub const LINE_PROPERTY_NU: u8 = 35;
36 pub const LINE_PROPERTY_PO_EAW: u8 = 39;
37 pub const LINE_PROPERTY_PR_EAW: u8 = 41;
38 pub const LINE_PROPERTY_SP: u8 = 47;
39 pub const LINE_PROPERTY_ZW: u8 = 53;
40 pub const LINE_PROPERTY_ZWJ: u8 = 54;
41 }
42
43 #[cfg_attr(not(test), allow(dead_code))]
44 #[doc(hidden)]
45 impl RuleBreakData<'_> {
46 pub const LINE_PROPERTY_AK: u8 = 2;
47 pub const LINE_PROPERTY_AL_DOTTED_CIRCLE: u8 = 4;
48 pub const LINE_PROPERTY_AP: u8 = 5;
49 pub const LINE_PROPERTY_AS: u8 = 6;
50 pub const LINE_PROPERTY_B2: u8 = 7;
51 pub const LINE_PROPERTY_BB: u8 = 9;
52 pub const LINE_PROPERTY_CB: u8 = 11;
53 pub const LINE_PROPERTY_CL: u8 = 13;
54 pub const LINE_PROPERTY_CP: u8 = 15;
55 pub const LINE_PROPERTY_EB: u8 = 17;
56 pub const LINE_PROPERTY_EM: u8 = 18;
57 pub const LINE_PROPERTY_GL: u8 = 20;
58 pub const LINE_PROPERTY_HL: u8 = 23;
59 pub const LINE_PROPERTY_ID_CN: u8 = 26;
60 pub const LINE_PROPERTY_IS: u8 = 28;
61 pub const LINE_PROPERTY_OP_EA: u8 = 36;
62 pub const LINE_PROPERTY_OP_OP30: u8 = 37;
63 pub const LINE_PROPERTY_PO: u8 = 38;
64 pub const LINE_PROPERTY_PR: u8 = 40;
65 pub const LINE_PROPERTY_QU: u8 = 42;
66 pub const LINE_PROPERTY_QU_PF: u8 = 43;
67 pub const LINE_PROPERTY_QU_PI: u8 = 44;
68 pub const LINE_PROPERTY_RI: u8 = 45;
69 pub const LINE_PROPERTY_SY: u8 = 48;
70 pub const LINE_PROPERTY_VF: u8 = 49;
71 pub const LINE_PROPERTY_VI: u8 = 50;
72 pub const LINE_PROPERTY_WJ: u8 = 51;
73 pub const LINE_PROPERTY_XX: u8 = 52;
74 }
75
76 fn is_break_utf32_by_normal(codepoint: u32, ja_zh: bool) -> bool {
77 matches!(codepoint, 0x301C | 0x30A0 if ja_zh)
78 }
79
80 #[inline]
81 fn is_break_utf32_by_loose(
82 right_codepoint: u32,
83 left_prop: u8,
84 right_prop: u8,
85 ja_zh: bool,
86 ) -> Option<bool> {
87 Some(match (right_prop, right_codepoint, left_prop) {
88 // breaks before hyphens
89 (RuleBreakData::LINE_PROPERTY_BA, 0x2010 | 0x2013, RuleBreakData::LINE_PROPERTY_ID) => true,
90 // breaks before certain CJK hyphen-like characters
91 (RuleBreakData::LINE_PROPERTY_NS, 0x301C | 0x30A0, _) => ja_zh,
92 // breaks before iteration marks
93 (
94 RuleBreakData::LINE_PROPERTY_NS,
95 0x3005 | 0x303B | 0x309D | 0x309E | 0x30FD | 0x30FE,
96 _,
97 ) => true,
98 // breaks before certain centered punctuation marks:
99 (
100 RuleBreakData::LINE_PROPERTY_NS,
101 0x30FB | 0xFF1A | 0xFF1B | 0xFF65 | 0x203C | 0x2047..=0x2049,
102 _,
103 ) => ja_zh,
104 // breaks between inseparable characters such as U+2025, U+2026 i.e. characters with the Unicode Line Break property IN
105 (RuleBreakData::LINE_PROPERTY_IN, _, RuleBreakData::LINE_PROPERTY_IN) => true,
106 // breaks before certain centered punctuation marks:
107 (RuleBreakData::LINE_PROPERTY_EX, 0xFF01 | 0xFF1F, _) => ja_zh,
108 // breaks before suffixes:
109 // Characters with the Unicode Line Break property PO and the East Asian Width property
110 (RuleBreakData::LINE_PROPERTY_PO_EAW, _, _) => ja_zh,
111 // breaks after prefixes:
112 // Characters with the Unicode Line Break property PR and the East Asian Width property
113 (_, _, RuleBreakData::LINE_PROPERTY_PR_EAW) => ja_zh,
114 _ => return None,
115 })
116 }
117
118 #[derive(Debug)]
119 pub(super) struct LineBreakIteratorV1<'data, 's, Y: RuleBreakType> {
120 iter: Y::IterAttr<'s>,
121 len: usize,
122 current_pos_data: Option<(usize, Y::CharType)>,
123 result_cache: Vec<usize>,
124 data: &'data RuleBreakData<'data>,
125 options: ResolvedLineBreakOptions,
126 complex: ComplexPayloadsBorrowed<'data>,
127 // Should return None if there is no complex handling
128 pub(crate) handle_complex:
129 fn(&mut LineBreakIteratorV1<'data, 's, Y>, Y::CharType) -> Option<usize>,
130 }
131
132 impl<'data, 's, Y: RuleBreakType> LineBreakIteratorV1<'data, 's, Y> {
133 pub(crate) fn new(
134 iter: Y::IterAttr<'s>,
135 len: usize,
136 data: &'data RuleBreakData<'data>,
137 options: ResolvedLineBreakOptions,
138 complex: ComplexPayloadsBorrowed<'data>,
139 handle_complex: fn(&mut LineBreakIteratorV1<'data, 's, Y>, Y::CharType) -> Option<usize>,
140 ) -> Self {
141 Self {
142 iter,
143 len,
144 current_pos_data: None,
145 result_cache: Vec::new(),
146 data,
147 options,
148 complex,
149 handle_complex,
150 }
151 }
152 }
153
154 impl<Y: RuleBreakType> Iterator for LineBreakIteratorV1<'_, '_, Y> {
155 type Item = usize;
156
157 fn next(&mut self) -> Option<Self::Item> {
158 if self.options.strictness == LineBreakStrictness::Anywhere {
159 use crate::grapheme::*;
160 let mut grapheme_iter =
161 GraphemeClusterBreakIterator(GraphemeClusterBreakIteratorInner::V1(
162 crate::rule_segmenter_v1::RuleBreakIterator {
163 iter: self.iter.clone(),
164 len: self.len,
165 current_pos_data: self.current_pos_data,
166 data: match self.complex.grapheme.0 {
167 GraphemeClusterSegmenterBorrowedInner::V1(data) => data,
168 #[cfg(feature = "unstable")]
169 GraphemeClusterSegmenterBorrowedInner::V2(_) => unreachable!(),
170 },
171 result_cache: Default::default(),
172 complex: None,
173 boundary_property: 0,
174 locale_override: None,
175 handle_complex: crate::rule_segmenter_v1::empty_handle_complex::<Y>,
176 },
177 ));
178 let r = grapheme_iter.next();
179 #[cfg_attr(not(feature = "unstable"), allow(irrefutable_let_patterns))]
180 let GraphemeClusterBreakIterator(GraphemeClusterBreakIteratorInner::V1(grapheme_iter)) =
181 grapheme_iter
182 else {
183 unreachable!();
184 };
185 self.iter = grapheme_iter.iter;
186 self.len = grapheme_iter.len;
187 self.current_pos_data = grapheme_iter.current_pos_data;
188 return r;
189 }
190
191 match self.check_eof() {
192 StringBoundaryPosType::Start => return Some(0),
193 StringBoundaryPosType::End => return None,
194 _ => (),
195 }
196
197 // If we have break point cache by previous run, return this result
198 if let Some(&first_pos) = self.result_cache.first() {
199 let mut i = 0;
200 loop {
201 if i == first_pos {
202 self.result_cache = self.result_cache.iter().skip(1).map(|r| r - i).collect();
203 return self.get_current_position();
204 }
205 i += self.get_current_codepoint().map_or(0, Y::char_len);
206 self.advance_iter();
207 if self.is_eof() {
208 self.result_cache.clear();
209 return Some(self.len);
210 }
211 }
212 }
213
214 // The state prior to a sequence of CM and ZWJ affected by rule LB9.
215 let mut lb9_left: Option<u8> = None;
216 // Whether LB9 was applied to a ZWJ, so that breaks at the current
217 // position must be suppressed.
218 let mut lb8a_after_lb9 = false;
219
220 'a: loop {
221 debug_assert!(!self.is_eof());
222
223 let left_codepoint = self.get_current_codepoint()?;
224 self.advance_iter();
225 let Some(right_codepoint) = self.get_current_codepoint() else {
226 return Some(self.len);
227 };
228
229 let left_prop = lb9_left.unwrap_or_else(|| self.get_linebreak_property(left_codepoint));
230 let right_prop = self.get_linebreak_property(right_codepoint);
231
232 // UAX14 doesn't have Thai etc, so use another way.
233 if Y::CAN_CONTAIN_SA
234 && self.get_linebreak_property(left_codepoint) == self.data.complex_property
235 && right_prop == self.data.complex_property
236 {
237 let result = (self.handle_complex)(self, left_codepoint);
238 if result.is_some() {
239 return result;
240 }
241 // I may have to fetch text until non-SA character?.
242 }
243
244 let after_zwj = lb8a_after_lb9
245 || (lb9_left.is_none() && left_prop == RuleBreakData::LINE_PROPERTY_ZWJ);
246
247 if (right_prop == RuleBreakData::LINE_PROPERTY_CM
248 || right_prop == RuleBreakData::LINE_PROPERTY_ZWJ)
249 && left_prop != RuleBreakData::LINE_PROPERTY_BK
250 && left_prop != RuleBreakData::LINE_PROPERTY_CR
251 && left_prop != RuleBreakData::LINE_PROPERTY_LF
252 && left_prop != RuleBreakData::LINE_PROPERTY_NL
253 && left_prop != RuleBreakData::LINE_PROPERTY_SP
254 && left_prop != RuleBreakData::LINE_PROPERTY_ZW
255 {
256 lb9_left = Some(left_prop);
257 lb8a_after_lb9 = right_prop == RuleBreakData::LINE_PROPERTY_ZWJ;
258 continue;
259 } else {
260 lb9_left = None;
261 lb8a_after_lb9 = false;
262 }
263
264 // CSS word-break property handling
265 #[allow(clippy::single_match)]
266 if self.options.word_option == LineBreakWordOption::KeepAll {
267 // typographic letter units shouldn't be break
268 if matches!(
269 left_prop,
270 RuleBreakData::LINE_PROPERTY_AI
271 | RuleBreakData::LINE_PROPERTY_AL
272 | RuleBreakData::LINE_PROPERTY_ID
273 | RuleBreakData::LINE_PROPERTY_NU
274 | RuleBreakData::LINE_PROPERTY_HY
275 | RuleBreakData::LINE_PROPERTY_H2
276 | RuleBreakData::LINE_PROPERTY_H3
277 | RuleBreakData::LINE_PROPERTY_JL
278 | RuleBreakData::LINE_PROPERTY_JV
279 | RuleBreakData::LINE_PROPERTY_JT
280 | RuleBreakData::LINE_PROPERTY_CJ
281 ) && matches!(
282 right_prop,
283 RuleBreakData::LINE_PROPERTY_AI
284 | RuleBreakData::LINE_PROPERTY_AL
285 | RuleBreakData::LINE_PROPERTY_ID
286 | RuleBreakData::LINE_PROPERTY_NU
287 | RuleBreakData::LINE_PROPERTY_HY
288 | RuleBreakData::LINE_PROPERTY_H2
289 | RuleBreakData::LINE_PROPERTY_H3
290 | RuleBreakData::LINE_PROPERTY_JL
291 | RuleBreakData::LINE_PROPERTY_JV
292 | RuleBreakData::LINE_PROPERTY_JT
293 | RuleBreakData::LINE_PROPERTY_CJ
294 ) {
295 continue;
296 }
297 }
298
299 // CSS line-break property handling
300 match self.options.strictness {
301 LineBreakStrictness::Normal
302 if is_break_utf32_by_normal(right_codepoint.into(), self.options.ja_zh)
303 && !after_zwj =>
304 {
305 return self.get_current_position();
306 }
307 LineBreakStrictness::Loose => {
308 if let Some(breakable) = is_break_utf32_by_loose(
309 right_codepoint.into(),
310 left_prop,
311 right_prop,
312 self.options.ja_zh,
313 ) {
314 if breakable && !after_zwj {
315 return self.get_current_position();
316 }
317 continue;
318 }
319 }
320 _ => (),
321 };
322
323 // If break_state is equals or grater than 0, it is alias of property.
324 match self.data.get_break_state_from_table(left_prop, right_prop) {
325 BreakState::Break | BreakState::NoMatch => {
326 if after_zwj {
327 continue;
328 } else {
329 return self.get_current_position();
330 }
331 }
332 BreakState::Keep => continue,
333 BreakState::Index(mut index) | BreakState::Intermediate(mut index) => {
334 let mut previous_iter = self.iter.clone();
335 let mut previous_pos_data = self.current_pos_data;
336 let mut previous_is_after_zwj = after_zwj;
337
338 // Since we are building up a state in this inner loop, we do not
339 // need an analogue of lb9_left; continuing the inner loop preserves
340 // `index` which is the current state, and thus implements the
341 // “treat as” rule.
342 let mut left_prop_pre_lb9 = right_prop;
343
344 // current state isn't resolved due to intermediating.
345 // Example, [AK] [AS] is processing LB28a, but if not matched after fetching
346 // data, we should break after [AK].
347 let is_intermediate_rule_no_match = if lb8a_after_lb9 {
348 // left was ZWJ so we don't break between ZWJ.
349 true
350 } else {
351 index > self.data.last_codepoint_property
352 };
353
354 loop {
355 self.advance_iter();
356 let after_zwj = left_prop_pre_lb9 == RuleBreakData::LINE_PROPERTY_ZWJ;
357
358 let previous_break_state_is_cp_prop =
359 index <= self.data.last_codepoint_property;
360
361 let Some(prop) = self.get_current_linebreak_property() else {
362 // Reached EOF. But we are analyzing multiple characters now, so next break may be previous point.
363 let break_state = self
364 .data
365 .get_break_state_from_table(index, self.data.eot_property);
366 if break_state == BreakState::NoMatch {
367 self.iter = previous_iter;
368 self.current_pos_data = previous_pos_data;
369 if previous_is_after_zwj {
370 // Do not break [AK] [ZWJ] ÷ [AS] (eot).
371 continue 'a;
372 } else {
373 return self.get_current_position();
374 }
375 }
376 // EOF
377 return Some(self.len);
378 };
379
380 if (prop == RuleBreakData::LINE_PROPERTY_CM
381 || prop == RuleBreakData::LINE_PROPERTY_ZWJ)
382 && left_prop_pre_lb9 != RuleBreakData::LINE_PROPERTY_BK
383 && left_prop_pre_lb9 != RuleBreakData::LINE_PROPERTY_CR
384 && left_prop_pre_lb9 != RuleBreakData::LINE_PROPERTY_LF
385 && left_prop_pre_lb9 != RuleBreakData::LINE_PROPERTY_NL
386 && left_prop_pre_lb9 != RuleBreakData::LINE_PROPERTY_SP
387 && left_prop_pre_lb9 != RuleBreakData::LINE_PROPERTY_ZW
388 {
389 left_prop_pre_lb9 = prop;
390 continue;
391 }
392
393 match self.data.get_break_state_from_table(index, prop) {
394 BreakState::Keep => continue 'a,
395 BreakState::NoMatch => {
396 self.iter = previous_iter;
397 self.current_pos_data = previous_pos_data;
398 if after_zwj {
399 // Break [AK] ÷ [AS] [ZWJ] [XX],
400 // but not [AK] [ZWJ] ÷ [AS] [ZWJ] [XX].
401 if is_intermediate_rule_no_match && !previous_is_after_zwj {
402 return self.get_current_position();
403 }
404 continue 'a;
405 } else if previous_is_after_zwj {
406 // Do not break [AK] [ZWJ] ÷ [AS] [XX].
407 continue 'a;
408 } else {
409 return self.get_current_position();
410 }
411 }
412 BreakState::Break => {
413 if after_zwj {
414 continue 'a;
415 } else {
416 return self.get_current_position();
417 }
418 }
419 BreakState::Intermediate(i) => {
420 index = i;
421 previous_iter = self.iter.clone();
422 previous_pos_data = self.current_pos_data;
423 previous_is_after_zwj = after_zwj;
424 }
425 BreakState::Index(i) => {
426 index = i;
427 if previous_break_state_is_cp_prop {
428 previous_iter = self.iter.clone();
429 previous_pos_data = self.current_pos_data;
430 previous_is_after_zwj = after_zwj;
431 }
432 }
433 }
434 left_prop_pre_lb9 = prop;
435 }
436 }
437 }
438 }
439 }
440 }
441
442 enum StringBoundaryPosType {
443 Start,
444 Middle,
445 End,
446 }
447
448 impl<Y: RuleBreakType> LineBreakIteratorV1<'_, '_, Y> {
449 fn advance_iter(&mut self) {
450 self.current_pos_data = self.iter.next();
451 }
452
453 fn is_eof(&self) -> bool {
454 self.current_pos_data.is_none()
455 }
456
457 #[inline]
458 fn check_eof(&mut self) -> StringBoundaryPosType {
459 if self.is_eof() {
460 self.advance_iter();
461 if self.is_eof() {
462 if self.len == 0 {
463 // Empty string. Since `self.current_pos_data` is always going to be empty,
464 // we never read `self.len` except for here, so we can use it to mark that
465 // we have already returned the single empty-string breakpoint.
466 self.len = 1;
467 StringBoundaryPosType::Start
468 } else {
469 StringBoundaryPosType::End
470 }
471 } else {
472 StringBoundaryPosType::Start
473 }
474 } else {
475 StringBoundaryPosType::Middle
476 }
477 }
478
479 fn get_current_position(&self) -> Option<usize> {
480 self.current_pos_data.map(|(pos, _)| pos)
481 }
482
483 fn get_current_codepoint(&self) -> Option<Y::CharType> {
484 self.current_pos_data.map(|(_, codepoint)| codepoint)
485 }
486
487 fn get_linebreak_property(&self, codepoint: Y::CharType) -> u8 {
488 match (
489 (self.options.word_option, self.options.strictness),
490 self.data.property_table.get32(codepoint.into()),
491 ) {
492 // CJ is treated as NS by default, yielding strict line breaking.
493 // https://www.unicode.org/reports/tr14/#CJ
494 (
495 (LineBreakWordOption::BreakAll, _)
496 | (_, LineBreakStrictness::Loose | LineBreakStrictness::Normal),
497 RuleBreakData::LINE_PROPERTY_CJ,
498 ) => RuleBreakData::LINE_PROPERTY_ID, // All CJ's General_Category is Other_Letter (Lo).
499 ((LineBreakWordOption::BreakAll, _), p) if p == self.data.complex_property => {
500 RuleBreakData::LINE_PROPERTY_ID
501 }
502 (
503 (LineBreakWordOption::BreakAll, _),
504 RuleBreakData::LINE_PROPERTY_AL | RuleBreakData::LINE_PROPERTY_NU,
505 ) => RuleBreakData::LINE_PROPERTY_ID,
506 (_, prop) => prop,
507 }
508 }
509
510 fn get_current_linebreak_property(&self) -> Option<u8> {
511 self.get_current_codepoint()
512 .map(|c| self.get_linebreak_property(c))
513 }
514 }
515
516 pub(super) fn line_handle_complex_utf8<T>(
517 iter: &mut LineBreakIteratorV1<'_, '_, T>,
518 left_codepoint: char,
519 ) -> Option<usize>
520 where
521 T: RuleBreakType<CharType = char>,
522 {
523 // word segmenter doesn't define break rules for some scripts such as Thai.
524 let start_iter = iter.iter.clone();
525 let start_point = iter.current_pos_data;
526 let mut s = String::new();
527 s.push(left_codepoint);
528 loop {
529 debug_assert!(!iter.is_eof());
530 s.push(iter.get_current_codepoint()?);
531 iter.advance_iter();
532 if let Some(current_codepoint) = iter.get_current_codepoint() {
533 if iter.get_linebreak_property(current_codepoint) != iter.data.complex_property {
534 break;
535 }
536 } else {
537 // EOF
538 break;
539 }
540 }
541
542 // Restore iterator to move to head of complex string
543 iter.iter = start_iter;
544 iter.current_pos_data = start_point;
545 let breaks = iter.complex.segment_str(&s);
546 iter.result_cache = breaks;
547 let first_pos = *iter.result_cache.first()?;
548 let mut i = left_codepoint.len_utf8();
549 loop {
550 if i == first_pos {
551 // Re-calculate breaking offset
552 iter.result_cache = iter.result_cache.iter().skip(1).map(|r| r - i).collect();
553 return iter.get_current_position();
554 }
555 debug_assert!(
556 i < first_pos,
557 "we should always arrive at first_pos: near index {:?}",
558 iter.get_current_position()
559 );
560 i += iter.get_current_codepoint().map_or(0, T::char_len);
561 iter.advance_iter();
562 if iter.is_eof() {
563 iter.result_cache.clear();
564 return Some(iter.len);
565 }
566 }
567 }
568
569 pub(super) fn line_handle_complex_utf16<T>(
570 iterator: &mut LineBreakIteratorV1<'_, '_, T>,
571 left_codepoint: T::CharType,
572 ) -> Option<usize>
573 where
574 T: RuleBreakType<CharType = u32>,
575 {
576 // word segmenter doesn't define break rules for some scripts such as Thai.
577 let start_iter = iterator.iter.clone();
578 let start_point = iterator.current_pos_data;
579 let mut s = vec![left_codepoint as u16];
580 loop {
581 debug_assert!(!iterator.is_eof());
582 s.push(iterator.get_current_codepoint()? as u16);
583 iterator.advance_iter();
584 if let Some(current_codepoint) = iterator.get_current_codepoint() {
585 if iterator.get_linebreak_property(current_codepoint) != iterator.data.complex_property
586 {
587 break;
588 }
589 } else {
590 // EOF
591 break;
592 }
593 }
594
595 // Restore iterator to move to head of complex string
596 iterator.iter = start_iter;
597 iterator.current_pos_data = start_point;
598 let breaks = iterator.complex.segment_utf16(&s);
599 iterator.result_cache = breaks;
600 // result_cache vector is utf-16 index that is in BMP.
601 let first_pos = *iterator.result_cache.first()?;
602 let mut i = 1;
603 loop {
604 if i == first_pos {
605 // Re-calculate breaking offset
606 iterator.result_cache = iterator
607 .result_cache
608 .iter()
609 .skip(1)
610 .map(|r| r - i)
611 .collect();
612 return iterator.get_current_position();
613 }
614 debug_assert!(
615 i < first_pos,
616 "we should always arrive at first_pos: near index {:?}",
617 iterator.get_current_position()
618 );
619 i += 1;
620 iterator.advance_iter();
621 if iterator.is_eof() {
622 iterator.result_cache.clear();
623 return Some(iterator.len);
624 }
625 }
626 }
627
628 #[cfg(test)]
629 mod tests {
630 use super::*;
631 use crate::*;
632 use icu_provider::prelude::*;
633
634 #[test]
635 fn linebreak_property() {
636 let super::super::LineBreakIteratorInner::V1(iterator) =
637 LineSegmenter::new_for_non_complex_scripts(Default::default())
638 .segment_str("input")
639 .0
640 else {
641 unreachable!()
642 };
643
644 assert_eq!(
645 iterator.get_linebreak_property('\u{0020}'),
646 RuleBreakData::LINE_PROPERTY_SP
647 );
648 assert_eq!(
649 iterator.get_linebreak_property('\u{0022}'),
650 RuleBreakData::LINE_PROPERTY_QU
651 );
652 assert_eq!(
653 iterator.get_linebreak_property('('),
654 RuleBreakData::LINE_PROPERTY_OP_OP30
655 );
656 assert_eq!(
657 iterator.get_linebreak_property('\u{0030}'),
658 RuleBreakData::LINE_PROPERTY_NU
659 );
660 assert_eq!(
661 iterator.get_linebreak_property('['),
662 RuleBreakData::LINE_PROPERTY_OP_OP30
663 );
664 assert_eq!(
665 iterator.get_linebreak_property('\u{1f3fb}'),
666 RuleBreakData::LINE_PROPERTY_EM
667 );
668 assert_eq!(
669 iterator.get_linebreak_property('\u{20000}'),
670 RuleBreakData::LINE_PROPERTY_ID
671 );
672 assert_eq!(
673 iterator.get_linebreak_property('\u{e0020}'),
674 RuleBreakData::LINE_PROPERTY_CM
675 );
676 assert_eq!(
677 iterator.get_linebreak_property('\u{3041}'),
678 RuleBreakData::LINE_PROPERTY_CJ
679 );
680 assert_eq!(
681 iterator.get_linebreak_property('\u{0025}'),
682 RuleBreakData::LINE_PROPERTY_PO
683 );
684 assert_eq!(
685 iterator.get_linebreak_property('\u{00A7}'),
686 RuleBreakData::LINE_PROPERTY_AI
687 );
688 assert_eq!(
689 iterator.get_linebreak_property('\u{50005}'),
690 RuleBreakData::LINE_PROPERTY_XX
691 );
692 assert_eq!(
693 iterator.get_linebreak_property('\u{17D6}'),
694 RuleBreakData::LINE_PROPERTY_NS
695 );
696 assert_eq!(
697 iterator.get_linebreak_property('\u{2014}'),
698 RuleBreakData::LINE_PROPERTY_B2
699 );
700 }
701
702 #[test]
703 fn break_rule() {
704 let payload = DataProvider::<SegmenterBreakLineV1>::load(&Baked, Default::default())
705 .expect("Loading should succeed!")
706 .payload;
707 let lb_data: &RuleBreakData = payload.get();
708
709 let is_break = |left, right| {
710 matches!(
711 lb_data.get_break_state_from_table(left, right),
712 BreakState::Break | BreakState::NoMatch
713 )
714 };
715
716 // LB4
717 assert_eq!(
718 is_break(
719 RuleBreakData::LINE_PROPERTY_BK,
720 RuleBreakData::LINE_PROPERTY_AL
721 ),
722 true
723 );
724 // LB5
725 assert_eq!(
726 is_break(
727 RuleBreakData::LINE_PROPERTY_CR,
728 RuleBreakData::LINE_PROPERTY_LF
729 ),
730 false
731 );
732 assert_eq!(
733 is_break(
734 RuleBreakData::LINE_PROPERTY_CR,
735 RuleBreakData::LINE_PROPERTY_AL
736 ),
737 true
738 );
739 assert_eq!(
740 is_break(
741 RuleBreakData::LINE_PROPERTY_LF,
742 RuleBreakData::LINE_PROPERTY_AL
743 ),
744 true
745 );
746 assert_eq!(
747 is_break(
748 RuleBreakData::LINE_PROPERTY_NL,
749 RuleBreakData::LINE_PROPERTY_AL
750 ),
751 true
752 );
753 // LB6
754 assert_eq!(
755 is_break(
756 RuleBreakData::LINE_PROPERTY_AL,
757 RuleBreakData::LINE_PROPERTY_BK
758 ),
759 false
760 );
761 assert_eq!(
762 is_break(
763 RuleBreakData::LINE_PROPERTY_AL,
764 RuleBreakData::LINE_PROPERTY_CR
765 ),
766 false
767 );
768 assert_eq!(
769 is_break(
770 RuleBreakData::LINE_PROPERTY_AL,
771 RuleBreakData::LINE_PROPERTY_LF
772 ),
773 false
774 );
775 assert_eq!(
776 is_break(
777 RuleBreakData::LINE_PROPERTY_AL,
778 RuleBreakData::LINE_PROPERTY_NL
779 ),
780 false
781 );
782 // LB7
783 assert_eq!(
784 is_break(
785 RuleBreakData::LINE_PROPERTY_AL,
786 RuleBreakData::LINE_PROPERTY_SP
787 ),
788 false
789 );
790 assert_eq!(
791 is_break(
792 RuleBreakData::LINE_PROPERTY_AL,
793 RuleBreakData::LINE_PROPERTY_ZW
794 ),
795 false
796 );
797 // LB8
798 // LB8a and LB9 omitted: These are handled outside of the state table.
799 // LB10
800 assert_eq!(
801 is_break(
802 RuleBreakData::LINE_PROPERTY_ZWJ,
803 RuleBreakData::LINE_PROPERTY_SP
804 ),
805 false
806 );
807 assert_eq!(
808 is_break(
809 RuleBreakData::LINE_PROPERTY_SP,
810 RuleBreakData::LINE_PROPERTY_CM
811 ),
812 true
813 );
814 // LB11
815 assert_eq!(
816 is_break(
817 RuleBreakData::LINE_PROPERTY_AL,
818 RuleBreakData::LINE_PROPERTY_WJ
819 ),
820 false
821 );
822 assert_eq!(
823 is_break(
824 RuleBreakData::LINE_PROPERTY_WJ,
825 RuleBreakData::LINE_PROPERTY_AL
826 ),
827 false
828 );
829 // LB12
830 assert_eq!(
831 is_break(
832 RuleBreakData::LINE_PROPERTY_GL,
833 RuleBreakData::LINE_PROPERTY_AL
834 ),
835 false
836 );
837 // LB12a
838 assert_eq!(
839 is_break(
840 RuleBreakData::LINE_PROPERTY_AL,
841 RuleBreakData::LINE_PROPERTY_GL
842 ),
843 false
844 );
845 assert_eq!(
846 is_break(
847 RuleBreakData::LINE_PROPERTY_SP,
848 RuleBreakData::LINE_PROPERTY_GL
849 ),
850 true
851 );
852 // LB13
853 assert_eq!(
854 is_break(
855 RuleBreakData::LINE_PROPERTY_AL,
856 RuleBreakData::LINE_PROPERTY_CL
857 ),
858 false
859 );
860 assert_eq!(
861 is_break(
862 RuleBreakData::LINE_PROPERTY_AL,
863 RuleBreakData::LINE_PROPERTY_CP
864 ),
865 false
866 );
867 assert_eq!(
868 is_break(
869 RuleBreakData::LINE_PROPERTY_AL,
870 RuleBreakData::LINE_PROPERTY_EX
871 ),
872 false
873 );
874 assert_eq!(
875 is_break(
876 RuleBreakData::LINE_PROPERTY_AL,
877 RuleBreakData::LINE_PROPERTY_IS
878 ),
879 false
880 );
881 assert_eq!(
882 is_break(
883 RuleBreakData::LINE_PROPERTY_AL,
884 RuleBreakData::LINE_PROPERTY_SY
885 ),
886 false
887 );
888 // LB18
889 assert_eq!(
890 is_break(
891 RuleBreakData::LINE_PROPERTY_SP,
892 RuleBreakData::LINE_PROPERTY_AL
893 ),
894 true
895 );
896 // LB19
897 assert_eq!(
898 is_break(
899 RuleBreakData::LINE_PROPERTY_AL,
900 RuleBreakData::LINE_PROPERTY_QU
901 ),
902 false
903 );
904 assert_eq!(
905 is_break(
906 RuleBreakData::LINE_PROPERTY_QU,
907 RuleBreakData::LINE_PROPERTY_AL
908 ),
909 false
910 );
911 // LB20
912 assert_eq!(
913 is_break(
914 RuleBreakData::LINE_PROPERTY_AL,
915 RuleBreakData::LINE_PROPERTY_CB
916 ),
917 true
918 );
919 assert_eq!(
920 is_break(
921 RuleBreakData::LINE_PROPERTY_CB,
922 RuleBreakData::LINE_PROPERTY_AL
923 ),
924 true
925 );
926 // LB20
927 assert_eq!(
928 is_break(
929 RuleBreakData::LINE_PROPERTY_AL,
930 RuleBreakData::LINE_PROPERTY_BA
931 ),
932 false
933 );
934 assert_eq!(
935 is_break(
936 RuleBreakData::LINE_PROPERTY_AL,
937 RuleBreakData::LINE_PROPERTY_HY
938 ),
939 false
940 );
941 assert_eq!(
942 is_break(
943 RuleBreakData::LINE_PROPERTY_AL,
944 RuleBreakData::LINE_PROPERTY_NS
945 ),
946 false
947 );
948 // LB21
949 assert_eq!(
950 is_break(
951 RuleBreakData::LINE_PROPERTY_AL,
952 RuleBreakData::LINE_PROPERTY_BA
953 ),
954 false
955 );
956 assert_eq!(
957 is_break(
958 RuleBreakData::LINE_PROPERTY_BB,
959 RuleBreakData::LINE_PROPERTY_AL
960 ),
961 false
962 );
963 assert_eq!(
964 is_break(
965 RuleBreakData::LINE_PROPERTY_ID,
966 RuleBreakData::LINE_PROPERTY_BA
967 ),
968 false
969 );
970 assert_eq!(
971 is_break(
972 RuleBreakData::LINE_PROPERTY_ID,
973 RuleBreakData::LINE_PROPERTY_NS
974 ),
975 false
976 );
977 // LB21a
978 // LB21b
979 assert_eq!(
980 is_break(
981 RuleBreakData::LINE_PROPERTY_SY,
982 RuleBreakData::LINE_PROPERTY_HL
983 ),
984 false
985 );
986 // LB22
987 assert_eq!(
988 is_break(
989 RuleBreakData::LINE_PROPERTY_AL,
990 RuleBreakData::LINE_PROPERTY_IN
991 ),
992 false
993 );
994 // LB 23
995 assert_eq!(
996 is_break(
997 RuleBreakData::LINE_PROPERTY_AL,
998 RuleBreakData::LINE_PROPERTY_NU
999 ),
1000 false
1001 );
1002 assert_eq!(
1003 is_break(
1004 RuleBreakData::LINE_PROPERTY_HL,
1005 RuleBreakData::LINE_PROPERTY_NU
1006 ),
1007 false
1008 );
1009 // LB 23a
1010 assert_eq!(
1011 is_break(
1012 RuleBreakData::LINE_PROPERTY_PR,
1013 RuleBreakData::LINE_PROPERTY_ID
1014 ),
1015 false
1016 );
1017 assert_eq!(
1018 is_break(
1019 RuleBreakData::LINE_PROPERTY_PR,
1020 RuleBreakData::LINE_PROPERTY_EB
1021 ),
1022 false
1023 );
1024 assert_eq!(
1025 is_break(
1026 RuleBreakData::LINE_PROPERTY_PR,
1027 RuleBreakData::LINE_PROPERTY_EM
1028 ),
1029 false
1030 );
1031 assert_eq!(
1032 is_break(
1033 RuleBreakData::LINE_PROPERTY_ID,
1034 RuleBreakData::LINE_PROPERTY_PO
1035 ),
1036 false
1037 );
1038 assert_eq!(
1039 is_break(
1040 RuleBreakData::LINE_PROPERTY_EB,
1041 RuleBreakData::LINE_PROPERTY_PO
1042 ),
1043 false
1044 );
1045 assert_eq!(
1046 is_break(
1047 RuleBreakData::LINE_PROPERTY_EM,
1048 RuleBreakData::LINE_PROPERTY_PO
1049 ),
1050 false
1051 );
1052 // LB26
1053 assert_eq!(
1054 is_break(
1055 RuleBreakData::LINE_PROPERTY_JL,
1056 RuleBreakData::LINE_PROPERTY_JL
1057 ),
1058 false
1059 );
1060 assert_eq!(
1061 is_break(
1062 RuleBreakData::LINE_PROPERTY_JL,
1063 RuleBreakData::LINE_PROPERTY_JV
1064 ),
1065 false
1066 );
1067 assert_eq!(
1068 is_break(
1069 RuleBreakData::LINE_PROPERTY_JL,
1070 RuleBreakData::LINE_PROPERTY_H2
1071 ),
1072 false
1073 );
1074 // LB27
1075 assert_eq!(
1076 is_break(
1077 RuleBreakData::LINE_PROPERTY_JL,
1078 RuleBreakData::LINE_PROPERTY_IN
1079 ),
1080 false
1081 );
1082 assert_eq!(
1083 is_break(
1084 RuleBreakData::LINE_PROPERTY_JL,
1085 RuleBreakData::LINE_PROPERTY_PO
1086 ),
1087 false
1088 );
1089 assert_eq!(
1090 is_break(
1091 RuleBreakData::LINE_PROPERTY_PR,
1092 RuleBreakData::LINE_PROPERTY_JL
1093 ),
1094 false
1095 );
1096 // LB28
1097 assert_eq!(
1098 is_break(
1099 RuleBreakData::LINE_PROPERTY_AL,
1100 RuleBreakData::LINE_PROPERTY_AL
1101 ),
1102 false
1103 );
1104 assert_eq!(
1105 is_break(
1106 RuleBreakData::LINE_PROPERTY_HL,
1107 RuleBreakData::LINE_PROPERTY_AL
1108 ),
1109 false
1110 );
1111 // LB29
1112 assert_eq!(
1113 is_break(
1114 RuleBreakData::LINE_PROPERTY_IS,
1115 RuleBreakData::LINE_PROPERTY_AL
1116 ),
1117 false
1118 );
1119 assert_eq!(
1120 is_break(
1121 RuleBreakData::LINE_PROPERTY_IS,
1122 RuleBreakData::LINE_PROPERTY_HL
1123 ),
1124 false
1125 );
1126 // LB30b
1127 assert_eq!(
1128 is_break(
1129 RuleBreakData::LINE_PROPERTY_EB,
1130 RuleBreakData::LINE_PROPERTY_EM
1131 ),
1132 false
1133 );
1134 // LB31
1135 assert_eq!(
1136 is_break(
1137 RuleBreakData::LINE_PROPERTY_ID,
1138 RuleBreakData::LINE_PROPERTY_ID
1139 ),
1140 true
1141 );
1142 }
1143 }