1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 22:05:07 +00:00
serenity/Userland/Libraries/LibWeb/Infra/Strings.cpp
Linus Groh 6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00

68 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/DeprecatedString.h>
#include <AK/Utf16View.h>
#include <AK/Utf8View.h>
#include <LibWeb/Infra/CharacterTypes.h>
#include <LibWeb/Infra/Strings.h>
namespace Web::Infra {
// https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
DeprecatedString strip_and_collapse_whitespace(StringView string)
{
// 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.
StringBuilder builder;
for (auto code_point : Utf8View { string }) {
if (Infra::is_ascii_whitespace(code_point)) {
if (!builder.string_view().ends_with(' '))
builder.append(' ');
continue;
}
builder.append_code_point(code_point);
}
// ...and then remove any leading and trailing ASCII whitespace from that string.
return builder.string_view().trim(Infra::ASCII_WHITESPACE);
}
// https://infra.spec.whatwg.org/#code-unit-prefix
bool is_code_unit_prefix(StringView potential_prefix, StringView input)
{
auto potential_prefix_utf16 = utf8_to_utf16(potential_prefix);
auto input_utf16 = utf8_to_utf16(input);
// 1. Let i be 0.
size_t i = 0;
// 2. While true:
while (true) {
// 1. If i is greater than or equal to potentialPrefixs length, then return true.
if (i >= potential_prefix.length())
return true;
// 2. If i is greater than or equal to inputs length, then return false.
if (i >= input.length())
return false;
// 3. Let potentialPrefixCodeUnit be the ith code unit of potentialPrefix.
auto potential_prefix_code_unit = Utf16View(potential_prefix_utf16).code_unit_at(i);
// 4. Let inputCodeUnit be the ith code unit of input.
auto input_code_unit = Utf16View(input_utf16).code_unit_at(i);
// 5. Return false if potentialPrefixCodeUnit is not inputCodeUnit.
if (potential_prefix_code_unit != input_code_unit)
return false;
// 6. Set i to i + 1.
++i;
}
}
}