| 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 |
#[cfg(feature = "unstable")] |
| 6 |
use crate::GraphemeClusterSegmenterBorrowed; |
| 7 |
#[cfg(feature = "unstable")] |
| 8 |
use crate::complex::{ComplexIterator, ComplexPayloadBorrowed, ComplexPayloadsBorrowed}; |
| 9 |
#[cfg(feature = "unstable")] |
| 10 |
use crate::provider::ComplexScript; |
| 11 |
|
| 12 |
use crate::indices::*; |
| 13 |
|
| 14 |
/// A trait allowing for `RuleBreakIterator` to be generalized to multiple string |
| 15 |
/// encoding methods and granularity such as grapheme cluster, word, etc. |
| 16 |
/// |
| 17 |
/// <div class="stab unstable"> |
| 18 |
/// 🚫 This trait is sealed; it cannot be implemented by user code. If an API requests an item that implements this |
| 19 |
/// trait, please consider using a type from the implementors listed below. |
| 20 |
/// </div> |
| 21 |
pub trait RuleBreakType: crate::private::Sealed + Sized { |
| 22 |
/// The iterator over characters. |
| 23 |
type IterAttr<'s>: Iterator<Item = (usize, Self::CharType)> + Clone + core::fmt::Debug; |
| 24 |
|
| 25 |
/// The character type. |
| 26 |
type CharType: Copy + Into<u32> + core::fmt::Debug; |
| 27 |
|
| 28 |
#[doc(hidden)] |
| 29 |
const CAN_CONTAIN_SA: bool; |
| 30 |
|
| 31 |
#[doc(hidden)] |
| 32 |
fn char_len(ch: Self::CharType) -> usize; |
| 33 |
|
| 34 |
#[doc(hidden)] |
| 35 |
#[cfg(feature = "unstable")] |
| 36 |
type ComplexPayloads<'data>: core::fmt::Debug + Clone + Copy; |
| 37 |
#[doc(hidden)] |
| 38 |
#[cfg(feature = "unstable")] |
| 39 |
type ComplexPayload<'data>: core::fmt::Debug; |
| 40 |
|
| 41 |
#[doc(hidden)] |
| 42 |
#[cfg(feature = "unstable")] |
| 43 |
fn select_complex<'a>( |
| 44 |
data: &Self::ComplexPayloads<'a>, |
| 45 |
complex_script: ComplexScript, |
| 46 |
) -> Option<Self::ComplexPayload<'a>>; |
| 47 |
|
| 48 |
#[doc(hidden)] |
| 49 |
#[cfg(feature = "unstable")] |
| 50 |
fn handle_complex<'data, 's>( |
| 51 |
data: &Self::ComplexPayload<'data>, |
| 52 |
complex: &Self::IterAttr<'s>, |
| 53 |
past_complex: &Self::IterAttr<'s>, |
| 54 |
) -> ComplexIterator<'data, 's, Self>; |
| 55 |
|
| 56 |
#[doc(hidden)] |
| 57 |
#[cfg(feature = "unstable")] |
| 58 |
fn offset<'s>(iter: &Self::IterAttr<'s>) -> usize; |
| 59 |
|
| 60 |
#[doc(hidden)] |
| 61 |
#[cfg(feature = "unstable")] |
| 62 |
fn is_empty<'s>(iter: &Self::IterAttr<'s>) -> bool; |
| 63 |
} |
| 64 |
|
| 65 |
#[derive(Debug)] |
| 66 |
#[non_exhaustive] |
| 67 |
/// [`RuleBreakType`] for UTF-8 strings |
| 68 |
pub struct Utf8; |
| 69 |
|
| 70 |
impl crate::private::Sealed for Utf8 {} |
| 71 |
|
| 72 |
impl RuleBreakType for Utf8 { |
| 73 |
type IterAttr<'s> = CharIndices<'s>; |
| 74 |
type CharType = char; |
| 75 |
|
| 76 |
const CAN_CONTAIN_SA: bool = true; |
| 77 |
|
| 78 |
#[cfg(feature = "unstable")] |
| 79 |
type ComplexPayloads<'data> = ComplexPayloadsBorrowed<'data>; |
| 80 |
#[cfg(feature = "unstable")] |
| 81 |
type ComplexPayload<'data> = ( |
| 82 |
ComplexPayloadBorrowed<'data>, |
| 83 |
GraphemeClusterSegmenterBorrowed<'data>, |
| 84 |
); |
| 85 |
|
| 86 |
#[cfg(feature = "unstable")] |
| 87 |
fn select_complex<'a>( |
| 88 |
&data: &Self::ComplexPayloads<'a>, |
| 89 |
complex_script: ComplexScript, |
| 90 |
) -> Option<Self::ComplexPayload<'a>> { |
| 91 |
data.select(complex_script).map(|d| (d, data.grapheme)) |
| 92 |
} |
| 93 |
|
| 94 |
#[cfg(feature = "unstable")] |
| 95 |
fn handle_complex<'data, 's>( |
| 96 |
&(lang, grapheme): &Self::ComplexPayload<'data>, |
| 97 |
complex: &Self::IterAttr<'s>, |
| 98 |
past_complex: &Self::IterAttr<'s>, |
| 99 |
) -> ComplexIterator<'data, 's, Self> { |
| 100 |
let complex_offset = complex.offset(); |
| 101 |
#[allow(clippy::indexing_slicing)] // valid offset |
| 102 |
let complex = &complex.as_str()[..(past_complex.offset() - complex_offset)]; |
| 103 |
lang.segment_str(complex, grapheme, complex_offset) |
| 104 |
} |
| 105 |
|
| 106 |
fn char_len(ch: Self::CharType) -> usize { |
| 107 |
ch.len_utf8() |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg(feature = "unstable")] |
| 111 |
fn offset<'s>(iter: &Self::IterAttr<'s>) -> usize { |
| 112 |
iter.offset() |
| 113 |
} |
| 114 |
|
| 115 |
#[cfg(feature = "unstable")] |
| 116 |
fn is_empty<'s>(iter: &Self::IterAttr<'s>) -> bool { |
| 117 |
iter.as_str().is_empty() |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
#[derive(Debug)] |
| 122 |
#[non_exhaustive] |
| 123 |
/// [`RuleBreakType`] for potentially ill-formed UTF-8 strings |
| 124 |
pub struct PotentiallyIllFormedUtf8; |
| 125 |
|
| 126 |
impl crate::private::Sealed for PotentiallyIllFormedUtf8 {} |
| 127 |
|
| 128 |
impl RuleBreakType for PotentiallyIllFormedUtf8 { |
| 129 |
type IterAttr<'s> = Utf8CharIndices<'s>; |
| 130 |
type CharType = char; |
| 131 |
|
| 132 |
const CAN_CONTAIN_SA: bool = true; |
| 133 |
|
| 134 |
#[cfg(feature = "unstable")] |
| 135 |
type ComplexPayloads<'data> = ComplexPayloadsBorrowed<'data>; |
| 136 |
#[cfg(feature = "unstable")] |
| 137 |
type ComplexPayload<'data> = ( |
| 138 |
ComplexPayloadBorrowed<'data>, |
| 139 |
GraphemeClusterSegmenterBorrowed<'data>, |
| 140 |
); |
| 141 |
|
| 142 |
#[cfg(feature = "unstable")] |
| 143 |
fn select_complex<'a>( |
| 144 |
data: &Self::ComplexPayloads<'a>, |
| 145 |
complex_script: ComplexScript, |
| 146 |
) -> Option<Self::ComplexPayload<'a>> { |
| 147 |
Utf8::select_complex(data, complex_script) |
| 148 |
} |
| 149 |
|
| 150 |
#[cfg(feature = "unstable")] |
| 151 |
fn handle_complex<'data, 's>( |
| 152 |
&(lang, grapheme): &Self::ComplexPayload<'data>, |
| 153 |
complex: &Self::IterAttr<'s>, |
| 154 |
past_complex: &Self::IterAttr<'s>, |
| 155 |
) -> ComplexIterator<'data, 's, Self> { |
| 156 |
let offset = complex.offset(); |
| 157 |
#[allow(clippy::indexing_slicing)] // valid offset |
| 158 |
let complex = &complex.as_slice()[..(past_complex.offset() - offset)]; |
| 159 |
lang.segment_utf8(complex, grapheme, offset) |
| 160 |
} |
| 161 |
|
| 162 |
fn char_len(ch: Self::CharType) -> usize { |
| 163 |
ch.len_utf8() |
| 164 |
} |
| 165 |
|
| 166 |
#[cfg(feature = "unstable")] |
| 167 |
fn offset<'s>(iter: &Self::IterAttr<'s>) -> usize { |
| 168 |
iter.offset() |
| 169 |
} |
| 170 |
|
| 171 |
#[cfg(feature = "unstable")] |
| 172 |
fn is_empty<'s>(iter: &Self::IterAttr<'s>) -> bool { |
| 173 |
iter.as_slice().is_empty() |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[derive(Debug)] |
| 178 |
#[non_exhaustive] |
| 179 |
/// [`RuleBreakType`] for Latin-1 strings |
| 180 |
pub struct Latin1; |
| 181 |
|
| 182 |
impl crate::private::Sealed for Latin1 {} |
| 183 |
|
| 184 |
impl RuleBreakType for Latin1 { |
| 185 |
type IterAttr<'s> = Latin1Indices<'s>; |
| 186 |
type CharType = u8; |
| 187 |
|
| 188 |
const CAN_CONTAIN_SA: bool = false; |
| 189 |
|
| 190 |
#[cfg(feature = "unstable")] |
| 191 |
type ComplexPayloads<'data> = core::convert::Infallible; |
| 192 |
#[cfg(feature = "unstable")] |
| 193 |
type ComplexPayload<'data> = core::convert::Infallible; |
| 194 |
|
| 195 |
#[cfg(feature = "unstable")] |
| 196 |
fn select_complex<'a>( |
| 197 |
&complex_payloads: &Self::ComplexPayloads<'a>, |
| 198 |
_: ComplexScript, |
| 199 |
) -> Option<Self::ComplexPayload<'a>> { |
| 200 |
match complex_payloads {} |
| 201 |
} |
| 202 |
|
| 203 |
#[cfg(feature = "unstable")] |
| 204 |
fn handle_complex<'data, 's>( |
| 205 |
&complex_payload: &Self::ComplexPayloads<'data>, |
| 206 |
_: &Self::IterAttr<'s>, |
| 207 |
_: &Self::IterAttr<'s>, |
| 208 |
) -> ComplexIterator<'data, 's, Self> { |
| 209 |
match complex_payload {} |
| 210 |
} |
| 211 |
|
| 212 |
fn char_len(_ch: Self::CharType) -> usize { |
| 213 |
unreachable!() |
| 214 |
} |
| 215 |
|
| 216 |
#[cfg(feature = "unstable")] |
| 217 |
fn offset<'s>(iter: &Self::IterAttr<'s>) -> usize { |
| 218 |
iter.offset() |
| 219 |
} |
| 220 |
|
| 221 |
#[cfg(feature = "unstable")] |
| 222 |
fn is_empty<'s>(iter: &Self::IterAttr<'s>) -> bool { |
| 223 |
iter.as_slice().is_empty() |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
#[derive(Debug)] |
| 228 |
#[non_exhaustive] |
| 229 |
/// [`RuleBreakType`] for UTF-16 strings |
| 230 |
pub struct Utf16; |
| 231 |
|
| 232 |
impl crate::private::Sealed for Utf16 {} |
| 233 |
|
| 234 |
impl RuleBreakType for Utf16 { |
| 235 |
type IterAttr<'s> = Utf16Indices<'s>; |
| 236 |
type CharType = u32; |
| 237 |
|
| 238 |
const CAN_CONTAIN_SA: bool = true; |
| 239 |
|
| 240 |
#[cfg(feature = "unstable")] |
| 241 |
type ComplexPayloads<'data> = ComplexPayloadsBorrowed<'data>; |
| 242 |
#[cfg(feature = "unstable")] |
| 243 |
type ComplexPayload<'data> = ( |
| 244 |
ComplexPayloadBorrowed<'data>, |
| 245 |
GraphemeClusterSegmenterBorrowed<'data>, |
| 246 |
); |
| 247 |
|
| 248 |
#[cfg(feature = "unstable")] |
| 249 |
fn select_complex<'a>( |
| 250 |
data: &Self::ComplexPayloads<'a>, |
| 251 |
complex_script: ComplexScript, |
| 252 |
) -> Option<Self::ComplexPayload<'a>> { |
| 253 |
Utf8::select_complex(data, complex_script) |
| 254 |
} |
| 255 |
|
| 256 |
#[cfg(feature = "unstable")] |
| 257 |
fn handle_complex<'data, 's>( |
| 258 |
&(lang, grapheme): &Self::ComplexPayload<'data>, |
| 259 |
complex: &Self::IterAttr<'s>, |
| 260 |
past_complex: &Self::IterAttr<'s>, |
| 261 |
) -> ComplexIterator<'data, 's, Utf16> { |
| 262 |
let complex_offset = complex.offset(); |
| 263 |
#[allow(clippy::indexing_slicing)] // valid offset |
| 264 |
let complex = &complex.as_slice()[..(past_complex.offset() - complex_offset)]; |
| 265 |
lang.segment_utf16(complex, grapheme, complex_offset) |
| 266 |
} |
| 267 |
|
| 268 |
fn char_len(ch: Self::CharType) -> usize { |
| 269 |
if ch >= 0x10000 { 2 } else { 1 } |
| 270 |
} |
| 271 |
|
| 272 |
#[cfg(feature = "unstable")] |
| 273 |
fn offset<'s>(iter: &Self::IterAttr<'s>) -> usize { |
| 274 |
iter.offset() |
| 275 |
} |
| 276 |
|
| 277 |
#[cfg(feature = "unstable")] |
| 278 |
fn is_empty<'s>(iter: &Self::IterAttr<'s>) -> bool { |
| 279 |
iter.as_slice().is_empty() |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
/// A trait allowing for [`WordBreakIterator`](crate::iterators::WordBreakIterator) to be generalized to multiple |
| 284 |
/// string iteration methods. |
| 285 |
/// |
| 286 |
/// This is implemented by ICU4X for several common string types. |
| 287 |
/// |
| 288 |
/// <div class="stab unstable"> |
| 289 |
/// 🚫 This trait is sealed; it cannot be implemented by user code. If an API requests an item that implements this |
| 290 |
/// trait, please consider using a type from the implementors listed below. |
| 291 |
/// </div> |
| 292 |
pub trait WordBreakType: crate::private::Sealed + Sized + RuleBreakType {} |
| 293 |
impl WordBreakType for Utf8 {} |
| 294 |
impl WordBreakType for PotentiallyIllFormedUtf8 {} |
| 295 |
impl WordBreakType for Latin1 {} |
| 296 |
impl WordBreakType for Utf16 {} |
| 297 |
|
| 298 |
/// A trait allowing for `LineBreakIterator` to be generalized to multiple string iteration methods. |
| 299 |
/// |
| 300 |
/// This is implemented by ICU4X for several common string types. |
| 301 |
/// |
| 302 |
/// <div class="stab unstable"> |
| 303 |
/// 🚫 This trait is sealed; it cannot be implemented by user code. If an API requests an item that implements this |
| 304 |
/// trait, please consider using a type from the implementors listed below. |
| 305 |
/// </div> |
| 306 |
pub trait LineBreakType: crate::private::Sealed + Sized + RuleBreakType {} |
| 307 |
impl LineBreakType for Utf8 {} |
| 308 |
impl LineBreakType for PotentiallyIllFormedUtf8 {} |
| 309 |
impl LineBreakType for Latin1 {} |
| 310 |
impl LineBreakType for Utf16 {} |