From 1748362e05a520986039f25968880260067bf5f7 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 11 Jul 2022 21:44:43 +0100 Subject: [PATCH] LibWeb: Add 'byte-{lower,upper}case' operations from the Infra spec Usually operations that mirror AOs from the Infra spec are simply part of the underlying data structures in AK directly, but these don't seem generally useful enough to add them as ByteBuffer methods. --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../Libraries/LibWeb/Infra/ByteSequences.cpp | 28 +++++++++++++++++++ .../Libraries/LibWeb/Infra/ByteSequences.h | 16 +++++++++++ 3 files changed, 45 insertions(+) create mode 100644 Userland/Libraries/LibWeb/Infra/ByteSequences.cpp create mode 100644 Userland/Libraries/LibWeb/Infra/ByteSequences.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 5c083401c0..8665b6e697 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -237,6 +237,7 @@ set(SOURCES HTML/WorkerLocation.cpp HighResolutionTime/Performance.cpp ImageDecoding.cpp + Infra/ByteSequences.cpp IntersectionObserver/IntersectionObserver.cpp Layout/BlockContainer.cpp Layout/BlockFormattingContext.cpp diff --git a/Userland/Libraries/LibWeb/Infra/ByteSequences.cpp b/Userland/Libraries/LibWeb/Infra/ByteSequences.cpp new file mode 100644 index 0000000000..f54008c0db --- /dev/null +++ b/Userland/Libraries/LibWeb/Infra/ByteSequences.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Web::Infra { + +// https://infra.spec.whatwg.org/#byte-lowercase +void byte_lowercase(ByteBuffer& bytes) +{ + // To byte-lowercase a byte sequence, increase each byte it contains, in the range 0x41 (A) to 0x5A (Z), inclusive, by 0x20. + for (size_t i = 0; i < bytes.size(); ++i) + bytes[i] = to_ascii_lowercase(bytes[i]); +} + +// https://infra.spec.whatwg.org/#byte-uppercase +void byte_uppercase(ByteBuffer& bytes) +{ + // To byte-uppercase a byte sequence, subtract each byte it contains, in the range 0x61 (a) to 0x7A (z), inclusive, by 0x20. + for (size_t i = 0; i < bytes.size(); ++i) + bytes[i] = to_ascii_uppercase(bytes[i]); +} + +} diff --git a/Userland/Libraries/LibWeb/Infra/ByteSequences.h b/Userland/Libraries/LibWeb/Infra/ByteSequences.h new file mode 100644 index 0000000000..aac82e314e --- /dev/null +++ b/Userland/Libraries/LibWeb/Infra/ByteSequences.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::Infra { + +void byte_lowercase(ByteBuffer&); +void byte_uppercase(ByteBuffer&); + +}