Locale.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/Optional.h>
  9. #include <AK/String.h>
  10. #include <AK/StringView.h>
  11. #include <AK/Variant.h>
  12. #include <AK/Vector.h>
  13. #include <LibUnicode/Forward.h>
  14. namespace Unicode {
  15. struct LanguageID {
  16. String to_string() const;
  17. bool operator==(LanguageID const&) const = default;
  18. bool is_root { false };
  19. Optional<String> language {};
  20. Optional<String> script {};
  21. Optional<String> region {};
  22. Vector<String> variants {};
  23. };
  24. struct Keyword {
  25. String key {};
  26. String value {};
  27. };
  28. struct LocaleExtension {
  29. Vector<String> attributes {};
  30. Vector<Keyword> keywords {};
  31. };
  32. struct TransformedField {
  33. String key {};
  34. String value {};
  35. };
  36. struct TransformedExtension {
  37. Optional<LanguageID> language {};
  38. Vector<TransformedField> fields {};
  39. };
  40. struct OtherExtension {
  41. char key {};
  42. String value {};
  43. };
  44. using Extension = AK::Variant<LocaleExtension, TransformedExtension, OtherExtension>;
  45. struct LocaleID {
  46. String to_string() const;
  47. template<typename ExtensionType>
  48. Vector<Extension> remove_extension_type()
  49. {
  50. Vector<Extension> removed_extensions {};
  51. auto tmp_extensions = move(extensions);
  52. for (auto& extension : tmp_extensions) {
  53. if (extension.has<ExtensionType>())
  54. removed_extensions.append(move(extension));
  55. else
  56. extensions.append(move(extension));
  57. }
  58. return removed_extensions;
  59. }
  60. LanguageID language_id {};
  61. Vector<Extension> extensions {};
  62. Vector<String> private_use_extensions {};
  63. };
  64. enum class Style {
  65. Long,
  66. Short,
  67. Narrow,
  68. };
  69. // Note: These methods only verify that the provided strings match the EBNF grammar of the
  70. // Unicode identifier subtag (i.e. no validation is done that the tags actually exist).
  71. constexpr bool is_unicode_language_subtag(StringView subtag)
  72. {
  73. // unicode_language_subtag = alpha{2,3} | alpha{5,8}
  74. if ((subtag.length() < 2) || (subtag.length() == 4) || (subtag.length() > 8))
  75. return false;
  76. return all_of(subtag, is_ascii_alpha);
  77. }
  78. constexpr bool is_unicode_script_subtag(StringView subtag)
  79. {
  80. // unicode_script_subtag = alpha{4}
  81. if (subtag.length() != 4)
  82. return false;
  83. return all_of(subtag, is_ascii_alpha);
  84. }
  85. constexpr bool is_unicode_region_subtag(StringView subtag)
  86. {
  87. // unicode_region_subtag = (alpha{2} | digit{3})
  88. if (subtag.length() == 2)
  89. return all_of(subtag, is_ascii_alpha);
  90. if (subtag.length() == 3)
  91. return all_of(subtag, is_ascii_digit);
  92. return false;
  93. }
  94. constexpr bool is_unicode_variant_subtag(StringView subtag)
  95. {
  96. // unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3})
  97. if ((subtag.length() >= 5) && (subtag.length() <= 8))
  98. return all_of(subtag, is_ascii_alphanumeric);
  99. if (subtag.length() == 4)
  100. return is_ascii_digit(subtag[0]) && all_of(subtag.substring_view(1), is_ascii_alphanumeric);
  101. return false;
  102. }
  103. bool is_type_identifier(StringView);
  104. Optional<LanguageID> parse_unicode_language_id(StringView);
  105. Optional<LocaleID> parse_unicode_locale_id(StringView);
  106. String canonicalize_unicode_locale_id(StringView);
  107. String canonicalize_unicode_extension_values(StringView key, StringView value);
  108. StringView default_locale();
  109. bool is_locale_available(StringView locale);
  110. Style style_from_string(StringView style);
  111. StringView style_to_string(Style style);
  112. Optional<String> add_likely_subtags(StringView);
  113. Optional<String> remove_likely_subtags(StringView);
  114. bool is_locale_character_ordering_right_to_left(StringView locale);
  115. }