mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
LibWeb: Implement an AO to normalize newlines in a string
This commit is contained in:
parent
324feb04e7
commit
1c0541d706
Notes:
sideshowbarker
2024-07-16 21:39:23 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/1c0541d706 Pull-request: https://github.com/SerenityOS/serenity/pull/23586
2 changed files with 25 additions and 0 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue