Skip to content

Truly independent web browser

LibTextCodec: Name the decode algorithms after the spec

Shannon Boothf1cc97c612ad
diff --git a/Libraries/LibJS/Rust/src/utilities/js.rs b/Libraries/LibJS/Rust/src/utilities/js.rs
index 4c4efc0828c..8e5d559347e 100644
--- a/Libraries/LibJS/Rust/src/utilities/js.rs
+++ b/Libraries/LibJS/Rust/src/utilities/js.rs
@@ -2015,9 +2015,8 @@ fn decode_utf16_with_replacement(bytes: &[u8], code_unit_from_bytes: fn([u8; 2])
output
}
-/// TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark() with the windows-1252
-/// decoder.
-fn convert_input_to_utf8_using_windows_1252_unless_there_is_a_byte_order_mark(input: &[u8]) -> String {
+/// TextCodec::decode_to_utf8() with the windows-1252 decoder as the fallback.
+fn decode_to_utf8_with_windows_1252_fallback(input: &[u8]) -> String {
if let Some(input) = input.strip_prefix(b"\xEF\xBB\xBF") {
return String::from_utf8_lossy(input).into_owned();
}
@@ -2155,10 +2154,7 @@ fn ladybird_main(arguments: &[String], line_editor: Option<JSLineEditor>) -> c_i
if utf16_from_wtf8(&file_contents).is_some() {
builder.extend_from_slice(&file_contents);
} else {
- builder.extend_from_slice(
- convert_input_to_utf8_using_windows_1252_unless_there_is_a_byte_order_mark(&file_contents)
- .as_bytes(),
- );
+ builder.extend_from_slice(decode_to_utf8_with_windows_1252_fallback(&file_contents).as_bytes());
}
}
diff --git a/Libraries/LibJS/SourceCode.cpp b/Libraries/LibJS/SourceCode.cpp
index f09c9a04623..78dcdbdc4b8 100644
--- a/Libraries/LibJS/SourceCode.cpp
+++ b/Libraries/LibJS/SourceCode.cpp
@@ -32,7 +32,7 @@ static Utf16String decode_source_bytes(ReadonlyBytes source_bytes, StringView so
auto decoder = TextCodec::decoder_for(source_encoding);
VERIFY(decoder.has_value());
- auto code = MUST(TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, StringView { source_bytes }));
+ auto code = MUST(TextCodec::decode_to_utf16(StringView { source_bytes }, *decoder));
if (code.length_in_code_units() > length_in_code_units)
return Utf16String::from_utf16(code.utf16_view().substring_view(0, length_in_code_units));
return code;
diff --git a/Libraries/LibJS/SyntaxHighlighter.cpp b/Libraries/LibJS/SyntaxHighlighter.cpp
index 2fa1faef85f..bb0301d8947 100644
--- a/Libraries/LibJS/SyntaxHighlighter.cpp
+++ b/Libraries/LibJS/SyntaxHighlighter.cpp
@@ -101,7 +101,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
auto text = m_client->get_text();
auto decoder = TextCodec::decoder_for("UTF-8"sv);
VERIFY(decoder.has_value());
- auto source = TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, text)
+ auto source = TextCodec::decode_to_utf16(text, *decoder)
.release_value_but_fixme_should_propagate_errors();
Vector<Syntax::TextDocumentSpan> spans;
diff --git a/Libraries/LibTextCodec/Decoder.cpp b/Libraries/LibTextCodec/Decoder.cpp
index ec663561bba..620e82f70b2 100644
--- a/Libraries/LibTextCodec/Decoder.cpp
+++ b/Libraries/LibTextCodec/Decoder.cpp
@@ -24,7 +24,7 @@ public:
{
}
- virtual ErrorOr<String> to_utf8(StringView input, IgnoreBOM, ErrorMode) override;
+ virtual ErrorOr<String> to_utf8(StringView input, ErrorMode) override;
virtual ErrorOr<size_t> length_in_utf16_code_units(StringView input) override;
private:
@@ -36,14 +36,14 @@ private:
class UTF8Decoder final : public Decoder {
public:
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
- virtual ErrorOr<String> to_utf8(StringView, IgnoreBOM, ErrorMode) override;
+ virtual ErrorOr<String> to_utf8(StringView, ErrorMode) override;
virtual ErrorOr<Utf16String> to_utf16(StringView) override;
virtual ErrorOr<size_t> length_in_utf16_code_units(StringView) override;
};
class UTF16BEDecoder final : public Decoder {
public:
- virtual ErrorOr<String> to_utf8(StringView, IgnoreBOM, ErrorMode) override;
+ virtual ErrorOr<String> to_utf8(StringView, ErrorMode) override;
virtual ErrorOr<size_t> length_in_utf16_code_units(StringView) override;
private:
@@ -52,7 +52,7 @@ private:
class UTF16LEDecoder final : public Decoder {
public:
- virtual ErrorOr<String> to_utf8(StringView, IgnoreBOM, ErrorMode) override;
+ virtual ErrorOr<String> to_utf8(StringView, ErrorMode) override;
virtual ErrorOr<size_t> length_in_utf16_code_units(StringView) override;
private:
@@ -129,7 +129,7 @@ static void append_decoded_utf16(void* context, u16 const* data, size_t length)
decode_context.builder.append(Utf16View { reinterpret_cast<char16_t const*>(data), length });
}
-ErrorOr<String> rust_decode_to_utf8(StringView encoding, StringView input, IgnoreBOM ignore_bom, ErrorMode error_mode)
+ErrorOr<String> rust_decode_to_utf8(StringView encoding, StringView input, ErrorMode error_mode)
{
DecodeContext context { .builder = StringBuilder(input.length()) };
auto succeeded = FFI::textcodec_rust_decode_to_utf8(
@@ -137,7 +137,6 @@ ErrorOr<String> rust_decode_to_utf8(StringView encoding, StringView input, Ignor
encoding.length(),
reinterpret_cast<u8 const*>(input.characters_without_null_termination()),
input.length(),
- ignore_bom == IgnoreBOM::No,
error_mode == ErrorMode::Fatal,
&context,
append_decoded_bytes);
@@ -162,17 +161,17 @@ ErrorOr<Utf16String> rust_streaming_decode_to_utf16(FFI::TextCodecRustStreamingD
return context.builder.to_string();
}
-ErrorOr<void> rust_process(StringView encoding, StringView input, IgnoreBOM ignore_bom, Function<ErrorOr<void>(u32)> on_code_point)
+ErrorOr<void> rust_process(StringView encoding, StringView input, Function<ErrorOr<void>(u32)> on_code_point)
{
- auto utf8 = TRY(rust_decode_to_utf8(encoding, input, ignore_bom, ErrorMode::Replacement));
+ auto utf8 = TRY(rust_decode_to_utf8(encoding, input, ErrorMode::Replacement));
for (auto code_point : Utf8View { utf8 })
TRY(on_code_point(code_point));
return {};
}
-ErrorOr<size_t> rust_length_in_utf16_code_units(StringView encoding, StringView input, IgnoreBOM ignore_bom)
+ErrorOr<size_t> rust_length_in_utf16_code_units(StringView encoding, StringView input)
{
- auto utf8 = TRY(rust_decode_to_utf8(encoding, input, ignore_bom, ErrorMode::Replacement));
+ auto utf8 = TRY(rust_decode_to_utf8(encoding, input, ErrorMode::Replacement));
size_t length = 0;
for (auto code_point : Utf8View { utf8 })
length += code_point <= 0xffff ? 1 : 2;
@@ -339,7 +338,7 @@ Optional<StringView> get_standardized_encoding(Utf16View encoding)
}
// https://encoding.spec.whatwg.org/#bom-sniff
-Optional<Decoder&> bom_sniff_to_decoder(StringView input)
+static Optional<Decoder&> bom_sniff_to_decoder(StringView input)
{
// 1. Let BOM be the result of peeking 3 bytes from ioQueue, converted to a byte sequence.
// 2. For each of the rows in the table below, starting with the first one and going down,
@@ -379,55 +378,55 @@ Optional<Decoder&> bom_sniff_to_decoder(StringView input)
}
// https://encoding.spec.whatwg.org/#decode
-ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
+static Decoder& decoder_for_decode(Decoder& fallback_decoder, StringView& input)
{
- Decoder* actual_decoder = &fallback_decoder;
-
// 1. Let BOMEncoding be the result of BOM sniffing ioQueue.
// 2. If BOMEncoding is non-null:
- if (auto unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder.has_value()) {
+ if (auto bom_decoder = bom_sniff_to_decoder(input); bom_decoder.has_value()) {
// 1. Set encoding to BOMEncoding.
- actual_decoder = &unicode_decoder.value();
-
// 2. Read three bytes from ioQueue, if BOMEncoding is UTF-8; otherwise read two bytes. (Do nothing with those bytes.)
- // FIXME: I imagine this will be pretty slow for large inputs, as it's regenerating the input without the first 2/3 bytes.
- input = input.substring_view(&unicode_decoder.value() == &s_utf8_decoder ? 3 : 2);
+ input = input.substring_view(&bom_decoder.value() == &s_utf8_decoder ? 3 : 2);
+ return bom_decoder.value();
}
+ return fallback_decoder;
+}
- VERIFY(actual_decoder);
+// https://encoding.spec.whatwg.org/#decode
+ErrorOr<String> decode_to_utf8(StringView input, Decoder& fallback_decoder, ErrorMode error_mode)
+{
+ auto& decoder = decoder_for_decode(fallback_decoder, input);
// 3. Process a queue with an instance of encoding’s decoder, ioQueue, output, and "replacement".
- // FIXME: This isn't the exact same as the spec, which is written in terms of I/O queues.
- auto output = TRY(actual_decoder->to_utf8(input, IgnoreBOM::Yes, ErrorMode::Replacement));
-
// 4. Return output.
- return output;
+ // NB: XML documents decode with the fatal error mode instead.
+ return decoder.to_utf8(input, error_mode);
}
-ErrorOr<Utf16String> convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
+// https://encoding.spec.whatwg.org/#decode
+ErrorOr<Utf16String> decode_to_utf16(StringView input, Decoder& fallback_decoder)
{
- Decoder* actual_decoder = &fallback_decoder;
-
- if (auto unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder.has_value()) {
- actual_decoder = &unicode_decoder.value();
- input = input.substring_view(&unicode_decoder.value() == &s_utf8_decoder ? 3 : 2);
- }
-
- VERIFY(actual_decoder);
- return actual_decoder->to_utf16(input);
+ auto& decoder = decoder_for_decode(fallback_decoder, input);
+ return decoder.to_utf16(input);
}
-ErrorOr<size_t> convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
+// https://encoding.spec.whatwg.org/#decode
+ErrorOr<size_t> decoded_length_in_utf16_code_units(StringView input, Decoder& fallback_decoder)
{
- Decoder* actual_decoder = &fallback_decoder;
+ auto& decoder = decoder_for_decode(fallback_decoder, input);
+ return decoder.length_in_utf16_code_units(input);
+}
- if (auto unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder.has_value()) {
- actual_decoder = &unicode_decoder.value();
- input = input.substring_view(&unicode_decoder.value() == &s_utf8_decoder ? 3 : 2);
- }
+// https://encoding.spec.whatwg.org/#utf-8-decode
+ErrorOr<Utf16String> utf8_decode_to_utf16(StringView input)
+{
+ // 1. Let buffer be the result of peeking three bytes from ioQueue, converted to a byte sequence.
+ // 2. If buffer is 0xEF 0xBB 0xBF, then read three bytes from ioQueue. (Do nothing with those bytes.)
+ if (input.starts_with("\xEF\xBB\xBF"sv))
+ input = input.substring_view(3);
- VERIFY(actual_decoder);
- return actual_decoder->length_in_utf16_code_units(input);
+ // 3. Process a queue with an instance of UTF-8’s decoder, ioQueue, output, and "replacement".
+ // 4. Return output.
+ return s_utf8_decoder.to_utf16(input);
}
// https://encoding.spec.whatwg.org/#get-an-output-encoding
@@ -441,7 +440,7 @@ StringView get_output_encoding(StringView encoding)
return encoding;
}
-ErrorOr<String> Decoder::to_utf8(StringView input, IgnoreBOM, ErrorMode)
+ErrorOr<String> Decoder::to_utf8(StringView input, ErrorMode)
{
StringBuilder builder(input.length());
TRY(process(input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
@@ -473,19 +472,19 @@ ErrorOr<void> Decoder::process_code_points(StringView input, Function<ErrorOr<vo
return process(input, move(on_code_point));
}
-ErrorOr<String> RustDecoder::to_utf8(StringView input, IgnoreBOM ignore_bom, ErrorMode error_mode)
+ErrorOr<String> RustDecoder::to_utf8(StringView input, ErrorMode error_mode)
{
- return rust_decode_to_utf8(m_encoding, input, ignore_bom, error_mode);
+ return rust_decode_to_utf8(m_encoding, input, error_mode);
}
ErrorOr<size_t> RustDecoder::length_in_utf16_code_units(StringView input)
{
- return rust_length_in_utf16_code_units(m_encoding, input, IgnoreBOM::Yes);
+ return rust_length_in_utf16_code_units(m_encoding, input);
}
ErrorOr<void> RustDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
{
- return rust_process(m_encoding, input, IgnoreBOM::Yes, move(on_code_point));
+ return rust_process(m_encoding, input, move(on_code_point));
}
ErrorOr<void> Latin1Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
@@ -537,12 +536,12 @@ ErrorOr<Utf16String> StreamingDecoder::finish_to_utf16()
ErrorOr<void> UTF8Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
{
- return rust_process("UTF-8"sv, input, IgnoreBOM::Yes, move(on_code_point));
+ return rust_process("UTF-8"sv, input, move(on_code_point));
}
-ErrorOr<String> UTF8Decoder::to_utf8(StringView input, IgnoreBOM ignore_bom, ErrorMode error_mode)
+ErrorOr<String> UTF8Decoder::to_utf8(StringView input, ErrorMode error_mode)
{
- return rust_decode_to_utf8("UTF-8"sv, input, ignore_bom, error_mode);
+ return rust_decode_to_utf8("UTF-8"sv, input, error_mode);
}
ErrorOr<Utf16String> UTF8Decoder::to_utf16(StringView input)
@@ -555,37 +554,37 @@ ErrorOr<Utf16String> UTF8Decoder::to_utf16(StringView input)
ErrorOr<size_t> UTF8Decoder::length_in_utf16_code_units(StringView input)
{
- return rust_length_in_utf16_code_units("UTF-8"sv, input, IgnoreBOM::Yes);
+ return rust_length_in_utf16_code_units("UTF-8"sv, input);
}
ErrorOr<void> UTF16BEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
{
- return rust_process("UTF-16BE"sv, input, IgnoreBOM::Yes, move(on_code_point));
+ return rust_process("UTF-16BE"sv, input, move(on_code_point));
}
-ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input, IgnoreBOM ignore_bom, ErrorMode error_mode)
+ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input, ErrorMode error_mode)
{
- return rust_decode_to_utf8("UTF-16BE"sv, input, ignore_bom, error_mode);
+ return rust_decode_to_utf8("UTF-16BE"sv, input, error_mode);
}
ErrorOr<size_t> UTF16BEDecoder::length_in_utf16_code_units(StringView input)
{
- return rust_length_in_utf16_code_units("UTF-16BE"sv, input, IgnoreBOM::Yes);
+ return rust_length_in_utf16_code_units("UTF-16BE"sv, input);
}
ErrorOr<void> UTF16LEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
{
- return rust_process("UTF-16LE"sv, input, IgnoreBOM::Yes, move(on_code_point));
+ return rust_process("UTF-16LE"sv, input, move(on_code_point));
}
-ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input, IgnoreBOM ignore_bom, ErrorMode error_mode)
+ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input, ErrorMode error_mode)
{
- return rust_decode_to_utf8("UTF-16LE"sv, input, ignore_bom, error_mode);
+ return rust_decode_to_utf8("UTF-16LE"sv, input, error_mode);
}
ErrorOr<size_t> UTF16LEDecoder::length_in_utf16_code_units(StringView input)
{
- return rust_length_in_utf16_code_units("UTF-16LE"sv, input, IgnoreBOM::Yes);
+ return rust_length_in_utf16_code_units("UTF-16LE"sv, input);
}
// https://infra.spec.whatwg.org/#isomorphic-decode
diff --git a/Libraries/LibTextCodec/Decoder.h b/Libraries/LibTextCodec/Decoder.h
index 5794786a503..b3fb6ff23fa 100644
--- a/Libraries/LibTextCodec/Decoder.h
+++ b/Libraries/LibTextCodec/Decoder.h
@@ -35,9 +35,10 @@ enum class ErrorMode {
Fatal,
};
+// NB: A decoder never removes a BOM. The decode algorithms below do that.
class TEXTCODEC_API Decoder {
public:
- virtual ErrorOr<String> to_utf8(StringView, IgnoreBOM, ErrorMode);
+ virtual ErrorOr<String> to_utf8(StringView, ErrorMode = ErrorMode::Replacement);
virtual ErrorOr<Utf16String> to_utf16(StringView);
virtual ErrorOr<size_t> length_in_utf16_code_units(StringView);
ErrorOr<void> process_code_points(StringView, Function<ErrorOr<void>(u32)>);
@@ -74,14 +75,13 @@ TEXTCODEC_API Optional<Decoder&> decoder_for(Utf16View encoding);
TEXTCODEC_API Optional<StringView> get_standardized_encoding(StringView encoding);
TEXTCODEC_API Optional<StringView> get_standardized_encoding(Utf16View encoding);
-// This returns the appropriate Unicode decoder for the sniffed BOM or nothing if there is no appropriate decoder.
-TEXTCODEC_API Optional<Decoder&> bom_sniff_to_decoder(StringView);
+// https://encoding.spec.whatwg.org/#decode
+TEXTCODEC_API ErrorOr<String> decode_to_utf8(StringView, Decoder& fallback_decoder, ErrorMode = ErrorMode::Replacement);
+TEXTCODEC_API ErrorOr<Utf16String> decode_to_utf16(StringView, Decoder& fallback_decoder);
+TEXTCODEC_API ErrorOr<size_t> decoded_length_in_utf16_code_units(StringView, Decoder& fallback_decoder);
-// NOTE: This has an obnoxious name to discourage usage. Only use this if you absolutely must! For example, XHR in LibWeb uses this.
-// 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.
-TEXTCODEC_API ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView);
-TEXTCODEC_API ErrorOr<Utf16String> convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView);
-TEXTCODEC_API ErrorOr<size_t> convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView);
+// https://encoding.spec.whatwg.org/#utf-8-decode
+TEXTCODEC_API ErrorOr<Utf16String> utf8_decode_to_utf16(StringView);
TEXTCODEC_API StringView get_output_encoding(StringView encoding);
diff --git a/Libraries/LibTextCodec/Rust/src/lib.rs b/Libraries/LibTextCodec/Rust/src/lib.rs
index b9af551a650..78c9bedf26d 100644
--- a/Libraries/LibTextCodec/Rust/src/lib.rs
+++ b/Libraries/LibTextCodec/Rust/src/lib.rs
@@ -100,7 +100,6 @@ pub unsafe extern "C" fn textcodec_rust_decode_to_utf8(
encoding_label_len: usize,
input: *const u8,
input_len: usize,
- remove_bom: bool,
fatal: bool,
ctx: *mut c_void,
on_bytes: FfiBytesFn,
@@ -117,11 +116,7 @@ pub unsafe extern "C" fn textcodec_rust_decode_to_utf8(
return false;
};
- let (output, had_errors) = if remove_bom {
- encoding.decode_with_bom_removal(input)
- } else {
- encoding.decode_without_bom_handling(input)
- };
+ let (output, had_errors) = encoding.decode_without_bom_handling(input);
if fatal && had_errors {
return false;
}
diff --git a/Libraries/LibWeb/CSS/Parser/Helpers.cpp b/Libraries/LibWeb/CSS/Parser/Helpers.cpp
index d6bc0c5b941..83c072f8cdd 100644
--- a/Libraries/LibWeb/CSS/Parser/Helpers.cpp
+++ b/Libraries/LibWeb/CSS/Parser/Helpers.cpp
@@ -171,7 +171,7 @@ ErrorOr<Utf16String> css_decode_bytes(Optional<StringView> const& environment_en
return Error::from_string_literal("No Decoder found");
}
// 2. Decode stylesheet’s stream of bytes with fallback encoding fallback, and return the result.
- return TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, encoded_string);
+ return TextCodec::decode_to_utf16(encoded_string, *decoder);
}
bool is_valid_animation_name_custom_ident(Utf16View ident)
diff --git a/Libraries/LibWeb/Clipboard/Clipboard.cpp b/Libraries/LibWeb/Clipboard/Clipboard.cpp
index 2e512451fea..b1786386687 100644
--- a/Libraries/LibWeb/Clipboard/Clipboard.cpp
+++ b/Libraries/LibWeb/Clipboard/Clipboard.cpp
@@ -396,7 +396,7 @@ void Clipboard::read_text(JS::Realm& realm, GC::Ref<WebIDL::Promise> promise)
// 2. Return p.
auto decoder = TextCodec::decoder_for("UTF-8"sv);
- auto string = MUST(TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, *data));
+ auto string = MUST(TextCodec::decode_to_utf8(*data, *decoder));
WebIDL::resolve_promise(promise, JS::PrimitiveString::create(realm.vm(), Utf16String::from_utf8(string)));
return;
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cpp
index f83058634e8..b14d8b1c52f 100644
--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp
+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp
@@ -84,10 +84,10 @@ static void convert_to_xml_error_document(DOM::Document& document, Utf16String e
bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<StringView> content_encoding)
{
- Optional<TextCodec::Decoder&> decoder = TextCodec::bom_sniff_to_decoder(data);
+ Optional<TextCodec::Decoder&> decoder;
// The actual HTTP headers and other metadata, not the headers as mutated or implied by the algorithms given in this specification,
// are the ones that must be used when determining the character encoding according to the rules given in the above specifications.
- if (!decoder.has_value() && content_encoding.has_value())
+ if (content_encoding.has_value())
decoder = TextCodec::decoder_for(*content_encoding);
// https://www.w3.org/TR/xml/#charencoding
// [...] it is a fatal error [...] for an entity which begins with neither a Byte Order Mark nor an encoding
@@ -96,7 +96,7 @@ bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optiona
decoder = TextCodec::decoder_for("UTF-8"sv);
VERIFY(decoder.has_value());
// Well-formed XML documents contain only properly encoded characters
- auto source_or_error = decoder->to_utf8(data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal);
+ auto source_or_error = TextCodec::decode_to_utf8(data, *decoder, TextCodec::ErrorMode::Fatal);
if (source_or_error.is_error()) {
convert_to_xml_error_document(document, "XML Document contains improperly-encoded characters"_utf16);
return false;
@@ -201,10 +201,10 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_xml_document(HTML::Navig
content_encoding = *maybe_encoding;
auto process_body = GC::create_function(GC::Heap::the(), [document, url = navigation_params.response->url().value(), content_encoding = move(content_encoding), mime = type](ByteBuffer data) {
- Optional<TextCodec::Decoder&> decoder = TextCodec::bom_sniff_to_decoder(data);
+ Optional<TextCodec::Decoder&> decoder;
// The actual HTTP headers and other metadata, not the headers as mutated or implied by the algorithms given in this specification,
// are the ones that must be used when determining the character encoding according to the rules given in the above specifications.
- if (!decoder.has_value() && content_encoding.has_value())
+ if (content_encoding.has_value())
decoder = TextCodec::decoder_for(content_encoding->bytes_as_string_view());
// https://www.w3.org/TR/xml/#charencoding
// [...] it is a fatal error [...] for an entity which begins with neither a Byte Order Mark nor an encoding
@@ -213,7 +213,7 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_xml_document(HTML::Navig
decoder = TextCodec::decoder_for("UTF-8"sv);
VERIFY(decoder.has_value());
// Well-formed XML documents contain only properly encoded characters
- auto source = decoder->to_utf8(data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal);
+ auto source = TextCodec::decode_to_utf8(data, *decoder, TextCodec::ErrorMode::Fatal);
if (source.is_error()) {
// FIXME: Insert error message into the document.
dbgln("Failed to decode XML document: {}", source.error());
@@ -292,7 +292,7 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_text_document(HTML::Navi
parser->tokenizer().switch_to(HTML::HTMLTokenizer::State::PLAINTEXT);
auto decoder = TextCodec::decoder_for(encoding);
VERIFY(decoder.has_value());
- auto decoded = TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, data).release_value_but_fixme_should_propagate_errors();
+ auto decoded = TextCodec::decode_to_utf16(data, *decoder).release_value_but_fixme_should_propagate_errors();
parser->tokenizer().insert_input_at_insertion_point(decoded);
parser->tokenizer().insert_eof();
parser->run(url);
diff --git a/Libraries/LibWeb/Fetch/Body.cpp b/Libraries/LibWeb/Fetch/Body.cpp
index 1e45a7b25a9..de7652c8c46 100644
--- a/Libraries/LibWeb/Fetch/Body.cpp
+++ b/Libraries/LibWeb/Fetch/Body.cpp
@@ -214,11 +214,8 @@ void BodyMixin::text(GC::Ref<WebIDL::Promise> promise) const
auto& realm = WebIDL::promise_realm(*promise);
// The text() method steps are to return the result of running consume body with this and UTF-8 decode.
consume_body(*this, promise, GC::create_function(GC::Heap::the(), [&realm](ByteBuffer bytes) -> WebIDL::ExceptionOr<JS::Value> {
- auto decoder = TextCodec::decoder_for("UTF-8"sv);
- VERIFY(decoder.has_value());
-
- auto utf8_text = MUST(decoder->to_utf8(bytes, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement));
- return JS::PrimitiveString::create(realm.vm(), Utf16String::from_utf8(utf8_text));
+ auto text = MUST(TextCodec::utf8_decode_to_utf16(bytes));
+ return JS::PrimitiveString::create(realm.vm(), move(text));
}));
}
diff --git a/Libraries/LibWeb/FileAPI/Blob.cpp b/Libraries/LibWeb/FileAPI/Blob.cpp
index b5a3d204b46..c894fd6b5ee 100644
--- a/Libraries/LibWeb/FileAPI/Blob.cpp
+++ b/Libraries/LibWeb/FileAPI/Blob.cpp
@@ -418,7 +418,7 @@ GC::Ref<WebIDL::Promise> Blob::text(JS::Object const& relevant_global_object)
auto const& array_buffer = static_cast<JS::ArrayBuffer const&>(object);
auto decoder = TextCodec::decoder_for("UTF-8"sv);
- auto utf8_text = TRY_OR_THROW_OOM(vm, TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, ReadonlyBytes { array_buffer.data_at(0), array_buffer.byte_length() }));
+ auto utf8_text = TRY_OR_THROW_OOM(vm, TextCodec::decode_to_utf8(ReadonlyBytes { array_buffer.data_at(0), array_buffer.byte_length() }, *decoder));
return JS::PrimitiveString::create(vm, Utf16String::from_utf8(utf8_text));
}));
}
diff --git a/Libraries/LibWeb/FileAPI/FileReader.cpp b/Libraries/LibWeb/FileAPI/FileReader.cpp
index c0eb1f7a639..5167e4053d7 100644
--- a/Libraries/LibWeb/FileAPI/FileReader.cpp
+++ b/Libraries/LibWeb/FileAPI/FileReader.cpp
@@ -172,7 +172,7 @@ WebIDL::ExceptionOr<FileReader::Result> FileReader::blob_package_data(ByteBuffer
// 5. Decode bytes using fallback encoding encoding, and return the result.
auto decoder = TextCodec::decoder_for(encoding.value_or("UTF-8"sv));
VERIFY(decoder.has_value());
- auto utf8_result = TRY_OR_THROW_OOM(JS::VM::the(), convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(decoder.value(), bytes));
+ auto utf8_result = TRY_OR_THROW_OOM(JS::VM::the(), TextCodec::decode_to_utf8(bytes, decoder.value()));
return Utf16String::from_utf8(utf8_result);
}
case Type::ArrayBuffer:
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index c4e749f2239..035ece5d915 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -166,7 +166,7 @@ static Utf16String decode_html_parser_input(StringView input, StringView encodin
{
auto decoder = TextCodec::decoder_for(encoding);
VERIFY(decoder.has_value());
- return MUST(TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, input));
+ return MUST(TextCodec::decode_to_utf16(input, *decoder));
}
extern "C" void ladybird_html_parser_log_parse_error(void*, u8 const*, size_t);
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index 3564dd0830c..81807714f5e 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -130,7 +130,7 @@ static BytecodeCacheSourceHash bytecode_cache_source_hash(ReadonlyBytes source_b
static ErrorOr<Utf16String> decode_source_text_to_utf16(TextCodec::Decoder& fallback_decoder, StringView input)
{
- return TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(fallback_decoder, input);
+ return TextCodec::decode_to_utf16(input, fallback_decoder);
}
static ErrorOr<Utf16String> decode_source_text_to_utf16(TextCodec::Decoder& fallback_decoder, ReadonlyBytes bytes)
@@ -714,7 +714,7 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& ur
// materialize a script straight from the validated cached bytecode without parsing or compiling.
if (bytecode.has_value()) {
auto source_encoding = ByteString { extracted_character_encoding };
- auto source_length = TextCodec::convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(*fallback_decoder, StringView { source_bytes }).release_value_but_fixme_should_propagate_errors();
+ auto source_length = TextCodec::decoded_length_in_utf16_code_units(StringView { source_bytes }, *fallback_decoder).release_value_but_fixme_should_propagate_errors();
prepare_bytecode_cache_off_thread(settings_object, *bytecode, JS::ProgramType::Script, source_length, *source_hash,
[response_url = move(response_url), response_url_string = move(response_url_string),
source_byte_storage = move(source_byte_storage),
@@ -1160,7 +1160,7 @@ void fetch_single_module_script(JS::Realm& realm,
if (bytecode.has_value()) {
auto decoder = TextCodec::decoder_for("UTF-8"sv);
VERIFY(decoder.has_value());
- auto source_length = TextCodec::convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, StringView { source_bytes }).release_value_but_fixme_should_propagate_errors();
+ auto source_length = TextCodec::decoded_length_in_utf16_code_units(StringView { source_bytes }, *decoder).release_value_but_fixme_should_propagate_errors();
prepare_bytecode_cache_off_thread(settings_object, *bytecode, JS::ProgramType::Module, source_length, *source_hash,
[url = move(url), url_string = move(url_string), response_url = move(response_url),
module_type_string = move(module_type_string),
diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
index ecec73ccabf..496a1a00a71 100644
--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
@@ -397,7 +397,7 @@ Utf16String XMLHttpRequest::get_text_response() const
// If we don't support the decoder yet, let's crash instead of attempting to return something, as the result would be incorrect and create obscure bugs.
VERIFY(decoder.has_value());
- return TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, m_received_bytes).release_value_but_fixme_should_propagate_errors();
+ return TextCodec::decode_to_utf16(m_received_bytes, *decoder).release_value_but_fixme_should_propagate_errors();
}
// https://xhr.spec.whatwg.org/#document-response
diff --git a/Libraries/LibWebView/Autocomplete.cpp b/Libraries/LibWebView/Autocomplete.cpp
index 441fa8b636d..70c990c59c9 100644
--- a/Libraries/LibWebView/Autocomplete.cpp
+++ b/Libraries/LibWebView/Autocomplete.cpp
@@ -556,7 +556,7 @@ ErrorOr<Vector<String>> Autocomplete::received_autocomplete_response(SearchSugge
if (!decoder.has_value())
decoder = TextCodec::decoder_for_exact_name("UTF-8"sv);
- auto decoded_response = TRY(decoder->to_utf8(response, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement));
+ auto decoded_response = TRY(TextCodec::decode_to_utf8(response, *decoder));
auto json = TRY(JsonValue::from_string(decoded_response));
switch (provider.response_format) {
diff --git a/Meta/Fuzzers/FuzzTextDecoder.cpp b/Meta/Fuzzers/FuzzTextDecoder.cpp
index f78385c178e..3125527ac7e 100644
--- a/Meta/Fuzzers/FuzzTextDecoder.cpp
+++ b/Meta/Fuzzers/FuzzTextDecoder.cpp
@@ -24,6 +24,6 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
if (!decoder.has_value())
return 0;
- (void)decoder->to_utf8(encoded_data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement);
+ (void)decoder->to_utf8(encoded_data);
return 0;
}
diff --git a/Services/RequestServer/Request.cpp b/Services/RequestServer/Request.cpp
index c8d0ad7cd75..cd3336ffb9d 100644
--- a/Services/RequestServer/Request.cpp
+++ b/Services/RequestServer/Request.cpp
@@ -1964,7 +1964,7 @@ size_t Request::on_header_received(void* buffer, size_t size, size_t nmemb, void
auto decoder = TextCodec::decoder_for_exact_name("ISO-8859-1"sv);
VERIFY(decoder.has_value());
- request.m_reason_phrase = MUST(decoder->to_utf8(reason_phrase, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement));
+ request.m_reason_phrase = MUST(decoder->to_utf8(reason_phrase));
}
}
diff --git a/Tests/LibTextCodec/TestTextDecoders.cpp b/Tests/LibTextCodec/TestTextDecoders.cpp
index 2b89f441c87..63065d526fb 100644
--- a/Tests/LibTextCodec/TestTextDecoders.cpp
+++ b/Tests/LibTextCodec/TestTextDecoders.cpp
@@ -37,13 +37,13 @@ TEST_CASE(test_utf8_decode)
// Bytes for U+1F600 GRINNING FACE
auto test_string = "\xf0\x9f\x98\x80"sv;
- EXPECT(!decoder.to_utf8(test_string, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal).is_error());
+ EXPECT(!decoder.to_utf8(test_string, TextCodec::ErrorMode::Fatal).is_error());
auto processed_code_points = process_code_points(decoder, test_string);
EXPECT(processed_code_points.size() == 1);
EXPECT(processed_code_points[0] == 0x1F600);
- EXPECT(MUST(decoder.to_utf8(test_string, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)) == test_string);
+ EXPECT(MUST(decoder.to_utf8(test_string)) == test_string);
}
TEST_CASE(test_utf8_process_code_points)
@@ -60,8 +60,8 @@ TEST_CASE(test_utf8_process_code_points_replaces_surrogates)
auto utf8_encoded_surrogate_bytes = Vector<u8> { 0xed, 0xa0, 0x80 };
auto utf8_encoded_surrogate = StringView(bytes(utf8_encoded_surrogate_bytes));
- EXPECT(decoder.to_utf8(utf8_encoded_surrogate, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal).is_error());
- EXPECT_EQ(MUST(decoder.to_utf8(utf8_encoded_surrogate, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"sv);
+ EXPECT(decoder.to_utf8(utf8_encoded_surrogate, TextCodec::ErrorMode::Fatal).is_error());
+ EXPECT_EQ(MUST(decoder.to_utf8(utf8_encoded_surrogate)), "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"sv);
EXPECT_EQ(process_code_points(decoder, utf8_encoded_surrogate), (Vector<u32> { 0xfffd, 0xfffd, 0xfffd }));
}
@@ -71,8 +71,8 @@ TEST_CASE(test_utf8_process_code_points_replaces_truncated_tail_as_single_error)
auto truncated_tail_bytes = Vector<u8> { 0xf0, 0x9f, 0x98 };
auto truncated_tail = StringView(bytes(truncated_tail_bytes));
- EXPECT(decoder.to_utf8(truncated_tail, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal).is_error());
- EXPECT_EQ(MUST(decoder.to_utf8(truncated_tail, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "\xef\xbf\xbd"sv);
+ EXPECT(decoder.to_utf8(truncated_tail, TextCodec::ErrorMode::Fatal).is_error());
+ EXPECT_EQ(MUST(decoder.to_utf8(truncated_tail)), "\xef\xbf\xbd"sv);
EXPECT_EQ(process_code_points(decoder, truncated_tail), (Vector<u32> { 0xfffd }));
}
@@ -82,8 +82,8 @@ TEST_CASE(test_utf8_process_code_points_replaces_overlong_sequences)
auto overlong_null_bytes = Vector<u8> { 0xc0, 0x80 };
auto overlong_null = StringView(bytes(overlong_null_bytes));
- EXPECT(decoder.to_utf8(overlong_null, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal).is_error());
- EXPECT_EQ(MUST(decoder.to_utf8(overlong_null, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "\xef\xbf\xbd\xef\xbf\xbd"sv);
+ EXPECT(decoder.to_utf8(overlong_null, TextCodec::ErrorMode::Fatal).is_error());
+ EXPECT_EQ(MUST(decoder.to_utf8(overlong_null)), "\xef\xbf\xbd\xef\xbf\xbd"sv);
EXPECT_EQ(process_code_points(decoder, overlong_null), (Vector<u32> { 0xfffd, 0xfffd }));
}
@@ -95,12 +95,12 @@ TEST_CASE(test_utf8_process_code_points_restores_invalid_second_byte)
auto out_of_range_four_byte_sequence_bytes = Vector<u8> { 0xf4, 0x90, 0x80, 0x80 };
auto out_of_range_four_byte_sequence = StringView(bytes(out_of_range_four_byte_sequence_bytes));
- EXPECT(decoder.to_utf8(overlong_three_byte_sequence, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal).is_error());
- EXPECT_EQ(MUST(decoder.to_utf8(overlong_three_byte_sequence, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"sv);
+ EXPECT(decoder.to_utf8(overlong_three_byte_sequence, TextCodec::ErrorMode::Fatal).is_error());
+ EXPECT_EQ(MUST(decoder.to_utf8(overlong_three_byte_sequence)), "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"sv);
EXPECT_EQ(process_code_points(decoder, overlong_three_byte_sequence), (Vector<u32> { 0xfffd, 0xfffd, 0xfffd }));
- EXPECT(decoder.to_utf8(out_of_range_four_byte_sequence, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal).is_error());
- EXPECT_EQ(MUST(decoder.to_utf8(out_of_range_four_byte_sequence, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"sv);
+ EXPECT(decoder.to_utf8(out_of_range_four_byte_sequence, TextCodec::ErrorMode::Fatal).is_error());
+ EXPECT_EQ(MUST(decoder.to_utf8(out_of_range_four_byte_sequence)), "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"sv);
EXPECT_EQ(process_code_points(decoder, out_of_range_four_byte_sequence), (Vector<u32> { 0xfffd, 0xfffd, 0xfffd, 0xfffd }));
}
@@ -110,7 +110,7 @@ TEST_CASE(test_utf16be_decode)
// This is the output of `python3 -c "print('säk😀'.encode('utf-16be'))"`.
auto test_string = "\x00s\x00\xe4\x00k\xd8=\xde\x00"sv;
- auto utf8 = MUST(decoder.to_utf8(test_string, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal));
+ auto utf8 = MUST(decoder.to_utf8(test_string, TextCodec::ErrorMode::Fatal));
EXPECT_EQ(utf8, "säk😀"sv);
}
@@ -121,7 +121,7 @@ TEST_CASE(test_utf16be_process_code_points)
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xfe, 0xff, 0x00, 'A', 0xd8, 0x3d, 0xde, 0x00 }))), (Vector<u32> { 0xfeff, 0x41, 0x1F600 }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xd8, 0x3d, 0x00, 'A', 0xde, 0x00 }))), (Vector<u32> { 0xfffd, 0x41, 0xfffd }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0x00, 'A', 0xff }))), (Vector<u32> { 0x41, 0xfffd }));
- EXPECT_EQ(MUST(decoder.to_utf8(StringView(bytes({ 0x00, 'A', 0xff })), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "A\xef\xbf\xbd"sv);
+ EXPECT_EQ(MUST(decoder.to_utf8(StringView(bytes({ 0x00, 'A', 0xff })))), "A\xef\xbf\xbd"sv);
}
TEST_CASE(test_streaming_decoder_utf8_mid_sequence)
@@ -140,7 +140,7 @@ TEST_CASE(test_streaming_decoder_finishes_incomplete_sequence)
auto incomplete_sequence = Vector<u8> { 0xc3 };
EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes(incomplete_sequence))), ""sv);
- EXPECT_EQ(MUST(streaming_decoder.finish()), MUST(decoder.to_utf8(StringView(bytes(incomplete_sequence)), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)));
+ EXPECT_EQ(MUST(streaming_decoder.finish()), MUST(decoder.to_utf8(StringView(bytes(incomplete_sequence)))));
}
TEST_CASE(test_streaming_decoder_utf8_invalid_second_byte_tail)
@@ -237,7 +237,7 @@ TEST_CASE(test_streaming_decoder_gb18030_four_byte_tail)
auto gb18030_sequence = Vector<u8> { 0x81, 0x30, 0x81, 0x30 };
EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 'a', 0x81, 0x30, 0x81 }))), "a"sv);
- EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0x30 }))), MUST(decoder.to_utf8(StringView(bytes(gb18030_sequence)), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)));
+ EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0x30 }))), MUST(decoder.to_utf8(StringView(bytes(gb18030_sequence)))));
EXPECT_EQ(MUST(streaming_decoder.finish()), ""sv);
}
@@ -247,12 +247,12 @@ TEST_CASE(test_streaming_decoder_big5_overlapping_trail)
auto streaming_decoder = TextCodec::StreamingDecoder { "Big5"sv, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement };
auto big5_sequence = Vector<u8> { 0xa4, 0xa4 };
- EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes(big5_sequence))), MUST(decoder.to_utf8(StringView(bytes(big5_sequence)), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)));
+ EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes(big5_sequence))), MUST(decoder.to_utf8(StringView(bytes(big5_sequence)))));
EXPECT_EQ(MUST(streaming_decoder.finish()), ""sv);
auto streaming_decoder_for_split_input = TextCodec::StreamingDecoder { "Big5"sv, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement };
EXPECT_EQ(MUST(streaming_decoder_for_split_input.to_utf8(bytes({ 0xa4 }))), ""sv);
- EXPECT_EQ(MUST(streaming_decoder_for_split_input.to_utf8(bytes({ 0xa4 }))), MUST(decoder.to_utf8(StringView(bytes(big5_sequence)), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)));
+ EXPECT_EQ(MUST(streaming_decoder_for_split_input.to_utf8(bytes({ 0xa4 }))), MUST(decoder.to_utf8(StringView(bytes(big5_sequence)))));
EXPECT_EQ(MUST(streaming_decoder_for_split_input.finish()), ""sv);
}
@@ -263,7 +263,7 @@ TEST_CASE(test_streaming_decoder_euc_jp_three_byte_tail)
auto euc_jp_sequence = Vector<u8> { 0x8f, 0xa2, 0xaf };
EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0x8f, 0xa2 }))), ""sv);
- EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0xaf }))), MUST(decoder.to_utf8(StringView(bytes(euc_jp_sequence)), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)));
+ EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0xaf }))), MUST(decoder.to_utf8(StringView(bytes(euc_jp_sequence)))));
EXPECT_EQ(MUST(streaming_decoder.finish()), ""sv);
}
@@ -274,7 +274,7 @@ TEST_CASE(test_streaming_decoder_shift_jis_tail)
auto shift_jis_sequence = Vector<u8> { 0x82, 0xa0 };
EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0x82 }))), ""sv);
- EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0xa0 }))), MUST(decoder.to_utf8(StringView(bytes(shift_jis_sequence)), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)));
+ EXPECT_EQ(MUST(streaming_decoder.to_utf8(bytes({ 0xa0 }))), MUST(decoder.to_utf8(StringView(bytes(shift_jis_sequence)))));
EXPECT_EQ(MUST(streaming_decoder.finish()), ""sv);
}
@@ -284,7 +284,7 @@ TEST_CASE(test_utf16le_decode)
// This is the output of `python3 -c "print('säk😀'.encode('utf-16le'))"`.
auto test_string = "s\x00\xe4\x00k\x00=\xd8\x00\xde"sv;
- auto utf8 = MUST(decoder.to_utf8(test_string, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal));
+ auto utf8 = MUST(decoder.to_utf8(test_string, TextCodec::ErrorMode::Fatal));
EXPECT_EQ(utf8, "säk😀"sv);
}
@@ -295,7 +295,7 @@ TEST_CASE(test_utf16le_process_code_points)
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xff, 0xfe, 'A', 0x00, 0x3d, 0xd8, 0x00, 0xde }))), (Vector<u32> { 0xfeff, 0x41, 0x1F600 }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0x3d, 0xd8, 'A', 0x00, 0x00, 0xde }))), (Vector<u32> { 0xfffd, 0x41, 0xfffd }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 'A', 0x00, 0xff }))), (Vector<u32> { 0x41, 0xfffd }));
- EXPECT_EQ(MUST(decoder.to_utf8(StringView(bytes({ 'A', 0x00, 0xff })), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "A\xef\xbf\xbd"sv);
+ EXPECT_EQ(MUST(decoder.to_utf8(StringView(bytes({ 'A', 0x00, 0xff })))), "A\xef\xbf\xbd"sv);
}
TEST_CASE(test_decode_removes_only_the_sniffed_byte_order_mark)
@@ -310,8 +310,8 @@ TEST_CASE(test_decode_removes_only_the_sniffed_byte_order_mark)
for (auto const& input_bytes : inputs) {
auto input = StringView(bytes(input_bytes));
- EXPECT_EQ(MUST(TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(fallback_decoder, input)), u"\uFEFFA"sv);
- EXPECT_EQ(MUST(TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(fallback_decoder, input)), "\uFEFFA"sv);
- EXPECT_EQ(MUST(TextCodec::convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(fallback_decoder, input)), 2u);
+ EXPECT_EQ(MUST(TextCodec::decode_to_utf16(input, fallback_decoder)), u"\uFEFFA"sv);
+ EXPECT_EQ(MUST(TextCodec::decode_to_utf8(input, fallback_decoder)), "\uFEFFA"sv);
+ EXPECT_EQ(MUST(TextCodec::decoded_length_in_utf16_code_units(input, fallback_decoder)), 2u);
}
}