| 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 icu_collections::codepointtrie::CodePointTrie; |
| 6 |
use icu_provider::prelude::*; |
| 7 |
use zerovec::ZeroVec; |
| 8 |
|
| 9 |
icu_provider::data_marker!( |
| 10 |
/// `SegmenterBreakSentenceOverrideV1` |
| 11 |
SegmenterBreakSentenceOverrideV1, |
| 12 |
"segmenter/break/sentence/override/v1", |
| 13 |
RuleBreakDataOverride<'static>, |
| 14 |
); |
| 15 |
|
| 16 |
icu_provider::data_marker!( |
| 17 |
/// `SegmenterBreakWordOverrideV1` |
| 18 |
SegmenterBreakWordOverrideV1, |
| 19 |
"segmenter/break/word/override/v1", |
| 20 |
RuleBreakDataOverride<'static>, |
| 21 |
); |
| 22 |
|
| 23 |
icu_provider::data_marker!( |
| 24 |
/// `SegmenterBreakLineV1` |
| 25 |
SegmenterBreakLineV1, |
| 26 |
"segmenter/break/line/v1", |
| 27 |
RuleBreakData<'static>, |
| 28 |
is_singleton = true |
| 29 |
); |
| 30 |
|
| 31 |
#[cfg(feature = "unstable")] |
| 32 |
icu_provider::data_marker!( |
| 33 |
/// `SegmenterBreakLineV3` |
| 34 |
SegmenterBreakLineV3, |
| 35 |
"segmenter/break/line/v3", |
| 36 |
RuleBreakData<'static>, |
| 37 |
is_singleton = true |
| 38 |
); |
| 39 |
|
| 40 |
icu_provider::data_marker!( |
| 41 |
/// `SegmenterBreakWordV1` |
| 42 |
SegmenterBreakWordV1, |
| 43 |
"segmenter/break/word/v1", |
| 44 |
RuleBreakData<'static>, |
| 45 |
is_singleton = true |
| 46 |
); |
| 47 |
|
| 48 |
icu_provider::data_marker!( |
| 49 |
/// `SegmenterBreakGraphemeClusterV1` |
| 50 |
SegmenterBreakGraphemeClusterV1, |
| 51 |
"segmenter/break/grapheme/cluster/v1", |
| 52 |
RuleBreakData<'static>, |
| 53 |
is_singleton = true |
| 54 |
); |
| 55 |
|
| 56 |
icu_provider::data_marker!( |
| 57 |
/// `SegmenterBreakSentenceV1` |
| 58 |
SegmenterBreakSentenceV1, |
| 59 |
"segmenter/break/sentence/v1", |
| 60 |
RuleBreakData<'static>, |
| 61 |
is_singleton = true |
| 62 |
); |
| 63 |
|
| 64 |
/// Pre-processed Unicode data in the form of tables to be used for rule-based breaking. |
| 65 |
/// |
| 66 |
/// <div class="stab unstable"> |
| 67 |
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 68 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 69 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 70 |
/// </div> |
| 71 |
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 72 |
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))] |
| 73 |
#[cfg_attr(feature = "datagen", databake(path = icu_segmenter::provider))] |
| 74 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 75 |
pub struct RuleBreakData<'data> { |
| 76 |
/// Property table. |
| 77 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 78 |
pub property_table: CodePointTrie<'data, u8>, |
| 79 |
|
| 80 |
/// Break state table. |
| 81 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 82 |
pub break_state_table: ZeroVec<'data, BreakState>, |
| 83 |
|
| 84 |
/// State status table. Only used for word segmenter. |
| 85 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 86 |
pub rule_status_table: ZeroVec<'data, u8>, |
| 87 |
|
| 88 |
/// Number of properties; should be the square root of the length of [`Self::break_state_table`]. |
| 89 |
pub property_count: u8, |
| 90 |
|
| 91 |
/// The index of the last simple state for [`Self::break_state_table`]. (A simple state has no |
| 92 |
/// `left` nor `right` in `SegmenterProperty`). |
| 93 |
pub last_codepoint_property: u8, |
| 94 |
|
| 95 |
/// The index of SOT (start of text) state for [`Self::break_state_table`]. |
| 96 |
pub sot_property: u8, |
| 97 |
|
| 98 |
/// The index of EOT (end of text) state [`Self::break_state_table`]. |
| 99 |
pub eot_property: u8, |
| 100 |
|
| 101 |
/// The index of "SA" state (or 127 if complex scripts aren't handled) for |
| 102 |
/// [`Self::break_state_table`]. |
| 103 |
pub complex_property: u8, |
| 104 |
} |
| 105 |
|
| 106 |
impl RuleBreakData<'_> { |
| 107 |
#[inline] |
| 108 |
pub(crate) fn get_break_state_from_table(&self, left: u8, right: u8) -> BreakState { |
| 109 |
let idx = (left as usize) * (self.property_count as usize) + (right as usize); |
| 110 |
// We use unwrap_or to fall back to the base case and prevent panics on bad data. |
| 111 |
self.break_state_table.get(idx).unwrap_or(BreakState::Keep) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
icu_provider::data_struct!( |
| 116 |
RuleBreakData<'_>, |
| 117 |
#[cfg(feature = "datagen")] |
| 118 |
); |
| 119 |
|
| 120 |
/// codepoint trie data that the difference by specific locale |
| 121 |
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 122 |
#[cfg_attr( |
| 123 |
feature = "datagen", |
| 124 |
derive(serde::Serialize,databake::Bake), |
| 125 |
databake(path = icu_segmenter::provider), |
| 126 |
)] |
| 127 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 128 |
pub struct RuleBreakDataOverride<'data> { |
| 129 |
/// The difference of property table for special locale. |
| 130 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 131 |
pub property_table_override: CodePointTrie<'data, u8>, |
| 132 |
} |
| 133 |
|
| 134 |
icu_provider::data_struct!( |
| 135 |
RuleBreakDataOverride<'_>, |
| 136 |
#[cfg(feature = "datagen")] |
| 137 |
); |
| 138 |
|
| 139 |
#[derive(Clone, Copy, PartialEq, Debug)] |
| 140 |
#[cfg_attr(feature = "datagen", derive(databake::Bake))] |
| 141 |
#[cfg_attr(feature = "datagen", databake(path = icu_segmenter::provider))] |
| 142 |
/// Break state |
| 143 |
/// |
| 144 |
/// <div class="stab unstable"> |
| 145 |
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 146 |
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only |
| 147 |
/// guaranteed to match with this version's `*_unstable` providers. Use with caution. |
| 148 |
/// </div> |
| 149 |
pub enum BreakState { |
| 150 |
/// Break |
| 151 |
Break, |
| 152 |
/// Keep rule |
| 153 |
Keep, |
| 154 |
/// Non-matching rule |
| 155 |
NoMatch, |
| 156 |
/// We have to look ahead one more character. |
| 157 |
Intermediate(u8), |
| 158 |
/// Index of a state. |
| 159 |
Index(u8), |
| 160 |
} |
| 161 |
|
| 162 |
#[cfg(feature = "datagen")] |
| 163 |
impl serde::Serialize for BreakState { |
| 164 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 165 |
where |
| 166 |
S: serde::Serializer, |
| 167 |
{ |
| 168 |
// would be nice to use the derive serde for JSON, but can't break serialization |
| 169 |
if serializer.is_human_readable() { |
| 170 |
i8::from_le_bytes([zerovec::ule::AsULE::to_unaligned(*self)]).serialize(serializer) |
| 171 |
} else { |
| 172 |
zerovec::ule::AsULE::to_unaligned(*self).serialize(serializer) |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[cfg(feature = "serde")] |
| 178 |
impl<'de> serde::Deserialize<'de> for BreakState { |
| 179 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 180 |
where |
| 181 |
D: serde::Deserializer<'de>, |
| 182 |
{ |
| 183 |
if deserializer.is_human_readable() { |
| 184 |
Ok(zerovec::ule::AsULE::from_unaligned( |
| 185 |
i8::deserialize(deserializer)?.to_le_bytes()[0], |
| 186 |
)) |
| 187 |
} else { |
| 188 |
u8::deserialize(deserializer).map(zerovec::ule::AsULE::from_unaligned) |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
impl zerovec::ule::AsULE for BreakState { |
| 194 |
type ULE = u8; |
| 195 |
|
| 196 |
fn to_unaligned(self) -> Self::ULE { |
| 197 |
match self { |
| 198 |
BreakState::Break => 253, |
| 199 |
BreakState::Keep => 255, |
| 200 |
BreakState::NoMatch => 254, |
| 201 |
BreakState::Intermediate(i) => i + 120, |
| 202 |
BreakState::Index(i) => i, |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
fn from_unaligned(unaligned: Self::ULE) -> Self { |
| 207 |
match unaligned { |
| 208 |
253 => BreakState::Break, |
| 209 |
255 => BreakState::Keep, |
| 210 |
254 => BreakState::NoMatch, |
| 211 |
i if (120..253).contains(&i) => BreakState::Intermediate(i - 120), |
| 212 |
i => BreakState::Index(i), |
| 213 |
} |
| 214 |
} |
| 215 |
} |