| 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 crate::complex::ComplexPayloadsBorrowed; |
| 6 |
use crate::provider::*; |
| 7 |
use crate::scaffold::RuleBreakType; |
| 8 |
use alloc::vec::Vec; |
| 9 |
|
| 10 |
/// Implements the [`Iterator`] trait over the segmenter boundaries of the given string. |
| 11 |
/// |
| 12 |
/// Lifetimes: |
| 13 |
/// |
| 14 |
/// - `'l` = lifetime of the segmenter object from which this iterator was created |
| 15 |
/// - `'data` = lifetime of data borrowed by segmenter object |
| 16 |
/// (this largely exists because segmenter data is invariant due to `ZeroMap` constraints, |
| 17 |
/// think of it as a second 'l) |
| 18 |
/// - `'s` = lifetime of the string being segmented |
| 19 |
/// |
| 20 |
/// The [`Iterator::Item`] is an [`usize`] representing index of a code unit |
| 21 |
/// _after_ the boundary (for a boundary at the end of text, this index is the length |
| 22 |
/// of the [`str`] or array of code units). |
| 23 |
#[derive(Debug)] |
| 24 |
pub struct RuleBreakIterator<'data, 's, Y: RuleBreakType> { |
| 25 |
pub(crate) iter: Y::IterAttr<'s>, |
| 26 |
pub(crate) len: usize, |
| 27 |
pub(crate) current_pos_data: Option<(usize, Y::CharType)>, |
| 28 |
pub(crate) result_cache: Vec<usize>, |
| 29 |
pub(crate) data: &'data RuleBreakData<'data>, |
| 30 |
pub(crate) complex: Option<ComplexPayloadsBorrowed<'data>>, |
| 31 |
// The property associated with the previous break |
| 32 |
pub(crate) boundary_property: u8, |
| 33 |
pub(crate) locale_override: Option<&'data RuleBreakDataOverride<'data>>, |
| 34 |
// Should return None if there is no complex script handling |
| 35 |
pub(crate) handle_complex: |
| 36 |
fn(&mut RuleBreakIterator<'data, 's, Y>, Y::CharType) -> Option<usize>, |
| 37 |
} |
| 38 |
|
| 39 |
pub(crate) fn empty_handle_complex<Y: RuleBreakType>( |
| 40 |
_i: &mut RuleBreakIterator<'_, '_, Y>, |
| 41 |
_c: Y::CharType, |
| 42 |
) -> Option<usize> { |
| 43 |
debug_assert!( |
| 44 |
false, |
| 45 |
"grapheme/sentence segmenters should never need complex handling" |
| 46 |
); |
| 47 |
None |
| 48 |
} |
| 49 |
|
| 50 |
impl<Y: RuleBreakType> Iterator for RuleBreakIterator<'_, '_, Y> { |
| 51 |
type Item = usize; |
| 52 |
|
| 53 |
fn next(&mut self) -> Option<Self::Item> { |
| 54 |
// If we have break point cache by previous run, return this result |
| 55 |
if let Some(&first_result) = self.result_cache.first() { |
| 56 |
let mut i = 0; |
| 57 |
loop { |
| 58 |
if i == first_result { |
| 59 |
self.result_cache = self.result_cache.iter().skip(1).map(|r| r - i).collect(); |
| 60 |
return self.get_current_position(); |
| 61 |
} |
| 62 |
i += self.get_current_codepoint().map_or(0, Y::char_len); |
| 63 |
self.advance_iter(); |
| 64 |
if self.is_eof() { |
| 65 |
self.result_cache.clear(); |
| 66 |
self.boundary_property = self.data.complex_property; |
| 67 |
return Some(self.len); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if self.is_eof() { |
| 73 |
self.advance_iter(); |
| 74 |
if self.is_eof() && self.len == 0 { |
| 75 |
// Empty string. Since `self.current_pos_data` is always going to be empty, |
| 76 |
// we never read `self.len` except for here, so we can use it to mark that |
| 77 |
// we have already returned the single empty-string breakpoint. |
| 78 |
self.len = 1; |
| 79 |
return Some(0); |
| 80 |
} |
| 81 |
let Some(right_prop) = self.get_current_break_property() else { |
| 82 |
// iterator already reaches to EOT. Reset boundary property. |
| 83 |
self.boundary_property = 0; |
| 84 |
return None; |
| 85 |
}; |
| 86 |
// SOT x anything |
| 87 |
if matches!( |
| 88 |
self.data |
| 89 |
.get_break_state_from_table(self.data.sot_property, right_prop), |
| 90 |
BreakState::Break | BreakState::NoMatch |
| 91 |
) { |
| 92 |
self.boundary_property = 0; // SOT is special type |
| 93 |
return self.get_current_position(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
'a: loop { |
| 98 |
debug_assert!(!self.is_eof()); |
| 99 |
let left_codepoint = self.get_current_codepoint()?; |
| 100 |
let left_prop = self.get_break_property(left_codepoint); |
| 101 |
self.advance_iter(); |
| 102 |
|
| 103 |
let Some(right_prop) = self.get_current_break_property() else { |
| 104 |
self.boundary_property = left_prop; |
| 105 |
return Some(self.len); |
| 106 |
}; |
| 107 |
|
| 108 |
// Some scripts rules doesn't have segmentation rules, we have to use LSTM (or dictionary) segmenter. |
| 109 |
// If property is marked as SA, use it |
| 110 |
if Y::CAN_CONTAIN_SA && right_prop == self.data.complex_property { |
| 111 |
if left_prop != self.data.complex_property { |
| 112 |
// break before SA |
| 113 |
self.boundary_property = left_prop; |
| 114 |
return self.get_current_position(); |
| 115 |
} |
| 116 |
let break_offset = (self.handle_complex)(self, left_codepoint); |
| 117 |
self.boundary_property = self.data.complex_property; |
| 118 |
if break_offset.is_some() { |
| 119 |
return break_offset; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
match self.data.get_break_state_from_table(left_prop, right_prop) { |
| 124 |
BreakState::Keep => continue, |
| 125 |
BreakState::Break | BreakState::NoMatch => { |
| 126 |
self.boundary_property = left_prop; |
| 127 |
return self.get_current_position(); |
| 128 |
} |
| 129 |
BreakState::Index(mut index) | BreakState::Intermediate(mut index) => { |
| 130 |
// This isn't simple rule set. We need marker to restore iterator to previous position. |
| 131 |
let mut previous_iter = self.iter.clone(); |
| 132 |
let mut previous_pos_data = self.current_pos_data; |
| 133 |
let mut previous_left_prop = left_prop; |
| 134 |
|
| 135 |
loop { |
| 136 |
self.advance_iter(); |
| 137 |
|
| 138 |
let Some(prop) = self.get_current_break_property() else { |
| 139 |
// Reached EOF. But we are analyzing multiple characters now, so next break may be previous point. |
| 140 |
self.boundary_property = index; |
| 141 |
if (self |
| 142 |
.data |
| 143 |
.get_break_state_from_table(index, self.data.eot_property)) |
| 144 |
== BreakState::NoMatch |
| 145 |
{ |
| 146 |
self.boundary_property = previous_left_prop; |
| 147 |
self.iter = previous_iter; |
| 148 |
self.current_pos_data = previous_pos_data; |
| 149 |
return self.get_current_position(); |
| 150 |
} |
| 151 |
// EOF |
| 152 |
return Some(self.len); |
| 153 |
}; |
| 154 |
|
| 155 |
let previous_break_state_is_cp_prop = |
| 156 |
index <= self.data.last_codepoint_property; |
| 157 |
|
| 158 |
match self.data.get_break_state_from_table(index, prop) { |
| 159 |
BreakState::Keep => continue 'a, |
| 160 |
BreakState::NoMatch => { |
| 161 |
self.boundary_property = previous_left_prop; |
| 162 |
self.iter = previous_iter; |
| 163 |
self.current_pos_data = previous_pos_data; |
| 164 |
return self.get_current_position(); |
| 165 |
} |
| 166 |
BreakState::Break => return self.get_current_position(), |
| 167 |
BreakState::Intermediate(i) => { |
| 168 |
index = i; |
| 169 |
if previous_break_state_is_cp_prop { |
| 170 |
// Move marker |
| 171 |
previous_left_prop = index; |
| 172 |
} |
| 173 |
previous_iter = self.iter.clone(); |
| 174 |
previous_pos_data = self.current_pos_data; |
| 175 |
} |
| 176 |
BreakState::Index(i) => { |
| 177 |
index = i; |
| 178 |
if previous_break_state_is_cp_prop { |
| 179 |
// Move marker |
| 180 |
previous_iter = self.iter.clone(); |
| 181 |
previous_pos_data = self.current_pos_data; |
| 182 |
previous_left_prop = index; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
impl<Y: RuleBreakType> RuleBreakIterator<'_, '_, Y> { |
| 194 |
pub(crate) fn advance_iter(&mut self) { |
| 195 |
self.current_pos_data = self.iter.next(); |
| 196 |
} |
| 197 |
|
| 198 |
pub(crate) fn is_eof(&self) -> bool { |
| 199 |
self.current_pos_data.is_none() |
| 200 |
} |
| 201 |
|
| 202 |
pub(crate) fn get_current_break_property(&self) -> Option<u8> { |
| 203 |
self.get_current_codepoint() |
| 204 |
.map(|c| self.get_break_property(c)) |
| 205 |
} |
| 206 |
|
| 207 |
pub(crate) fn get_current_position(&self) -> Option<usize> { |
| 208 |
self.current_pos_data.map(|(pos, _)| pos) |
| 209 |
} |
| 210 |
|
| 211 |
pub(crate) fn get_current_codepoint(&self) -> Option<Y::CharType> { |
| 212 |
self.current_pos_data.map(|(_, codepoint)| codepoint) |
| 213 |
} |
| 214 |
|
| 215 |
fn get_break_property(&self, codepoint: Y::CharType) -> u8 { |
| 216 |
// Note: Default value is 0 == UNKNOWN |
| 217 |
if let Some(locale_override) = &self.locale_override { |
| 218 |
let property = locale_override |
| 219 |
.property_table_override |
| 220 |
.get32(codepoint.into()); |
| 221 |
if property != 0 { |
| 222 |
return property; |
| 223 |
} |
| 224 |
} |
| 225 |
self.data.property_table.get32(codepoint.into()) |
| 226 |
} |
| 227 |
|
| 228 |
/// Return the status value of break boundary. |
| 229 |
pub(crate) fn rule_status(&self) -> u8 { |
| 230 |
if self.boundary_property == 0 { |
| 231 |
// break position is SOT / Any |
| 232 |
return 0; |
| 233 |
} |
| 234 |
self.data |
| 235 |
.rule_status_table |
| 236 |
.get((self.boundary_property - 1) as usize) |
| 237 |
.unwrap_or_default() |
| 238 |
} |
| 239 |
} |