Просмотр исходного кода

LibWeb: Implement an AO to normalize newlines in a string

Timothy Flynn 1 год назад
Родитель
Сommit
1c0541d706

+ 24 - 0
Userland/Libraries/LibWeb/Infra/Strings.cpp

@@ -8,6 +8,7 @@
  */
 
 #include <AK/CharacterTypes.h>
+#include <AK/GenericLexer.h>
 #include <AK/String.h>
 #include <AK/Utf16View.h>
 #include <AK/Utf8View.h>
@@ -24,6 +25,29 @@ bool is_ascii_case_insensitive_match(StringView a, StringView b)
     return AK::StringUtils::equals_ignoring_ascii_case(a, b);
 }
 
+// https://infra.spec.whatwg.org/#normalize-newlines
+String normalize_newlines(String const& string)
+{
+    // To normalize newlines in a string, replace every U+000D CR U+000A LF code point pair with a single U+000A LF
+    // code point, and then replace every remaining U+000D CR code point with a U+000A LF code point.
+    if (!string.contains('\r'))
+        return string;
+
+    StringBuilder builder;
+    GenericLexer lexer { string };
+
+    while (!lexer.is_eof()) {
+        builder.append(lexer.consume_until('\r'));
+
+        if (lexer.peek() == '\r') {
+            lexer.ignore(1 + static_cast<size_t>(lexer.peek(1) == '\n'));
+            builder.append('\n');
+        }
+    }
+
+    return MUST(builder.to_string());
+}
+
 // https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
 ErrorOr<String> strip_and_collapse_whitespace(StringView string)
 {

+ 1 - 0
Userland/Libraries/LibWeb/Infra/Strings.h

@@ -14,6 +14,7 @@
 namespace Web::Infra {
 
 bool is_ascii_case_insensitive_match(StringView a, StringView b);
+String normalize_newlines(String const&);
 ErrorOr<String> strip_and_collapse_whitespace(StringView string);
 bool is_code_unit_prefix(StringView potential_prefix, StringView input);
 ErrorOr<String> convert_to_scalar_value_string(StringView string);