CharacterTypes.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/Platform.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Types.h>
  10. #include <AK/Utf16View.h>
  11. #include <AK/Utf8View.h>
  12. #include <LibUnicode/CharacterTypes.h>
  13. #if ENABLE_UNICODE_DATA
  14. # include <LibUnicode/UnicodeData.h>
  15. #endif
  16. // For details on the algorithms used here, see Section 3.13 Default Case Algorithms
  17. // https://www.unicode.org/versions/Unicode13.0.0/ch03.pdf
  18. namespace Unicode {
  19. Optional<String> __attribute__((weak)) code_point_display_name(u32) { return {}; }
  20. Optional<StringView> __attribute__((weak)) code_point_block_display_name(u32) { return {}; }
  21. Optional<StringView> __attribute__((weak)) code_point_abbreviation(u32) { return {}; }
  22. u32 __attribute__((weak)) canonical_combining_class(u32) { return {}; }
  23. Span<SpecialCasing const* const> __attribute__((weak)) special_case_mapping(u32) { return {}; }
  24. Span<BlockName const> __attribute__((weak)) block_display_names() { return {}; }
  25. #if ENABLE_UNICODE_DATA
  26. static bool is_after_uppercase_i(Utf8View const& string, size_t index)
  27. {
  28. // There is an uppercase I before C, and there is no intervening combining character class 230 (Above) or 0.
  29. auto preceding_view = string.substring_view(0, index);
  30. bool found_uppercase_i = false;
  31. // FIXME: Would be better if Utf8View supported reverse iteration.
  32. for (auto code_point : preceding_view) {
  33. if (code_point == 'I') {
  34. found_uppercase_i = true;
  35. continue;
  36. }
  37. u32 combining_class = canonical_combining_class(code_point);
  38. if (combining_class == 0)
  39. found_uppercase_i = false;
  40. else if (combining_class == 230)
  41. found_uppercase_i = false;
  42. }
  43. return found_uppercase_i;
  44. }
  45. static bool is_after_soft_dotted_code_point(Utf8View const& string, size_t index)
  46. {
  47. // There is a Soft_Dotted character before C, with no intervening character of combining class 0 or 230 (Above).
  48. auto preceding_view = string.substring_view(0, index);
  49. bool found_soft_dotted_code_point = false;
  50. // FIXME: Would be better if Utf8View supported reverse iteration.
  51. for (auto code_point : preceding_view) {
  52. if (code_point_has_property(code_point, Property::Soft_Dotted)) {
  53. found_soft_dotted_code_point = true;
  54. continue;
  55. }
  56. u32 combining_class = canonical_combining_class(code_point);
  57. if (combining_class == 0)
  58. found_soft_dotted_code_point = false;
  59. else if (combining_class == 230)
  60. found_soft_dotted_code_point = false;
  61. }
  62. return found_soft_dotted_code_point;
  63. }
  64. static bool is_final_code_point(Utf8View const& string, size_t index, size_t byte_length)
  65. {
  66. // C is preceded by a sequence consisting of a cased letter and then zero or more case-ignorable
  67. // characters, and C is not followed by a sequence consisting of zero or more case-ignorable
  68. // characters and then a cased letter.
  69. auto preceding_view = string.substring_view(0, index);
  70. auto following_view = ((index + byte_length) < string.byte_length())
  71. ? string.substring_view(index + byte_length)
  72. : Utf8View {};
  73. size_t cased_letter_count = 0;
  74. for (auto code_point : preceding_view) {
  75. bool is_cased = code_point_has_property(code_point, Property::Cased);
  76. bool is_case_ignorable = code_point_has_property(code_point, Property::Case_Ignorable);
  77. if (is_cased && !is_case_ignorable)
  78. ++cased_letter_count;
  79. else if (!is_case_ignorable)
  80. cased_letter_count = 0;
  81. }
  82. if (cased_letter_count == 0)
  83. return false;
  84. for (auto code_point : following_view) {
  85. bool is_cased = code_point_has_property(code_point, Property::Cased);
  86. bool is_case_ignorable = code_point_has_property(code_point, Property::Case_Ignorable);
  87. if (is_case_ignorable)
  88. continue;
  89. if (is_cased)
  90. return false;
  91. break;
  92. }
  93. return true;
  94. }
  95. static bool is_followed_by_combining_class_above(Utf8View const& string, size_t index, size_t byte_length)
  96. {
  97. // C is followed by a character of combining class 230 (Above) with no intervening character of combining class 0 or 230 (Above).
  98. auto following_view = ((index + byte_length) < string.byte_length())
  99. ? string.substring_view(index + byte_length)
  100. : Utf8View {};
  101. for (auto code_point : following_view) {
  102. u32 combining_class = canonical_combining_class(code_point);
  103. if (combining_class == 0)
  104. return false;
  105. if (combining_class == 230)
  106. return true;
  107. }
  108. return false;
  109. }
  110. static bool is_followed_by_combining_dot_above(Utf8View const& string, size_t index, size_t byte_length)
  111. {
  112. // C is followed by combining dot above (U+0307). Any sequence of characters with a combining class that is neither 0 nor 230 may
  113. // intervene between the current character and the combining dot above.
  114. auto following_view = ((index + byte_length) < string.byte_length())
  115. ? string.substring_view(index + byte_length)
  116. : Utf8View {};
  117. for (auto code_point : following_view) {
  118. if (code_point == 0x307)
  119. return true;
  120. u32 combining_class = canonical_combining_class(code_point);
  121. if (combining_class == 0)
  122. return false;
  123. if (combining_class == 230)
  124. return false;
  125. }
  126. return false;
  127. }
  128. static SpecialCasing const* find_matching_special_case(u32 code_point, Utf8View const& string, Optional<StringView> locale, size_t index, size_t byte_length)
  129. {
  130. auto requested_locale = Locale::None;
  131. if (locale.has_value()) {
  132. if (auto maybe_locale = locale_from_string(*locale); maybe_locale.has_value())
  133. requested_locale = *maybe_locale;
  134. }
  135. auto special_casings = special_case_mapping(code_point);
  136. for (auto const* special_casing : special_casings) {
  137. if (special_casing->locale != Locale::None && special_casing->locale != requested_locale)
  138. continue;
  139. switch (special_casing->condition) {
  140. case Condition::None:
  141. return special_casing;
  142. case Condition::AfterI:
  143. if (is_after_uppercase_i(string, index))
  144. return special_casing;
  145. break;
  146. case Condition::AfterSoftDotted:
  147. if (is_after_soft_dotted_code_point(string, index))
  148. return special_casing;
  149. break;
  150. case Condition::FinalSigma:
  151. if (is_final_code_point(string, index, byte_length))
  152. return special_casing;
  153. break;
  154. case Condition::MoreAbove:
  155. if (is_followed_by_combining_class_above(string, index, byte_length))
  156. return special_casing;
  157. break;
  158. case Condition::NotBeforeDot:
  159. if (!is_followed_by_combining_dot_above(string, index, byte_length))
  160. return special_casing;
  161. break;
  162. }
  163. }
  164. return nullptr;
  165. }
  166. #endif
  167. u32 __attribute__((weak)) to_unicode_lowercase(u32 code_point)
  168. {
  169. return to_ascii_lowercase(code_point);
  170. }
  171. u32 __attribute__((weak)) to_unicode_uppercase(u32 code_point)
  172. {
  173. return to_ascii_uppercase(code_point);
  174. }
  175. String to_unicode_lowercase_full(StringView string, [[maybe_unused]] Optional<StringView> locale)
  176. {
  177. #if ENABLE_UNICODE_DATA
  178. Utf8View view { string };
  179. StringBuilder builder;
  180. size_t index = 0;
  181. size_t byte_length = 0;
  182. for (auto it = view.begin(); it != view.end(); ++it, index += byte_length) {
  183. u32 code_point = *it;
  184. byte_length = it.underlying_code_point_length_in_bytes();
  185. auto const* special_casing = find_matching_special_case(code_point, view, locale, index, byte_length);
  186. if (!special_casing) {
  187. builder.append_code_point(to_unicode_lowercase(code_point));
  188. continue;
  189. }
  190. for (size_t i = 0; i < special_casing->lowercase_mapping_size; ++i)
  191. builder.append_code_point(special_casing->lowercase_mapping[i]);
  192. }
  193. return builder.build();
  194. #else
  195. return string.to_lowercase_string();
  196. #endif
  197. }
  198. String to_unicode_uppercase_full(StringView string, [[maybe_unused]] Optional<StringView> locale)
  199. {
  200. #if ENABLE_UNICODE_DATA
  201. Utf8View view { string };
  202. StringBuilder builder;
  203. size_t index = 0;
  204. size_t byte_length = 0;
  205. for (auto it = view.begin(); it != view.end(); ++it, index += byte_length) {
  206. u32 code_point = *it;
  207. byte_length = it.underlying_code_point_length_in_bytes();
  208. auto const* special_casing = find_matching_special_case(code_point, view, locale, index, byte_length);
  209. if (!special_casing) {
  210. builder.append_code_point(to_unicode_uppercase(code_point));
  211. continue;
  212. }
  213. for (size_t i = 0; i < special_casing->uppercase_mapping_size; ++i)
  214. builder.append_code_point(special_casing->uppercase_mapping[i]);
  215. }
  216. return builder.build();
  217. #else
  218. return string.to_uppercase_string();
  219. #endif
  220. }
  221. Optional<GeneralCategory> __attribute__((weak)) general_category_from_string(StringView) { return {}; }
  222. bool __attribute__((weak)) code_point_has_general_category(u32, GeneralCategory) { return {}; }
  223. Optional<Property> __attribute__((weak)) property_from_string(StringView) { return {}; }
  224. bool __attribute__((weak)) code_point_has_property(u32, Property) { return {}; }
  225. bool is_ecma262_property([[maybe_unused]] Property property)
  226. {
  227. #if ENABLE_UNICODE_DATA
  228. // EMCA-262 only allows a subset of Unicode properties: https://tc39.es/ecma262/#table-binary-unicode-properties
  229. switch (property) {
  230. case Unicode::Property::ASCII:
  231. case Unicode::Property::ASCII_Hex_Digit:
  232. case Unicode::Property::Alphabetic:
  233. case Unicode::Property::Any:
  234. case Unicode::Property::Assigned:
  235. case Unicode::Property::Bidi_Control:
  236. case Unicode::Property::Bidi_Mirrored:
  237. case Unicode::Property::Case_Ignorable:
  238. case Unicode::Property::Cased:
  239. case Unicode::Property::Changes_When_Casefolded:
  240. case Unicode::Property::Changes_When_Casemapped:
  241. case Unicode::Property::Changes_When_Lowercased:
  242. case Unicode::Property::Changes_When_NFKC_Casefolded:
  243. case Unicode::Property::Changes_When_Titlecased:
  244. case Unicode::Property::Changes_When_Uppercased:
  245. case Unicode::Property::Dash:
  246. case Unicode::Property::Default_Ignorable_Code_Point:
  247. case Unicode::Property::Deprecated:
  248. case Unicode::Property::Diacritic:
  249. case Unicode::Property::Emoji:
  250. case Unicode::Property::Emoji_Component:
  251. case Unicode::Property::Emoji_Modifier:
  252. case Unicode::Property::Emoji_Modifier_Base:
  253. case Unicode::Property::Emoji_Presentation:
  254. case Unicode::Property::Extended_Pictographic:
  255. case Unicode::Property::Extender:
  256. case Unicode::Property::Grapheme_Base:
  257. case Unicode::Property::Grapheme_Extend:
  258. case Unicode::Property::Hex_Digit:
  259. case Unicode::Property::IDS_Binary_Operator:
  260. case Unicode::Property::IDS_Trinary_Operator:
  261. case Unicode::Property::ID_Continue:
  262. case Unicode::Property::ID_Start:
  263. case Unicode::Property::Ideographic:
  264. case Unicode::Property::Join_Control:
  265. case Unicode::Property::Logical_Order_Exception:
  266. case Unicode::Property::Lowercase:
  267. case Unicode::Property::Math:
  268. case Unicode::Property::Noncharacter_Code_Point:
  269. case Unicode::Property::Pattern_Syntax:
  270. case Unicode::Property::Pattern_White_Space:
  271. case Unicode::Property::Quotation_Mark:
  272. case Unicode::Property::Radical:
  273. case Unicode::Property::Regional_Indicator:
  274. case Unicode::Property::Sentence_Terminal:
  275. case Unicode::Property::Soft_Dotted:
  276. case Unicode::Property::Terminal_Punctuation:
  277. case Unicode::Property::Unified_Ideograph:
  278. case Unicode::Property::Uppercase:
  279. case Unicode::Property::Variation_Selector:
  280. case Unicode::Property::White_Space:
  281. case Unicode::Property::XID_Continue:
  282. case Unicode::Property::XID_Start:
  283. return true;
  284. default:
  285. return false;
  286. }
  287. #else
  288. return false;
  289. #endif
  290. }
  291. Optional<Script> __attribute__((weak)) script_from_string(StringView) { return {}; }
  292. bool __attribute__((weak)) code_point_has_script(u32, Script) { return {}; }
  293. bool __attribute__((weak)) code_point_has_script_extension(u32, Script) { return {}; }
  294. Optional<Block> __attribute__((weak)) block_from_string(StringView) { return {}; }
  295. bool __attribute__((weak)) code_point_has_block(u32, Block) { return {}; }
  296. bool __attribute__((weak)) code_point_has_grapheme_break_property(u32, GraphemeBreakProperty) { return {}; }
  297. bool __attribute__((weak)) code_point_has_word_break_property(u32, WordBreakProperty) { return {}; }
  298. bool __attribute__((weak)) code_point_has_sentence_break_property(u32, SentenceBreakProperty) { return {}; }
  299. Vector<size_t> find_grapheme_segmentation_boundaries([[maybe_unused]] Utf16View const& view)
  300. {
  301. #if ENABLE_UNICODE_DATA
  302. using GBP = GraphemeBreakProperty;
  303. Vector<size_t> boundaries;
  304. // https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundary_Rules
  305. if (view.length_in_code_points() == 0)
  306. return boundaries;
  307. auto has_any_gbp = [](u32 code_point, auto&&... properties) {
  308. return (code_point_has_grapheme_break_property(code_point, properties) || ...);
  309. };
  310. // GB1
  311. boundaries.append(0);
  312. if (view.length_in_code_points() > 1) {
  313. auto it = view.begin();
  314. auto code_point = *it;
  315. u32 next_code_point;
  316. auto current_ri_chain = 0;
  317. auto in_emoji_sequence = false;
  318. for (++it; it != view.end(); ++it, code_point = next_code_point) {
  319. next_code_point = *it;
  320. auto code_point_is_cr = has_any_gbp(code_point, GBP::CR);
  321. auto next_code_point_is_lf = has_any_gbp(next_code_point, GBP::LF);
  322. // GB3
  323. if (code_point_is_cr && next_code_point_is_lf)
  324. continue;
  325. // GB4, GB5
  326. if (code_point_is_cr || next_code_point_is_lf || has_any_gbp(next_code_point, GBP::CR, GBP::Control) || has_any_gbp(code_point, GBP::LF, GBP::Control)) {
  327. boundaries.append(view.code_unit_offset_of(it));
  328. continue;
  329. }
  330. auto next_code_point_is_v = has_any_gbp(next_code_point, GBP::V);
  331. auto next_code_point_is_t = has_any_gbp(next_code_point, GBP::T);
  332. // GB6
  333. if (has_any_gbp(code_point, GBP::L) && (next_code_point_is_v || has_any_gbp(next_code_point, GBP::L, GBP::LV, GBP::LVT)))
  334. continue;
  335. // GB7
  336. if ((next_code_point_is_v || next_code_point_is_t) && has_any_gbp(code_point, GBP::LV, GBP::V))
  337. continue;
  338. // GB8
  339. if (next_code_point_is_t && has_any_gbp(code_point, GBP::LVT, GBP::T))
  340. continue;
  341. auto code_point_is_zwj = has_any_gbp(code_point, GBP::ZWJ);
  342. if (!in_emoji_sequence && code_point_has_property(code_point, Property::Extended_Pictographic))
  343. in_emoji_sequence = true;
  344. else if (in_emoji_sequence && !has_any_gbp(code_point, GBP::Extend) && !code_point_is_zwj)
  345. in_emoji_sequence = false;
  346. // GB9
  347. if (has_any_gbp(next_code_point, GBP::Extend, GBP::ZWJ))
  348. continue;
  349. // GB9a
  350. if (has_any_gbp(next_code_point, GBP::SpacingMark))
  351. continue;
  352. // GB9b
  353. if (has_any_gbp(code_point, GBP::Prepend))
  354. continue;
  355. // GB11
  356. if (in_emoji_sequence && code_point_is_zwj && code_point_has_property(next_code_point, Property::Extended_Pictographic))
  357. continue;
  358. auto code_point_is_ri = has_any_gbp(code_point, GBP::Regional_Indicator);
  359. current_ri_chain = code_point_is_ri ? current_ri_chain + 1 : 0;
  360. // GB12, GB13
  361. if (code_point_is_ri && has_any_gbp(next_code_point, GBP::Regional_Indicator) && current_ri_chain % 2 == 1)
  362. continue;
  363. // GB999
  364. boundaries.append(view.code_unit_offset_of(it));
  365. }
  366. }
  367. // GB2
  368. boundaries.append(view.length_in_code_units());
  369. return boundaries;
  370. #else
  371. return {};
  372. #endif
  373. }
  374. Vector<size_t> find_word_segmentation_boundaries([[maybe_unused]] Utf16View const& view)
  375. {
  376. #if ENABLE_UNICODE_DATA
  377. using WBP = WordBreakProperty;
  378. Vector<size_t> boundaries;
  379. // https://www.unicode.org/reports/tr29/#Word_Boundary_Rules
  380. if (view.length_in_code_points() == 0)
  381. return boundaries;
  382. auto has_any_wbp = [](u32 code_point, auto&&... properties) {
  383. return (code_point_has_word_break_property(code_point, properties) || ...);
  384. };
  385. // WB1
  386. boundaries.append(0);
  387. if (view.length_in_code_points() > 1) {
  388. auto it = view.begin();
  389. auto code_point = *it;
  390. u32 next_code_point;
  391. Optional<u32> previous_code_point;
  392. auto current_ri_chain = 0;
  393. for (++it; it != view.end(); ++it, previous_code_point = code_point, code_point = next_code_point) {
  394. next_code_point = *it;
  395. auto code_point_is_cr = has_any_wbp(code_point, WBP::CR);
  396. auto next_code_point_is_lf = has_any_wbp(next_code_point, WBP::LF);
  397. // WB3
  398. if (code_point_is_cr && next_code_point_is_lf)
  399. continue;
  400. // WB3a, WB3b
  401. if (code_point_is_cr || next_code_point_is_lf || has_any_wbp(next_code_point, WBP::CR, WBP::Newline) || has_any_wbp(code_point, WBP::LF, WBP::Newline)) {
  402. boundaries.append(view.code_unit_offset_of(it));
  403. continue;
  404. }
  405. // WB3c
  406. if (has_any_wbp(code_point, WBP::ZWJ) && code_point_has_property(next_code_point, Property::Extended_Pictographic))
  407. continue;
  408. // WB3d
  409. if (has_any_wbp(code_point, WBP::WSegSpace) && has_any_wbp(next_code_point, WBP::WSegSpace))
  410. continue;
  411. // WB4
  412. if (has_any_wbp(next_code_point, WBP::Format, WBP::Extend, WBP::ZWJ))
  413. continue;
  414. auto code_point_is_hebrew_letter = has_any_wbp(code_point, WBP::Hebrew_Letter);
  415. auto code_point_is_ah_letter = code_point_is_hebrew_letter || has_any_wbp(code_point, WBP::ALetter);
  416. auto next_code_point_is_hebrew_letter = has_any_wbp(next_code_point, WBP::Hebrew_Letter);
  417. auto next_code_point_is_ah_letter = next_code_point_is_hebrew_letter || has_any_wbp(next_code_point, WBP::ALetter);
  418. // WB5
  419. if (code_point_is_ah_letter && next_code_point_is_ah_letter)
  420. continue;
  421. Optional<u32> next_next_code_point;
  422. if (it != view.end()) {
  423. auto it_copy = it;
  424. ++it_copy;
  425. if (it_copy != view.end())
  426. next_next_code_point = *it;
  427. }
  428. bool next_next_code_point_is_hebrew_letter = next_next_code_point.has_value() && has_any_wbp(*next_next_code_point, WBP::Hebrew_Letter);
  429. bool next_next_code_point_is_ah_letter = next_next_code_point_is_hebrew_letter || (next_next_code_point.has_value() && has_any_wbp(*next_next_code_point, WBP::ALetter));
  430. auto next_code_point_is_mid_num_let_q = has_any_wbp(next_code_point, WBP::MidNumLet, WBP::Single_Quote);
  431. // WB6
  432. if (code_point_is_ah_letter && next_next_code_point_is_ah_letter && (next_code_point_is_mid_num_let_q || has_any_wbp(next_code_point, WBP::MidLetter)))
  433. continue;
  434. auto code_point_is_mid_num_let_q = has_any_wbp(code_point, WBP::MidNumLet, WBP::Single_Quote);
  435. auto previous_code_point_is_hebrew_letter = previous_code_point.has_value() && has_any_wbp(*previous_code_point, WBP::Hebrew_Letter);
  436. auto previous_code_point_is_ah_letter = previous_code_point_is_hebrew_letter || (previous_code_point.has_value() && has_any_wbp(*previous_code_point, WBP::ALetter));
  437. // WB7
  438. if (previous_code_point_is_ah_letter && next_code_point_is_ah_letter && (code_point_is_mid_num_let_q || has_any_wbp(code_point, WBP::MidLetter)))
  439. continue;
  440. // WB7a
  441. if (code_point_is_hebrew_letter && has_any_wbp(next_code_point, WBP::Single_Quote))
  442. continue;
  443. // WB7b
  444. if (code_point_is_hebrew_letter && next_next_code_point_is_hebrew_letter && has_any_wbp(next_code_point, WBP::Double_Quote))
  445. continue;
  446. // WB7c
  447. if (previous_code_point_is_hebrew_letter && next_code_point_is_hebrew_letter && has_any_wbp(code_point, WBP::Double_Quote))
  448. continue;
  449. auto code_point_is_numeric = has_any_wbp(code_point, WBP::Numeric);
  450. auto next_code_point_is_numeric = has_any_wbp(next_code_point, WBP::Numeric);
  451. // WB8
  452. if (code_point_is_numeric && next_code_point_is_numeric)
  453. continue;
  454. // WB9
  455. if (code_point_is_ah_letter && next_code_point_is_numeric)
  456. continue;
  457. // WB10
  458. if (code_point_is_numeric && next_code_point_is_ah_letter)
  459. continue;
  460. auto previous_code_point_is_numeric = previous_code_point.has_value() && has_any_wbp(code_point, WBP::Numeric);
  461. // WB11
  462. if (previous_code_point_is_numeric && next_code_point_is_numeric && (code_point_is_mid_num_let_q || has_any_wbp(code_point, WBP::MidNum)))
  463. continue;
  464. bool next_next_code_point_is_numeric = next_next_code_point.has_value() && has_any_wbp(*next_next_code_point, WBP::Numeric);
  465. // WB12
  466. if (code_point_is_numeric && next_next_code_point_is_numeric && (next_code_point_is_mid_num_let_q || has_any_wbp(next_code_point, WBP::MidNum)))
  467. continue;
  468. auto code_point_is_katakana = has_any_wbp(code_point, WBP::Katakana);
  469. auto next_code_point_is_katakana = has_any_wbp(next_code_point, WBP::Katakana);
  470. // WB13
  471. if (code_point_is_katakana && next_code_point_is_katakana)
  472. continue;
  473. auto code_point_is_extend_num_let = has_any_wbp(code_point, WBP::ExtendNumLet);
  474. // WB13a
  475. if ((code_point_is_ah_letter || code_point_is_numeric || code_point_is_katakana || code_point_is_extend_num_let) && has_any_wbp(next_code_point, WBP::ExtendNumLet))
  476. continue;
  477. // WB13b
  478. if (code_point_is_extend_num_let && (next_code_point_is_ah_letter || next_code_point_is_numeric || next_code_point_is_katakana))
  479. continue;
  480. auto code_point_is_ri = has_any_wbp(code_point, WBP::Regional_Indicator);
  481. current_ri_chain = code_point_is_ri ? current_ri_chain + 1 : 0;
  482. // WB15, WB16
  483. if (code_point_is_ri && has_any_wbp(next_code_point, WBP::Regional_Indicator) && current_ri_chain % 2 == 1)
  484. continue;
  485. // WB999
  486. boundaries.append(view.code_unit_offset_of(it));
  487. }
  488. }
  489. // WB2
  490. boundaries.append(view.length_in_code_units());
  491. return boundaries;
  492. #else
  493. return {};
  494. #endif
  495. }
  496. Vector<size_t> find_sentence_segmentation_boundaries([[maybe_unused]] Utf16View const& view)
  497. {
  498. #if ENABLE_UNICODE_DATA
  499. using SBP = SentenceBreakProperty;
  500. Vector<size_t> boundaries;
  501. // https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundary_Rules
  502. if (view.length_in_code_points() == 0)
  503. return boundaries;
  504. auto has_any_sbp = [](u32 code_point, auto&&... properties) {
  505. return (code_point_has_sentence_break_property(code_point, properties) || ...);
  506. };
  507. // SB1
  508. boundaries.append(0);
  509. if (view.length_in_code_points() > 1) {
  510. auto it = view.begin();
  511. auto code_point = *it;
  512. u32 next_code_point;
  513. Optional<u32> previous_code_point;
  514. enum class TerminatorSequenceState {
  515. None,
  516. Term,
  517. Close,
  518. Sp
  519. } terminator_sequence_state { TerminatorSequenceState::None };
  520. auto term_was_a_term = false;
  521. for (++it; it != view.end(); ++it, previous_code_point = code_point, code_point = next_code_point) {
  522. next_code_point = *it;
  523. auto code_point_is_cr = has_any_sbp(code_point, SBP::CR);
  524. auto next_code_point_is_lf = has_any_sbp(next_code_point, SBP::LF);
  525. // SB3
  526. if (code_point_is_cr && next_code_point_is_lf)
  527. continue;
  528. auto code_point_is_para_sep = code_point_is_cr || has_any_sbp(code_point, SBP::LF, SBP::Sep);
  529. // SB4
  530. if (code_point_is_para_sep) {
  531. boundaries.append(view.code_unit_offset_of(it));
  532. continue;
  533. }
  534. // SB5
  535. if (has_any_sbp(next_code_point, SBP::Format, SBP::Extend))
  536. continue;
  537. auto code_point_is_a_term = has_any_sbp(code_point, SBP::ATerm);
  538. // SB6
  539. if (code_point_is_a_term && has_any_sbp(next_code_point, SBP::Numeric))
  540. continue;
  541. // SB7
  542. if (code_point_is_a_term && previous_code_point.has_value() && has_any_sbp(*previous_code_point, SBP::Upper, SBP::Lower) && has_any_sbp(next_code_point, SBP::Upper))
  543. continue;
  544. if (code_point_is_a_term || has_any_sbp(code_point, SBP::STerm)) {
  545. terminator_sequence_state = TerminatorSequenceState::Term;
  546. term_was_a_term = code_point_is_a_term;
  547. } else if (terminator_sequence_state >= TerminatorSequenceState::Term && terminator_sequence_state <= TerminatorSequenceState::Close && has_any_sbp(code_point, SBP::Close)) {
  548. terminator_sequence_state = TerminatorSequenceState::Close;
  549. } else if (terminator_sequence_state >= TerminatorSequenceState::Term && has_any_sbp(code_point, SBP::Sp)) {
  550. terminator_sequence_state = TerminatorSequenceState::Sp;
  551. } else {
  552. terminator_sequence_state = TerminatorSequenceState::None;
  553. }
  554. // SB8
  555. if (terminator_sequence_state >= TerminatorSequenceState::Term && term_was_a_term) {
  556. auto it_copy = it;
  557. bool illegal_sequence = false;
  558. for (auto sequence_code_point = *it_copy; it_copy != view.end(); ++it_copy) {
  559. if (has_any_sbp(sequence_code_point, SBP::Close, SBP::SContinue, SBP::Numeric, SBP::Sp, SBP::Format, SBP::Extend))
  560. continue;
  561. illegal_sequence = has_any_sbp(sequence_code_point, SBP::Lower);
  562. }
  563. if (illegal_sequence)
  564. continue;
  565. }
  566. // SB8a
  567. if (terminator_sequence_state >= TerminatorSequenceState::Term && (has_any_sbp(next_code_point, SBP::SContinue, SBP::STerm, SBP::ATerm)))
  568. continue;
  569. auto next_code_point_is_sp = has_any_sbp(next_code_point, SBP::Sp);
  570. auto next_code_point_is_para_sep = has_any_sbp(next_code_point, SBP::Sep, SBP::CR, SBP::LF);
  571. // SB9
  572. if (terminator_sequence_state >= TerminatorSequenceState::Term && terminator_sequence_state <= TerminatorSequenceState::Close && (next_code_point_is_sp || next_code_point_is_para_sep || has_any_sbp(next_code_point, SBP::Close)))
  573. continue;
  574. // SB10
  575. if (terminator_sequence_state >= TerminatorSequenceState::Term && (next_code_point_is_sp || next_code_point_is_para_sep))
  576. continue;
  577. // SB11
  578. if (terminator_sequence_state >= TerminatorSequenceState::Term)
  579. boundaries.append(view.code_unit_offset_of(it));
  580. // SB998
  581. }
  582. }
  583. // SB2
  584. boundaries.append(view.length_in_code_units());
  585. return boundaries;
  586. #else
  587. return {};
  588. #endif
  589. }
  590. }