Strings.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  5. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/CharacterTypes.h>
  10. #include <AK/String.h>
  11. #include <AK/Utf16View.h>
  12. #include <AK/Utf8View.h>
  13. #include <LibWeb/Infra/CharacterTypes.h>
  14. #include <LibWeb/Infra/Strings.h>
  15. namespace Web::Infra {
  16. // https://infra.spec.whatwg.org/#ascii-case-insensitive
  17. bool is_ascii_case_insensitive_match(StringView a, StringView b)
  18. {
  19. // A string A is an ASCII case-insensitive match for a string B,
  20. // if the ASCII lowercase of A is the ASCII lowercase of B.
  21. return AK::StringUtils::equals_ignoring_ascii_case(a, b);
  22. }
  23. // https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
  24. ErrorOr<String> strip_and_collapse_whitespace(StringView string)
  25. {
  26. // Replace any sequence of one or more consecutive code points that are ASCII whitespace in the string with a single U+0020 SPACE code point.
  27. StringBuilder builder;
  28. for (auto code_point : Utf8View { string }) {
  29. if (Infra::is_ascii_whitespace(code_point)) {
  30. if (!builder.string_view().ends_with(' '))
  31. builder.append(' ');
  32. continue;
  33. }
  34. TRY(builder.try_append_code_point(code_point));
  35. }
  36. // ...and then remove any leading and trailing ASCII whitespace from that string.
  37. return String::from_utf8(builder.string_view().trim(Infra::ASCII_WHITESPACE));
  38. }
  39. // https://infra.spec.whatwg.org/#code-unit-prefix
  40. bool is_code_unit_prefix(StringView potential_prefix, StringView input)
  41. {
  42. auto potential_prefix_utf16 = utf8_to_utf16(potential_prefix).release_value_but_fixme_should_propagate_errors();
  43. auto input_utf16 = utf8_to_utf16(input).release_value_but_fixme_should_propagate_errors();
  44. // 1. Let i be 0.
  45. size_t i = 0;
  46. // 2. While true:
  47. while (true) {
  48. // 1. If i is greater than or equal to potentialPrefix’s length, then return true.
  49. if (i >= potential_prefix.length())
  50. return true;
  51. // 2. If i is greater than or equal to input’s length, then return false.
  52. if (i >= input.length())
  53. return false;
  54. // 3. Let potentialPrefixCodeUnit be the ith code unit of potentialPrefix.
  55. auto potential_prefix_code_unit = Utf16View(potential_prefix_utf16).code_unit_at(i);
  56. // 4. Let inputCodeUnit be the ith code unit of input.
  57. auto input_code_unit = Utf16View(input_utf16).code_unit_at(i);
  58. // 5. Return false if potentialPrefixCodeUnit is not inputCodeUnit.
  59. if (potential_prefix_code_unit != input_code_unit)
  60. return false;
  61. // 6. Set i to i + 1.
  62. ++i;
  63. }
  64. }
  65. // https://infra.spec.whatwg.org/#scalar-value-string
  66. ErrorOr<String> convert_to_scalar_value_string(StringView string)
  67. {
  68. // To convert a string into a scalar value string, replace any surrogates with U+FFFD.
  69. StringBuilder scalar_value_builder;
  70. auto utf8_view = Utf8View { string };
  71. for (u32 code_point : utf8_view) {
  72. if (is_unicode_surrogate(code_point))
  73. code_point = 0xFFFD;
  74. TRY(scalar_value_builder.try_append(code_point));
  75. }
  76. return scalar_value_builder.to_string();
  77. }
  78. // https://infra.spec.whatwg.org/#ascii-lowercase
  79. ErrorOr<String> to_ascii_lowercase(StringView string)
  80. {
  81. // To ASCII lowercase a string, replace all ASCII upper alphas in the string with their
  82. // corresponding code point in ASCII lower alpha.
  83. StringBuilder string_builder;
  84. auto utf8_view = Utf8View { string };
  85. for (u32 code_point : utf8_view) {
  86. code_point = AK::to_ascii_lowercase(code_point);
  87. TRY(string_builder.try_append(code_point));
  88. }
  89. return string_builder.to_string();
  90. }
  91. // https://infra.spec.whatwg.org/#ascii-uppercase
  92. ErrorOr<String> to_ascii_uppercase(StringView string)
  93. {
  94. // To ASCII uppercase a string, replace all ASCII lower alphas in the string with their
  95. // corresponding code point in ASCII upper alpha.
  96. StringBuilder string_builder;
  97. auto utf8_view = Utf8View { string };
  98. for (u32 code_point : utf8_view) {
  99. code_point = AK::to_ascii_uppercase(code_point);
  100. TRY(string_builder.try_append(code_point));
  101. }
  102. return string_builder.to_string();
  103. }
  104. }