Serenity Operating System
at master 57 lines 1.9 kB view raw
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 10namespace Web::Infra { 11 12// https://infra.spec.whatwg.org/#byte-lowercase 13void byte_lowercase(ByteBuffer& bytes) 14{ 15 // To byte-lowercase a byte sequence, increase each byte it contains, in the range 0x41 (A) to 0x5A (Z), inclusive, by 0x20. 16 for (size_t i = 0; i < bytes.size(); ++i) 17 bytes[i] = to_ascii_lowercase(bytes[i]); 18} 19 20// https://infra.spec.whatwg.org/#byte-uppercase 21void byte_uppercase(ByteBuffer& bytes) 22{ 23 // To byte-uppercase a byte sequence, subtract each byte it contains, in the range 0x61 (a) to 0x7A (z), inclusive, by 0x20. 24 for (size_t i = 0; i < bytes.size(); ++i) 25 bytes[i] = to_ascii_uppercase(bytes[i]); 26} 27 28// https://infra.spec.whatwg.org/#byte-sequence-starts-with 29bool is_prefix_of(ReadonlyBytes potential_prefix, ReadonlyBytes input) 30{ 31 // "input starts with potentialPrefix" can be used as a synonym for "potentialPrefix is a prefix of input". 32 return input.starts_with(potential_prefix); 33} 34 35// https://infra.spec.whatwg.org/#byte-less-than 36bool is_byte_less_than(ReadonlyBytes a, ReadonlyBytes b) 37{ 38 // 1. If b is a prefix of a, then return false. 39 if (is_prefix_of(b, a)) 40 return false; 41 42 // 2. If a is a prefix of b, then return true. 43 if (is_prefix_of(a, b)) 44 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 for (size_t i = 0; i < a.size(); ++i) { 51 if (a[i] != b[i]) 52 return a[i] < b[i]; 53 } 54 VERIFY_NOT_REACHED(); 55} 56 57}