Strings.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/GenericLexer.h>
  11. #include <AK/String.h>
  12. #include <AK/Utf16View.h>
  13. #include <AK/Utf8View.h>
  14. #include <LibWeb/Infra/CharacterTypes.h>
  15. #include <LibWeb/Infra/Strings.h>
  16. namespace Web::Infra {
  17. // https://infra.spec.whatwg.org/#ascii-case-insensitive
  18. bool is_ascii_case_insensitive_match(StringView a, StringView b)
  19. {
  20. // A string A is an ASCII case-insensitive match for a string B,
  21. // if the ASCII lowercase of A is the ASCII lowercase of B.
  22. return AK::StringUtils::equals_ignoring_ascii_case(a, b);
  23. }
  24. // https://infra.spec.whatwg.org/#normalize-newlines
  25. String normalize_newlines(String const& string)
  26. {
  27. // To normalize newlines in a string, replace every U+000D CR U+000A LF code point pair with a single U+000A LF
  28. // code point, and then replace every remaining U+000D CR code point with a U+000A LF code point.
  29. if (!string.contains('\r'))
  30. return string;
  31. StringBuilder builder;
  32. GenericLexer lexer { string };
  33. while (!lexer.is_eof()) {
  34. builder.append(lexer.consume_until('\r'));
  35. if (lexer.peek() == '\r') {
  36. lexer.ignore(1 + static_cast<size_t>(lexer.peek(1) == '\n'));
  37. builder.append('\n');
  38. }
  39. }
  40. return MUST(builder.to_string());
  41. }
  42. // https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
  43. ErrorOr<String> strip_and_collapse_whitespace(StringView string)
  44. {
  45. // 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.
  46. StringBuilder builder;
  47. for (auto code_point : Utf8View { string }) {
  48. if (Infra::is_ascii_whitespace(code_point)) {
  49. if (!builder.string_view().ends_with(' '))
  50. builder.append(' ');
  51. continue;
  52. }
  53. TRY(builder.try_append_code_point(code_point));
  54. }
  55. // ...and then remove any leading and trailing ASCII whitespace from that string.
  56. return String::from_utf8(builder.string_view().trim(Infra::ASCII_WHITESPACE));
  57. }
  58. // https://infra.spec.whatwg.org/#code-unit-prefix
  59. bool is_code_unit_prefix(StringView potential_prefix, StringView input)
  60. {
  61. auto potential_prefix_utf16 = utf8_to_utf16(potential_prefix).release_value_but_fixme_should_propagate_errors();
  62. auto input_utf16 = utf8_to_utf16(input).release_value_but_fixme_should_propagate_errors();
  63. // 1. Let i be 0.
  64. size_t i = 0;
  65. // 2. While true:
  66. while (true) {
  67. // 1. If i is greater than or equal to potentialPrefix’s length, then return true.
  68. if (i >= potential_prefix.length())
  69. return true;
  70. // 2. If i is greater than or equal to input’s length, then return false.
  71. if (i >= input.length())
  72. return false;
  73. // 3. Let potentialPrefixCodeUnit be the ith code unit of potentialPrefix.
  74. auto potential_prefix_code_unit = Utf16View(potential_prefix_utf16).code_unit_at(i);
  75. // 4. Let inputCodeUnit be the ith code unit of input.
  76. auto input_code_unit = Utf16View(input_utf16).code_unit_at(i);
  77. // 5. Return false if potentialPrefixCodeUnit is not inputCodeUnit.
  78. if (potential_prefix_code_unit != input_code_unit)
  79. return false;
  80. // 6. Set i to i + 1.
  81. ++i;
  82. }
  83. }
  84. // https://infra.spec.whatwg.org/#scalar-value-string
  85. ErrorOr<String> convert_to_scalar_value_string(StringView string)
  86. {
  87. // To convert a string into a scalar value string, replace any surrogates with U+FFFD.
  88. StringBuilder scalar_value_builder;
  89. auto utf8_view = Utf8View { string };
  90. for (u32 code_point : utf8_view) {
  91. if (is_unicode_surrogate(code_point))
  92. code_point = 0xFFFD;
  93. scalar_value_builder.append_code_point(code_point);
  94. }
  95. return scalar_value_builder.to_string();
  96. }
  97. // https://infra.spec.whatwg.org/#ascii-lowercase
  98. ErrorOr<String> to_ascii_lowercase(StringView string)
  99. {
  100. // To ASCII lowercase a string, replace all ASCII upper alphas in the string with their
  101. // corresponding code point in ASCII lower alpha.
  102. StringBuilder string_builder;
  103. auto utf8_view = Utf8View { string };
  104. for (u32 code_point : utf8_view) {
  105. code_point = AK::to_ascii_lowercase(code_point);
  106. string_builder.append_code_point(code_point);
  107. }
  108. return string_builder.to_string();
  109. }
  110. // https://infra.spec.whatwg.org/#ascii-uppercase
  111. ErrorOr<String> to_ascii_uppercase(StringView string)
  112. {
  113. // To ASCII uppercase a string, replace all ASCII lower alphas in the string with their
  114. // corresponding code point in ASCII upper alpha.
  115. StringBuilder string_builder;
  116. auto utf8_view = Utf8View { string };
  117. for (u32 code_point : utf8_view) {
  118. code_point = AK::to_ascii_uppercase(code_point);
  119. string_builder.append_code_point(code_point);
  120. }
  121. return string_builder.to_string();
  122. }
  123. }