mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 03:25:13 +00:00
LibWeb: Implement code_unit_less_than
This commit is contained in:
parent
e2a7f844e6
commit
d26ac9c446
Notes:
github-actions[bot]
2024-11-25 10:55:07 +00:00
Author: https://github.com/stelar7 Commit: https://github.com/LadybirdBrowser/ladybird/commit/d26ac9c446d Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2279 Reviewed-by: https://github.com/gmta Reviewed-by: https://github.com/tcl3
2 changed files with 33 additions and 0 deletions
|
@ -167,4 +167,36 @@ String isomorphic_decode(ReadonlyBytes input)
|
|||
return builder.to_string_without_validation();
|
||||
}
|
||||
|
||||
// https://infra.spec.whatwg.org/#code-unit-less-than
|
||||
bool code_unit_less_than(StringView a, StringView b)
|
||||
{
|
||||
// 1. If b is a code unit prefix of a, then return false.
|
||||
if (is_code_unit_prefix(b, a))
|
||||
return false;
|
||||
|
||||
// 2. If a is a code unit prefix of b, then return true.
|
||||
if (is_code_unit_prefix(a, b))
|
||||
return true;
|
||||
|
||||
auto code_units_a = MUST(utf8_to_utf16(a));
|
||||
auto code_units_b = MUST(utf8_to_utf16(b));
|
||||
|
||||
auto view_a = Utf16View(code_units_a);
|
||||
auto view_b = Utf16View(code_units_b);
|
||||
|
||||
// 3. Let n be the smallest index such that the nth code unit of a is different from the nth code unit of b.
|
||||
// (There has to be such an index, since neither string is a prefix of the other.)
|
||||
size_t n = 0;
|
||||
size_t min_length = min(view_a.length_in_code_units(), view_b.length_in_code_units());
|
||||
while (n < min_length && view_a.code_unit_at(n) == view_b.code_unit_at(n))
|
||||
++n;
|
||||
|
||||
// 4. If the nth code unit of a is less than the nth code unit of b, then return true.
|
||||
if (view_a.code_unit_at(n) < view_b.code_unit_at(n))
|
||||
return true;
|
||||
|
||||
// 5. Return false.
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,5 +22,6 @@ ErrorOr<String> to_ascii_lowercase(StringView string);
|
|||
ErrorOr<String> to_ascii_uppercase(StringView string);
|
||||
ByteBuffer isomorphic_encode(StringView input);
|
||||
String isomorphic_decode(ReadonlyBytes input);
|
||||
bool code_unit_less_than(StringView a, StringView b);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue