CharacterTypes.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2021-2023, 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/Utf32View.h>
  12. #include <AK/Utf8View.h>
  13. #include <LibUnicode/CharacterTypes.h>
  14. #include <LibUnicode/UnicodeUtils.h>
  15. #if ENABLE_UNICODE_DATA
  16. # include <LibUnicode/UnicodeData.h>
  17. #endif
  18. namespace Unicode {
  19. Optional<ByteString> __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. ReadonlySpan<BlockName> __attribute__((weak)) block_display_names() { return {}; }
  24. u32 __attribute__((weak)) to_unicode_lowercase(u32 code_point)
  25. {
  26. return to_ascii_lowercase(code_point);
  27. }
  28. u32 __attribute__((weak)) to_unicode_uppercase(u32 code_point)
  29. {
  30. return to_ascii_uppercase(code_point);
  31. }
  32. u32 __attribute__((weak)) to_unicode_titlecase(u32 code_point)
  33. {
  34. return to_ascii_uppercase(code_point);
  35. }
  36. template<typename ViewType>
  37. class CasefoldStringComparator {
  38. public:
  39. explicit CasefoldStringComparator(ViewType string)
  40. : m_string(string)
  41. , m_it(m_string.begin())
  42. {
  43. }
  44. bool has_more_data() const
  45. {
  46. return !m_casefolded_code_points.is_empty() || (m_it != m_string.end());
  47. }
  48. u32 next_code_point()
  49. {
  50. VERIFY(has_more_data());
  51. if (m_casefolded_code_points.is_empty()) {
  52. m_current_code_point = *m_it;
  53. ++m_it;
  54. m_casefolded_code_points = Unicode::Detail::casefold_code_point(m_current_code_point);
  55. VERIFY(!m_casefolded_code_points.is_empty()); // Must at least contain the provided code point.
  56. }
  57. auto code_point = m_casefolded_code_points[0];
  58. m_casefolded_code_points = m_casefolded_code_points.substring_view(1);
  59. return code_point;
  60. }
  61. private:
  62. ViewType m_string;
  63. typename ViewType::Iterator m_it;
  64. u32 m_current_code_point { 0 };
  65. Utf32View m_casefolded_code_points;
  66. };
  67. // https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G34145
  68. template<typename ViewType>
  69. bool equals_ignoring_case(ViewType lhs, ViewType rhs)
  70. {
  71. // A string X is a caseless match for a string Y if and only if:
  72. // toCasefold(X) = toCasefold(Y)
  73. CasefoldStringComparator lhs_comparator { lhs };
  74. CasefoldStringComparator rhs_comparator { rhs };
  75. while (lhs_comparator.has_more_data() && rhs_comparator.has_more_data()) {
  76. if (lhs_comparator.next_code_point() != rhs_comparator.next_code_point())
  77. return false;
  78. }
  79. return !lhs_comparator.has_more_data() && !rhs_comparator.has_more_data();
  80. }
  81. template bool equals_ignoring_case(Utf8View, Utf8View);
  82. template bool equals_ignoring_case(Utf16View, Utf16View);
  83. template bool equals_ignoring_case(Utf32View, Utf32View);
  84. Optional<GeneralCategory> __attribute__((weak)) general_category_from_string(StringView) { return {}; }
  85. bool __attribute__((weak)) code_point_has_general_category(u32, GeneralCategory) { return {}; }
  86. Optional<Property> __attribute__((weak)) property_from_string(StringView) { return {}; }
  87. bool __attribute__((weak)) code_point_has_property(u32, Property) { return {}; }
  88. bool is_ecma262_property([[maybe_unused]] Property property)
  89. {
  90. #if ENABLE_UNICODE_DATA
  91. // EMCA-262 only allows a subset of Unicode properties: https://tc39.es/ecma262/#table-binary-unicode-properties
  92. switch (property) {
  93. case Unicode::Property::ASCII:
  94. case Unicode::Property::ASCII_Hex_Digit:
  95. case Unicode::Property::Alphabetic:
  96. case Unicode::Property::Any:
  97. case Unicode::Property::Assigned:
  98. case Unicode::Property::Bidi_Control:
  99. case Unicode::Property::Bidi_Mirrored:
  100. case Unicode::Property::Case_Ignorable:
  101. case Unicode::Property::Cased:
  102. case Unicode::Property::Changes_When_Casefolded:
  103. case Unicode::Property::Changes_When_Casemapped:
  104. case Unicode::Property::Changes_When_Lowercased:
  105. case Unicode::Property::Changes_When_NFKC_Casefolded:
  106. case Unicode::Property::Changes_When_Titlecased:
  107. case Unicode::Property::Changes_When_Uppercased:
  108. case Unicode::Property::Dash:
  109. case Unicode::Property::Default_Ignorable_Code_Point:
  110. case Unicode::Property::Deprecated:
  111. case Unicode::Property::Diacritic:
  112. case Unicode::Property::Emoji:
  113. case Unicode::Property::Emoji_Component:
  114. case Unicode::Property::Emoji_Modifier:
  115. case Unicode::Property::Emoji_Modifier_Base:
  116. case Unicode::Property::Emoji_Presentation:
  117. case Unicode::Property::Extended_Pictographic:
  118. case Unicode::Property::Extender:
  119. case Unicode::Property::Grapheme_Base:
  120. case Unicode::Property::Grapheme_Extend:
  121. case Unicode::Property::Hex_Digit:
  122. case Unicode::Property::IDS_Binary_Operator:
  123. case Unicode::Property::IDS_Trinary_Operator:
  124. case Unicode::Property::ID_Continue:
  125. case Unicode::Property::ID_Start:
  126. case Unicode::Property::Ideographic:
  127. case Unicode::Property::Join_Control:
  128. case Unicode::Property::Logical_Order_Exception:
  129. case Unicode::Property::Lowercase:
  130. case Unicode::Property::Math:
  131. case Unicode::Property::Noncharacter_Code_Point:
  132. case Unicode::Property::Pattern_Syntax:
  133. case Unicode::Property::Pattern_White_Space:
  134. case Unicode::Property::Quotation_Mark:
  135. case Unicode::Property::Radical:
  136. case Unicode::Property::Regional_Indicator:
  137. case Unicode::Property::Sentence_Terminal:
  138. case Unicode::Property::Soft_Dotted:
  139. case Unicode::Property::Terminal_Punctuation:
  140. case Unicode::Property::Unified_Ideograph:
  141. case Unicode::Property::Uppercase:
  142. case Unicode::Property::Variation_Selector:
  143. case Unicode::Property::White_Space:
  144. case Unicode::Property::XID_Continue:
  145. case Unicode::Property::XID_Start:
  146. return true;
  147. default:
  148. return false;
  149. }
  150. #else
  151. return false;
  152. #endif
  153. }
  154. Optional<Script> __attribute__((weak)) script_from_string(StringView) { return {}; }
  155. bool __attribute__((weak)) code_point_has_script(u32, Script) { return {}; }
  156. bool __attribute__((weak)) code_point_has_script_extension(u32, Script) { return {}; }
  157. bool __attribute__((weak)) code_point_has_grapheme_break_property(u32, GraphemeBreakProperty) { return {}; }
  158. bool __attribute__((weak)) code_point_has_word_break_property(u32, WordBreakProperty) { return {}; }
  159. bool __attribute__((weak)) code_point_has_sentence_break_property(u32, SentenceBreakProperty) { return {}; }
  160. Optional<BidirectionalClass> __attribute__((weak)) bidirectional_class_from_string(StringView) { return {}; }
  161. Optional<BidirectionalClass> __attribute__((weak)) bidirectional_class(u32) { return {}; }
  162. }