mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 17:31:58 -05:00
LibWeb: Add is_code_unit_prefix() function
This commit is contained in:
parent
dcad8494d6
commit
ee27d8cdfd
2 changed files with 38 additions and 0 deletions
|
@ -1,10 +1,12 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/Utf16View.h>
|
||||
#include <AK/Utf8View.h>
|
||||
#include <LibWeb/Infra/CharacterTypes.h>
|
||||
#include <LibWeb/Infra/Strings.h>
|
||||
|
@ -29,4 +31,38 @@ String strip_and_collapse_whitespace(StringView 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 potentialPrefix’s length, then return true.
|
||||
if (i >= potential_prefix.length())
|
||||
return true;
|
||||
|
||||
// 2. If i is greater than or equal to input’s 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -11,5 +12,6 @@
|
|||
namespace Web::Infra {
|
||||
|
||||
String strip_and_collapse_whitespace(StringView string);
|
||||
bool is_code_unit_prefix(StringView potential_prefix, StringView input);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue