| 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_segmenter::LineBreakOptions; |
| 6 |
use icu_segmenter::LineBreakStrictness; |
| 7 |
use icu_segmenter::LineBreakWordOption; |
| 8 |
use icu_segmenter::LineSegmenter; |
| 9 |
|
| 10 |
fn check_with_options( |
| 11 |
s: &str, |
| 12 |
mut expect_utf8: Vec<usize>, |
| 13 |
mut expect_utf16: Vec<usize>, |
| 14 |
options: LineBreakOptions, |
| 15 |
) { |
| 16 |
let segmenter = LineSegmenter::new_dictionary_with_options(options); |
| 17 |
|
| 18 |
let iter = segmenter.segment_str(s); |
| 19 |
let result: Vec<usize> = iter.collect(); |
| 20 |
expect_utf8.insert(0, 0); |
| 21 |
assert_eq!(expect_utf8, result, "{s}"); |
| 22 |
|
| 23 |
let s_utf16: Vec<u16> = s.encode_utf16().collect(); |
| 24 |
let iter = segmenter.segment_utf16(&s_utf16); |
| 25 |
let result: Vec<usize> = iter.collect(); |
| 26 |
expect_utf16.insert(0, 0); |
| 27 |
assert_eq!(expect_utf16, result, "{s}"); |
| 28 |
} |
| 29 |
|
| 30 |
fn strict(s: &str, ja_zh: bool, expect_utf8: Vec<usize>, expect_utf16: Vec<usize>) { |
| 31 |
let mut options = LineBreakOptions::default(); |
| 32 |
options.strictness = LineBreakStrictness::Strict; |
| 33 |
options.word_option = LineBreakWordOption::Normal; |
| 34 |
options.ja_zh = ja_zh; |
| 35 |
check_with_options(s, expect_utf8, expect_utf16, options); |
| 36 |
} |
| 37 |
|
| 38 |
fn normal(s: &str, ja_zh: bool, expect_utf8: Vec<usize>, expect_utf16: Vec<usize>) { |
| 39 |
let mut options = LineBreakOptions::default(); |
| 40 |
options.strictness = LineBreakStrictness::Normal; |
| 41 |
options.word_option = LineBreakWordOption::Normal; |
| 42 |
options.ja_zh = ja_zh; |
| 43 |
check_with_options(s, expect_utf8, expect_utf16, options); |
| 44 |
} |
| 45 |
|
| 46 |
fn loose(s: &str, ja_zh: bool, expect_utf8: Vec<usize>, expect_utf16: Vec<usize>) { |
| 47 |
let mut options = LineBreakOptions::default(); |
| 48 |
options.strictness = LineBreakStrictness::Loose; |
| 49 |
options.word_option = LineBreakWordOption::Normal; |
| 50 |
options.ja_zh = ja_zh; |
| 51 |
check_with_options(s, expect_utf8, expect_utf16, options); |
| 52 |
} |
| 53 |
|
| 54 |
fn anywhere(s: &str, ja_zh: bool, expect_utf8: Vec<usize>, expect_utf16: Vec<usize>) { |
| 55 |
let mut options = LineBreakOptions::default(); |
| 56 |
options.strictness = LineBreakStrictness::Anywhere; |
| 57 |
options.word_option = LineBreakWordOption::Normal; |
| 58 |
options.ja_zh = ja_zh; |
| 59 |
check_with_options(s, expect_utf8, expect_utf16, options); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn linebreak_strict() { |
| 64 |
// from css/css-text/line-break/line-break-*-011.xht |
| 65 |
strict("サ\u{3041}サ", false, vec![6, 9], vec![2, 3]); |
| 66 |
|
| 67 |
// from css/css-text/line-break/line-break-*-012.xht |
| 68 |
strict("サ\u{30FC}サ", false, vec![6, 9], vec![2, 3]); |
| 69 |
|
| 70 |
// from css/css-text/line-break/line-break-*-013.xht |
| 71 |
strict("サ\u{301C}サ", false, vec![6, 9], vec![2, 3]); |
| 72 |
|
| 73 |
// from css/css-text/line-break/line-break-*-014.xht |
| 74 |
strict("サ\u{3005}サ", false, vec![6, 9], vec![2, 3]); |
| 75 |
|
| 76 |
// from css/css-text/line-break/line-break-*-015a.xht |
| 77 |
// XXX ID x IN in UAX14. But why? |
| 78 |
strict("サ\u{2025}\u{2025}サ", false, vec![9, 12], vec![3, 4]); |
| 79 |
|
| 80 |
// from css/css-text/line-break/line-break-*-016a.xht |
| 81 |
strict("サ\u{30FB}サ", false, vec![6, 9], vec![2, 3]); |
| 82 |
|
| 83 |
// from css/css-text/line-break/line-break-*-017a.xht |
| 84 |
strict("サ\u{00B0}サ", false, vec![5, 8], vec![2, 3]); |
| 85 |
|
| 86 |
// from css/css-text/line-break/line-break-*-018.xht |
| 87 |
//strict("サ\u{20AC}サ", false, vec![9], vec![3]); |
| 88 |
|
| 89 |
// from css/css-text/i18n/ja/css-text-line-break-ja-pr-strict.html |
| 90 |
// TODO: Why ID ÷ ID × PR × ID ÷ ID ? |
| 91 |
//strict("文文\u{00b1}字字", true, vec![3, 11, 14], vec![1, 4, 5]); |
| 92 |
//strict("文文\u{20AC}字字", true, vec![3, 11, 14], vec![1, 4, 5]); |
| 93 |
//strict("文文\u{FF04}字字", true, vec![3, 11, 14], vec![1, 4, 5]); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn linebreak_normal() { |
| 98 |
// from css/css-text/line-break/line-break-*-011.xht |
| 99 |
normal("サ\u{3041}サ", false, vec![3, 6, 9], vec![1, 2, 3]); |
| 100 |
|
| 101 |
// from css/css-text/line-break/line-break-*-012.xht |
| 102 |
normal("サ\u{30FC}サ", false, vec![3, 6, 9], vec![1, 2, 3]); |
| 103 |
|
| 104 |
// from css/css-text/line-break/line-break-*-013.xht |
| 105 |
normal("サ\u{301C}サ", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 106 |
|
| 107 |
// from css/css-text/line-break/line-break-*-014.xht |
| 108 |
normal("サ\u{3005}サ", true, vec![6, 9], vec![2, 3]); |
| 109 |
|
| 110 |
// from css/css-text/line-break/line-break-*-015.xht |
| 111 |
normal("サ\u{2025}\u{2025}サ", true, vec![9, 12], vec![3, 4]); |
| 112 |
|
| 113 |
// from css/css-text/line-break/line-break-*-016a.xht |
| 114 |
normal("サ\u{30FB}サ", true, vec![6, 9], vec![2, 3]); |
| 115 |
|
| 116 |
// from css/css-text/line-break/line-break-*-017a.xht |
| 117 |
normal("サ\u{00B0}サ", true, vec![5, 8], vec![2, 3]); |
| 118 |
|
| 119 |
// from css/css-text/line-break/line-break-*-018.xht |
| 120 |
normal("サ\u{20AC}サ", true, vec![3, 9], vec![1, 3]); |
| 121 |
|
| 122 |
// from css/css-text/i18n/unknown-lang/css-text-line-break-pr-normal.html |
| 123 |
// TODO: Why ID ÷ ID × PR × ID ÷ ID ? |
| 124 |
//normal("文文\u{00b1}字字", false, vec![3, 11, 14], vec![1, 4, 5]); |
| 125 |
//normal("文文\u{20AC}字字", false, vec![3, 11, 14], vec![1, 4, 5]); |
| 126 |
//normal("文文\u{2116}字字", false, vec![3, 11, 14], vec![1, 4, 5]); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn linebreak_loose() { |
| 131 |
// from css/css-text/line-break/line-break-*-011.xht |
| 132 |
loose("サ\u{3041}サ", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 133 |
|
| 134 |
// from css/css-text/line-break/line-break-*-012.xht |
| 135 |
loose("サ\u{30FC}サ", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 136 |
|
| 137 |
// from css/css-text/line-break/line-break-loose-013.xht |
| 138 |
loose("サ\u{301C}サ", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 139 |
|
| 140 |
// from css/css-text/line-break/line-break-*-014.xht |
| 141 |
loose("サ\u{3005}サ", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 142 |
|
| 143 |
// from css/css-text/line-break/line-break-*-015.xht |
| 144 |
loose( |
| 145 |
"サ\u{2025}\u{2025}サ", |
| 146 |
true, |
| 147 |
vec![3, 6, 9, 12], |
| 148 |
vec![1, 2, 3, 4], |
| 149 |
); |
| 150 |
|
| 151 |
// from css/css-text/line-break/line-break-*-016a.xht |
| 152 |
loose("サ\u{30FB}サ", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 153 |
|
| 154 |
// from css/css-text/line-break/line-break-*-017a.xht |
| 155 |
loose("サ\u{00B0}サ", true, vec![3, 5, 8], vec![1, 2, 3]); |
| 156 |
|
| 157 |
// from css/css-text/line-break/line-break-*-018.xht |
| 158 |
loose("文\u{20AC}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 159 |
loose("文\u{2116}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 160 |
loose("文\u{ff04}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 161 |
loose("文\u{ffe1}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 162 |
loose("文\u{ffe5}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 163 |
|
| 164 |
// from css/css-text/i18n/ja/css-text-line-break-ja-pr-loose.html |
| 165 |
loose("文\u{00b1}文", true, vec![3, 5, 8], vec![1, 2, 3]); |
| 166 |
loose("文\u{20ac}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 167 |
loose("文\u{ff04}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 168 |
|
| 169 |
// from css/css-text/i18n/unknown-lang/css-text-line-break-in-loose.html |
| 170 |
loose("文\u{2024}文", false, vec![3, 6, 9], vec![1, 2, 3]); |
| 171 |
loose("文\u{2025}文", false, vec![3, 6, 9], vec![1, 2, 3]); |
| 172 |
loose("文\u{2026}文", false, vec![3, 6, 9], vec![1, 2, 3]); |
| 173 |
loose("文\u{22ef}文", false, vec![3, 6, 9], vec![1, 2, 3]); |
| 174 |
loose("文\u{fe19}文", false, vec![3, 6, 9], vec![1, 2, 3]); |
| 175 |
|
| 176 |
// from css/css-text/i18n/unknown-lang/css-text-line-break-pr-loose.html |
| 177 |
//loose("文\u{00b1}文", false, vec![8], vec![3]); |
| 178 |
//loose("文\u{20ac}文", false, vec![9], vec![3]); |
| 179 |
//loose("文\u{2116}文", false, vec![9], vec![3]); |
| 180 |
//loose("文\u{ff04}文", false, vec![9], vec![3]); |
| 181 |
|
| 182 |
// from css/css-text/i18n/zh/css-text-line-break-zh-in-loose.xht |
| 183 |
loose("文\u{2024}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 184 |
loose("文\u{2025}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 185 |
loose("文\u{2026}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 186 |
loose("文\u{22ef}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 187 |
loose("文\u{fe19}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 188 |
|
| 189 |
// css/css-text/line-break/line-break-loose-hyphens-001.html |
| 190 |
loose("文\u{2010}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 191 |
loose("文\u{2013}文", true, vec![3, 6, 9], vec![1, 2, 3]); |
| 192 |
|
| 193 |
// css/css-text/line-break/line-break-loose-hyphens-003.html |
| 194 |
loose("aa\u{2010}", false, vec![5], vec![3]); |
| 195 |
loose("aa\u{2013}", false, vec![5], vec![3]); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn linebreak_anywhere() { |
| 200 |
// css/css-text/line-break/line-break-anywhere-001.html |
| 201 |
anywhere( |
| 202 |
"aa-a.a)a,a) a\u{00A0}aa\u{2060}a\u{200D}a・a", |
| 203 |
true, |
| 204 |
vec![ |
| 205 |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 20, 21, 24, 25, 28, 29, |
| 206 |
], |
| 207 |
vec![ |
| 208 |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, |
| 209 |
], |
| 210 |
); |
| 211 |
|
| 212 |
// css/css-text/line-break/line-break-anywhere-002.html |
| 213 |
anywhere( |
| 214 |
"no hyphenation", |
| 215 |
false, |
| 216 |
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], |
| 217 |
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], |
| 218 |
); |
| 219 |
|
| 220 |
// css/css-text/line-break/line-break-anywhere-003.html |
| 221 |
anywhere("latin", false, vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]); |
| 222 |
|
| 223 |
// css/css-text/line-break/line-break-anywhere-004.html |
| 224 |
anywhere( |
| 225 |
"XX XXX", |
| 226 |
false, |
| 227 |
vec![1, 2, 3, 4, 5, 6], |
| 228 |
vec![1, 2, 3, 4, 5, 6], |
| 229 |
); |
| 230 |
|
| 231 |
// css/css-text/line-break/line-break-anywhere-005.html |
| 232 |
anywhere("X X", false, vec![1, 2, 3], vec![1, 2, 3]); |
| 233 |
|
| 234 |
// css/css-text/line-break/line-break-anywhere-006.html |
| 235 |
anywhere( |
| 236 |
"XXXX\u{00A0}XXXX", |
| 237 |
false, |
| 238 |
vec![1, 2, 3, 4, 6, 7, 8, 9, 10], |
| 239 |
vec![1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 240 |
); |
| 241 |
|
| 242 |
// css/css-text/line-break/line-break-anywhere-007.html |
| 243 |
anywhere( |
| 244 |
"X XX...", |
| 245 |
true, |
| 246 |
vec![1, 2, 3, 4, 5, 6, 7], |
| 247 |
vec![1, 2, 3, 4, 5, 6, 7], |
| 248 |
); |
| 249 |
|
| 250 |
// css/css-text/line-break/line-break-anywhere-008.html |
| 251 |
anywhere( |
| 252 |
"X XX...", |
| 253 |
true, |
| 254 |
vec![1, 2, 3, 4, 5, 6, 7], |
| 255 |
vec![1, 2, 3, 4, 5, 6, 7], |
| 256 |
); |
| 257 |
|
| 258 |
// css/css-text/line-break/line-break-anywhere-009.html |
| 259 |
anywhere("X\u{00A0}X", true, vec![1, 3, 4], vec![1, 2, 3]); |
| 260 |
|
| 261 |
// css/css-text/line-break/line-break-anywhere-010.html |
| 262 |
anywhere( |
| 263 |
"XXXX\u{00A0}XXXX", |
| 264 |
true, |
| 265 |
vec![1, 2, 3, 4, 6, 7, 8, 9, 10], |
| 266 |
vec![1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 267 |
); |
| 268 |
|
| 269 |
// css/css-text/line-break/line-break-anywhere-011.html |
| 270 |
anywhere("XX///", true, vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]); |
| 271 |
|
| 272 |
// css/css-text/line-break/line-break-anywhere-012.html |
| 273 |
anywhere( |
| 274 |
"X XX\\\\\\", |
| 275 |
true, |
| 276 |
vec![1, 2, 3, 4, 5, 6, 7], |
| 277 |
vec![1, 2, 3, 4, 5, 6, 7], |
| 278 |
); |
| 279 |
|
| 280 |
// css/css-text/line-break/line-break-anywhere-013.html |
| 281 |
anywhere("XXX/X", true, vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]); |
| 282 |
|
| 283 |
// css/css-text/line-break/line-break-anywhere-014.html |
| 284 |
anywhere("XXX\\X", false, vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]); |
| 285 |
|
| 286 |
// css/css-text/line-break/line-break-anywhere-015.html |
| 287 |
anywhere("XXX\\X", false, vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]); |
| 288 |
|
| 289 |
// css/css-text/line-break/line-break-anywhere-016.html |
| 290 |
anywhere("XXX/X", false, vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]); |
| 291 |
|
| 292 |
// css/css-text/line-break/line-break-anywhere-017.html |
| 293 |
anywhere( |
| 294 |
"XXXX X", |
| 295 |
false, |
| 296 |
vec![1, 2, 3, 4, 5, 6], |
| 297 |
vec![1, 2, 3, 4, 5, 6], |
| 298 |
); |
| 299 |
|
| 300 |
// line-break-anywhere-overrides-uax-behavior-001.htm |
| 301 |
anywhere( |
| 302 |
"XX\u{2060}XX", |
| 303 |
false, |
| 304 |
vec![1, 2, 5, 6, 7], |
| 305 |
vec![1, 2, 3, 4, 5], |
| 306 |
); |
| 307 |
|
| 308 |
// line-break-anywhere-overrides-uax-behavior-004.htm |
| 309 |
anywhere( |
| 310 |
"..\u{200B}...X", |
| 311 |
false, |
| 312 |
vec![1, 2, 5, 6, 7, 8, 9], |
| 313 |
vec![1, 2, 3, 4, 5, 6, 7], |
| 314 |
); |
| 315 |
} |