Skip to content

Truly independent web browser

LibTextCodec: Remove length_in_utf16_code_units()

Andreas Kling5ec8eab9d37f
diff --git a/Libraries/LibTextCodec/Decoder.cpp b/Libraries/LibTextCodec/Decoder.cpp
index cc9b578b2f2..300589fe980 100644
--- a/Libraries/LibTextCodec/Decoder.cpp
+++ b/Libraries/LibTextCodec/Decoder.cpp
@@ -141,20 +141,6 @@ ErrorOr<Utf16String> Decoder::to_utf16(ReadonlyBytes input) const
return context.builder.to_string();
}
-ErrorOr<size_t> Decoder::length_in_utf16_code_units(ReadonlyBytes input) const
-{
- size_t length = 0;
- auto succeeded = FFI::textcodec_rust_decoded_length_in_utf16_code_units(
- reinterpret_cast<u8 const*>(m_encoding.characters_without_null_termination()),
- m_encoding.length(),
- input.data(),
- input.size(),
- &length);
- if (!succeeded)
- return Error::from_string_literal("Failed to decode input");
- return length;
-}
-
// https://encoding.spec.whatwg.org/#bom-sniff
static Optional<StringView> bom_sniff(ReadonlyBytes input)
{
@@ -210,13 +196,6 @@ ErrorOr<Utf16String> decode_to_utf16(ReadonlyBytes input, Decoder fallback_decod
return decoder.to_utf16(input);
}
-// https://encoding.spec.whatwg.org/#decode
-ErrorOr<size_t> decoded_length_in_utf16_code_units(ReadonlyBytes input, Decoder fallback_decoder)
-{
- auto decoder = decoder_for_decode(fallback_decoder, input);
- return decoder.length_in_utf16_code_units(input);
-}
-
// https://encoding.spec.whatwg.org/#utf-8-decode
ErrorOr<Utf16String> utf8_decode_to_utf16(ReadonlyBytes input)
{
diff --git a/Libraries/LibTextCodec/Decoder.h b/Libraries/LibTextCodec/Decoder.h
index e6ff9ed6000..c52d395e7db 100644
--- a/Libraries/LibTextCodec/Decoder.h
+++ b/Libraries/LibTextCodec/Decoder.h
@@ -45,7 +45,6 @@ class TEXTCODEC_API Decoder {
public:
ErrorOr<String> to_utf8(ReadonlyBytes, ErrorMode = ErrorMode::Replacement) const;
ErrorOr<Utf16String> to_utf16(ReadonlyBytes) const;
- ErrorOr<size_t> length_in_utf16_code_units(ReadonlyBytes) const;
private:
friend TEXTCODEC_API Optional<Decoder> decoder_for(StringView);
@@ -80,7 +79,6 @@ TEXTCODEC_API Optional<StringView> get_standardized_encoding(Utf16View encoding)
// https://encoding.spec.whatwg.org/#decode
TEXTCODEC_API ErrorOr<String> decode_to_utf8(ReadonlyBytes, Decoder fallback_decoder, ErrorMode = ErrorMode::Replacement);
TEXTCODEC_API ErrorOr<Utf16String> decode_to_utf16(ReadonlyBytes, Decoder fallback_decoder);
-TEXTCODEC_API ErrorOr<size_t> decoded_length_in_utf16_code_units(ReadonlyBytes, Decoder fallback_decoder);
// https://encoding.spec.whatwg.org/#utf-8-decode
TEXTCODEC_API ErrorOr<Utf16String> utf8_decode_to_utf16(ReadonlyBytes);
diff --git a/Libraries/LibTextCodec/Rust/src/lib.rs b/Libraries/LibTextCodec/Rust/src/lib.rs
index 017b9c52a45..878438d0929 100644
--- a/Libraries/LibTextCodec/Rust/src/lib.rs
+++ b/Libraries/LibTextCodec/Rust/src/lib.rs
@@ -19,7 +19,6 @@ pub use streaming_decoder::{DecodingFailed, StreamingDecoder};
use crate::rust_panic::abort_on_panic;
use ak::Utf16StringUnits;
-use encoding_rs::CoderResult;
use encoding_rs::EncoderResult;
use encoding_rs::Encoding;
use std::ffi::c_void;
@@ -161,50 +160,6 @@ pub unsafe extern "C" fn textcodec_rust_decode_to_utf16(
}
}
-/// # Safety
-/// - `encoding_label`/`encoding_label_len` and `input`/`input_len` must be valid byte slices.
-/// - `out_length` must be a valid writable pointer.
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn textcodec_rust_decoded_length_in_utf16_code_units(
- encoding_label: *const u8,
- encoding_label_len: usize,
- input: *const u8,
- input_len: usize,
- out_length: *mut usize,
-) -> bool {
- unsafe {
- abort_on_panic(|| {
- let Some(label) = bytes_from_raw(encoding_label, encoding_label_len) else {
- return false;
- };
- let Some(input) = bytes_from_raw(input, input_len) else {
- return false;
- };
- let Some(encoding) = Encoding::for_label(label) else {
- return false;
- };
- if out_length.is_null() {
- eprintln!("textcodec_rust_decoded_length_in_utf16_code_units: null output pointer");
- return false;
- }
-
- let mut decoder = encoding.new_decoder_without_bom_handling();
- let mut output = [0u16; 4096];
- let mut total_read = 0;
- let mut length = 0;
- loop {
- let (result, read, written, _) = decoder.decode_to_utf16(&input[total_read..], &mut output, true);
- total_read += read;
- length += written;
- if result == CoderResult::InputEmpty {
- *out_length = length;
- return true;
- }
- }
- })
- }
-}
-
/// # Safety
/// - `encoding_label`/`encoding_label_len` must be a valid byte slice.
/// - The returned pointer must be freed with `textcodec_rust_streaming_decoder_free`.
diff --git a/Tests/LibTextCodec/TestTextDecoders.cpp b/Tests/LibTextCodec/TestTextDecoders.cpp
index 293f08f9f3d..239ac0b8c4b 100644
--- a/Tests/LibTextCodec/TestTextDecoders.cpp
+++ b/Tests/LibTextCodec/TestTextDecoders.cpp
@@ -24,7 +24,6 @@ static TextCodec::Decoder decoder_for(StringView encoding)
static Vector<u32> process_code_points(TextCodec::Decoder decoder, ReadonlyBytes input)
{
auto utf16 = MUST(decoder.to_utf16(input));
- EXPECT_EQ(MUST(decoder.length_in_utf16_code_units(input)), utf16.length_in_code_units());
EXPECT_EQ(Utf16String::from_utf8(MUST(decoder.to_utf8(input))), utf16);
Vector<u32> code_points;
@@ -308,6 +307,5 @@ TEST_CASE(test_decode_removes_only_the_sniffed_byte_order_mark)
auto input = bytes(input_bytes);
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);
}
}