From eaa4048870012df17e594a59304b874c2d909000 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Sun, 18 Jun 2023 16:25:33 +0100 Subject: [PATCH] LibTextCodec: Add "get output encoding" from the Encoding specification --- Userland/Libraries/LibTextCodec/Decoder.cpp | 11 +++++++++++ Userland/Libraries/LibTextCodec/Decoder.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/Userland/Libraries/LibTextCodec/Decoder.cpp b/Userland/Libraries/LibTextCodec/Decoder.cpp index 5b40442a6a..31d0dfc8f5 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.cpp +++ b/Userland/Libraries/LibTextCodec/Decoder.cpp @@ -221,6 +221,17 @@ ErrorOr convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte return output; } +// https://encoding.spec.whatwg.org/#get-an-output-encoding +StringView get_output_encoding(StringView encoding) +{ + // 1. If encoding is replacement or UTF-16BE/LE, then return UTF-8. + if (encoding.is_one_of_ignoring_ascii_case("replacement"sv, "utf-16le"sv, "utf-16be"sv)) + return "UTF-8"sv; + + // 2. Return encoding. + return encoding; +} + ErrorOr Decoder::to_utf8(StringView input) { StringBuilder builder(input.length()); diff --git a/Userland/Libraries/LibTextCodec/Decoder.h b/Userland/Libraries/LibTextCodec/Decoder.h index c678677bf5..5586db1fc4 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.h +++ b/Userland/Libraries/LibTextCodec/Decoder.h @@ -97,4 +97,6 @@ Optional bom_sniff_to_decoder(StringView); // This will use the given decoder unless there is a byte order mark in the input, in which we will instead use the appropriate Unicode decoder. ErrorOr convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView); +StringView get_output_encoding(StringView encoding); + }