/src/serenity/Userland/Libraries/LibWeb/Infra/ByteSequences.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/CharacterTypes.h> |
8 | | #include <LibWeb/Infra/ByteSequences.h> |
9 | | |
10 | | namespace Web::Infra { |
11 | | |
12 | | // https://infra.spec.whatwg.org/#byte-lowercase |
13 | | void byte_lowercase(ByteBuffer& bytes) |
14 | 0 | { |
15 | | // To byte-lowercase a byte sequence, increase each byte it contains, in the range 0x41 (A) to 0x5A (Z), inclusive, by 0x20. |
16 | 0 | for (size_t i = 0; i < bytes.size(); ++i) |
17 | 0 | bytes[i] = to_ascii_lowercase(bytes[i]); |
18 | 0 | } |
19 | | |
20 | | // https://infra.spec.whatwg.org/#byte-uppercase |
21 | | void byte_uppercase(ByteBuffer& bytes) |
22 | 0 | { |
23 | | // To byte-uppercase a byte sequence, subtract each byte it contains, in the range 0x61 (a) to 0x7A (z), inclusive, by 0x20. |
24 | 0 | for (size_t i = 0; i < bytes.size(); ++i) |
25 | 0 | bytes[i] = to_ascii_uppercase(bytes[i]); |
26 | 0 | } |
27 | | |
28 | | // https://infra.spec.whatwg.org/#byte-sequence-starts-with |
29 | | bool is_prefix_of(ReadonlyBytes potential_prefix, ReadonlyBytes input) |
30 | 0 | { |
31 | | // "input starts with potentialPrefix" can be used as a synonym for "potentialPrefix is a prefix of input". |
32 | 0 | return input.starts_with(potential_prefix); |
33 | 0 | } |
34 | | |
35 | | // https://infra.spec.whatwg.org/#byte-less-than |
36 | | bool is_byte_less_than(ReadonlyBytes a, ReadonlyBytes b) |
37 | 0 | { |
38 | | // 1. If b is a prefix of a, then return false. |
39 | 0 | if (is_prefix_of(b, a)) |
40 | 0 | return false; |
41 | | |
42 | | // 2. If a is a prefix of b, then return true. |
43 | 0 | if (is_prefix_of(a, b)) |
44 | 0 | return true; |
45 | | |
46 | | // 3. Let n be the smallest index such that the nth byte of a is different from the nth byte of b. |
47 | | // (There has to be such an index, since neither byte sequence is a prefix of the other.) |
48 | | // 4. If the nth byte of a is less than the nth byte of b, then return true. |
49 | | // 5. Return false. |
50 | 0 | for (size_t i = 0; i < a.size(); ++i) { |
51 | 0 | if (a[i] != b[i]) |
52 | 0 | return a[i] < b[i]; |
53 | 0 | } |
54 | 0 | VERIFY_NOT_REACHED(); |
55 | 0 | } |
56 | | |
57 | | } |