Skip to content

Truly independent web browser

LibTextCodec: Use one encoder for every encoding

Shannon Bootha4285b62bd4b
diff --git a/Libraries/LibTextCodec/Encoder.cpp b/Libraries/LibTextCodec/Encoder.cpp
index 0e7dc6cf501..28788bc4914 100644
--- a/Libraries/LibTextCodec/Encoder.cpp
+++ b/Libraries/LibTextCodec/Encoder.cpp
@@ -16,208 +16,53 @@ namespace TextCodec {
namespace {
-class RustEncoder final : public Encoder {
-public:
- explicit RustEncoder(StringView encoding)
- : m_encoding(encoding)
- {
- }
-
- virtual ErrorOr<void> process(Utf8View input, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
-
-private:
- StringView m_encoding;
-};
-
-class UTF8Encoder final : public Encoder {
-public:
- virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
-};
-
-UTF8Encoder s_utf8_encoder;
-
-RustEncoder s_gb18030_encoder { "gb18030"sv };
-RustEncoder s_gbk_encoder { "GBK"sv };
-RustEncoder s_big5_encoder { "Big5"sv };
-RustEncoder s_euc_jp_encoder { "EUC-JP"sv };
-RustEncoder s_iso_2022_jp_encoder { "ISO-2022-JP"sv };
-RustEncoder s_shift_jis_encoder { "Shift_JIS"sv };
-RustEncoder s_euc_kr_encoder { "EUC-KR"sv };
-RustEncoder s_ibm866_encoder { "IBM866"sv };
-RustEncoder s_latin2_encoder { "ISO-8859-2"sv };
-RustEncoder s_latin3_encoder { "ISO-8859-3"sv };
-RustEncoder s_latin4_encoder { "ISO-8859-4"sv };
-RustEncoder s_latin_cyrillic_encoder { "ISO-8859-5"sv };
-RustEncoder s_latin_arabic_encoder { "ISO-8859-6"sv };
-RustEncoder s_latin_greek_encoder { "ISO-8859-7"sv };
-RustEncoder s_latin_hebrew_encoder { "ISO-8859-8"sv };
-RustEncoder s_latin6_encoder { "ISO-8859-10"sv };
-RustEncoder s_latin7_encoder { "ISO-8859-13"sv };
-RustEncoder s_latin8_encoder { "ISO-8859-14"sv };
-RustEncoder s_latin9_encoder { "ISO-8859-15"sv };
-RustEncoder s_latin10_encoder { "ISO-8859-16"sv };
-RustEncoder s_centraleurope_encoder { "windows-1250"sv };
-RustEncoder s_cyrillic_encoder { "windows-1251"sv };
-RustEncoder s_hebrew_encoder { "windows-1255"sv };
-RustEncoder s_koi8r_encoder { "KOI8-R"sv };
-RustEncoder s_koi8u_encoder { "KOI8-U"sv };
-RustEncoder s_mac_roman_encoder { "macintosh"sv };
-RustEncoder s_windows874_encoder { "windows-874"sv };
-RustEncoder s_windows1252_encoder { "windows-1252"sv };
-RustEncoder s_windows1253_encoder { "windows-1253"sv };
-RustEncoder s_turkish_encoder { "windows-1254"sv };
-RustEncoder s_windows1256_encoder { "windows-1256"sv };
-RustEncoder s_windows1257_encoder { "windows-1257"sv };
-RustEncoder s_windows1258_encoder { "windows-1258"sv };
-RustEncoder s_mac_cyrillic_encoder { "x-mac-cyrillic"sv };
-RustEncoder s_x_user_defined_encoder { "x-user-defined"sv };
-
struct EncodeContext {
- Function<ErrorOr<void>(u8)>* on_byte { nullptr };
- Function<ErrorOr<void>(u32)>* on_error { nullptr };
+ Function<ErrorOr<void>(ReadonlyBytes)>& on_bytes;
+ Function<ErrorOr<void>(u32)>& on_error;
ErrorOr<void> result {};
};
-static void append_encoded_bytes(void* context, u8 const* data, size_t length)
+void append_encoded_bytes(void* context, u8 const* data, size_t length)
{
auto& encode_context = *static_cast<EncodeContext*>(context);
if (encode_context.result.is_error())
return;
-
- for (size_t i = 0; i < length; ++i) {
- encode_context.result = (*encode_context.on_byte)(data[i]);
- if (encode_context.result.is_error())
- return;
- }
+ encode_context.result = encode_context.on_bytes({ data, length });
}
-static void report_unmappable_code_point(void* context, u32 code_point)
+void report_unmappable_code_point(void* context, u32 code_point)
{
auto& encode_context = *static_cast<EncodeContext*>(context);
if (encode_context.result.is_error())
return;
-
- encode_context.result = (*encode_context.on_error)(code_point);
-}
-
-ErrorOr<void> rust_encode(StringView encoding, Utf8View input, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error)
-{
- auto input_view = StringView { input.bytes(), input.byte_length() };
- EncodeContext context {
- .on_byte = &on_byte,
- .on_error = &on_error,
- };
- auto succeeded = FFI::textcodec_rust_encode_from_utf8(
- reinterpret_cast<u8 const*>(encoding.characters_without_null_termination()),
- encoding.length(),
- reinterpret_cast<u8 const*>(input_view.characters_without_null_termination()),
- input_view.length(),
- &context,
- append_encoded_bytes,
- report_unmappable_code_point);
- if (!succeeded)
- return Error::from_errno(EINVAL);
- TRY(context.result);
- return {};
+ encode_context.result = encode_context.on_error(code_point);
}
}
-Optional<Encoder&> encoder_for_exact_name(StringView encoding)
-{
- if (encoding.equals_ignoring_ascii_case("utf-8"sv))
- return s_utf8_encoder;
- if (encoding.equals_ignoring_ascii_case("big5"sv))
- return s_big5_encoder;
- if (encoding.equals_ignoring_ascii_case("euc-jp"sv))
- return s_euc_jp_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-2022-jp"sv))
- return s_iso_2022_jp_encoder;
- if (encoding.equals_ignoring_ascii_case("shift_jis"sv))
- return s_shift_jis_encoder;
- if (encoding.equals_ignoring_ascii_case("euc-kr"sv))
- return s_euc_kr_encoder;
- if (encoding.equals_ignoring_ascii_case("gb18030"sv))
- return s_gb18030_encoder;
- if (encoding.equals_ignoring_ascii_case("gbk"sv))
- return s_gbk_encoder;
- if (encoding.equals_ignoring_ascii_case("ibm866"sv))
- return s_ibm866_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-2"sv))
- return s_latin2_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-3"sv))
- return s_latin3_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-4"sv))
- return s_latin4_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-5"sv))
- return s_latin_cyrillic_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-6"sv))
- return s_latin_arabic_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-7"sv))
- return s_latin_greek_encoder;
- if (encoding.is_one_of_ignoring_ascii_case("iso-8859-8"sv, "iso-8859-8-i"sv))
- return s_latin_hebrew_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-10"sv))
- return s_latin6_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-13"sv))
- return s_latin7_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-14"sv))
- return s_latin8_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-15"sv))
- return s_latin9_encoder;
- if (encoding.equals_ignoring_ascii_case("iso-8859-16"sv))
- return s_latin10_encoder;
- if (encoding.equals_ignoring_ascii_case("koi8-r"sv))
- return s_koi8r_encoder;
- if (encoding.equals_ignoring_ascii_case("koi8-u"sv))
- return s_koi8u_encoder;
- if (encoding.equals_ignoring_ascii_case("macintosh"sv))
- return s_mac_roman_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-874"sv))
- return s_windows874_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1250"sv))
- return s_centraleurope_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1251"sv))
- return s_cyrillic_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1252"sv))
- return s_windows1252_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1253"sv))
- return s_windows1253_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1254"sv))
- return s_turkish_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1255"sv))
- return s_hebrew_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1256"sv))
- return s_windows1256_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1257"sv))
- return s_windows1257_encoder;
- if (encoding.equals_ignoring_ascii_case("windows-1258"sv))
- return s_windows1258_encoder;
- if (encoding.equals_ignoring_ascii_case("x-mac-cyrillic"sv))
- return s_mac_cyrillic_encoder;
- if (encoding.equals_ignoring_ascii_case("x-user-defined"sv))
- return s_x_user_defined_encoder;
- dbgln("TextCodec: No encoder implemented for encoding '{}'", encoding);
- return {};
-}
-
-Optional<Encoder&> encoder_for(StringView label)
+Optional<Encoder> encoder_for(StringView label)
{
auto encoding = get_standardized_encoding(label);
- return encoding.has_value() ? encoder_for_exact_name(encoding.value()) : Optional<Encoder&> {};
+ return encoding.has_value() ? Encoder { get_output_encoding(*encoding) } : Optional<Encoder> {};
}
-ErrorOr<void> RustEncoder::process(Utf8View input, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error)
+ErrorOr<void> Encoder::process(Utf8View input, Function<ErrorOr<void>(ReadonlyBytes)> on_bytes, Function<ErrorOr<void>(u32)> on_error) const
{
- return rust_encode(m_encoding, input, move(on_byte), move(on_error));
-}
+ if (m_output_encoding == "UTF-8"sv)
+ return on_bytes({ input.bytes(), input.byte_length() });
-ErrorOr<void> UTF8Encoder::process(Utf8View input, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)>)
-{
- ReadonlyBytes bytes { input.bytes(), input.byte_length() };
- for (auto byte : bytes)
- TRY(on_byte(byte));
- return {};
+ EncodeContext context { .on_bytes = on_bytes, .on_error = on_error };
+ auto succeeded = FFI::textcodec_rust_encode_from_utf8(
+ reinterpret_cast<u8 const*>(m_output_encoding.characters_without_null_termination()),
+ m_output_encoding.length(),
+ input.bytes(),
+ input.byte_length(),
+ &context,
+ append_encoded_bytes,
+ report_unmappable_code_point);
+ if (!succeeded)
+ return Error::from_errno(EINVAL);
+ return move(context.result);
}
// https://infra.spec.whatwg.org/#isomorphic-encode
diff --git a/Libraries/LibTextCodec/Encoder.h b/Libraries/LibTextCodec/Encoder.h
index 266bbcac802..7bd67630cec 100644
--- a/Libraries/LibTextCodec/Encoder.h
+++ b/Libraries/LibTextCodec/Encoder.h
@@ -13,16 +13,25 @@
namespace TextCodec {
+class Encoder;
+
+TEXTCODEC_API Optional<Encoder> encoder_for(StringView label);
+
+// NB: An encoder encodes with the output encoding of its encoding, https://encoding.spec.whatwg.org/#get-an-output-encoding.
class TEXTCODEC_API Encoder {
public:
- virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) = 0;
+ ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(ReadonlyBytes)> on_bytes, Function<ErrorOr<void>(u32)> on_error) const;
-protected:
- virtual ~Encoder() = default;
-};
+private:
+ friend TEXTCODEC_API Optional<Encoder> encoder_for(StringView);
-TEXTCODEC_API Optional<Encoder&> encoder_for_exact_name(StringView encoding);
-TEXTCODEC_API Optional<Encoder&> encoder_for(StringView label);
+ explicit Encoder(StringView output_encoding)
+ : m_output_encoding(output_encoding)
+ {
+ }
+
+ StringView m_output_encoding;
+};
TEXTCODEC_API ByteString isomorphic_encode(StringView);
TEXTCODEC_API ByteString isomorphic_encode(Utf16View);
diff --git a/Libraries/LibURL/Parser.cpp b/Libraries/LibURL/Parser.cpp
index f96b50712b8..a363e2cbdb7 100644
--- a/Libraries/LibURL/Parser.cpp
+++ b/Libraries/LibURL/Parser.cpp
@@ -26,7 +26,7 @@ Optional<Host> Parser::parse_host(Utf16View input, bool is_opaque)
}
// https://url.spec.whatwg.org/#string-percent-encode-after-encoding
-String Parser::percent_encode_after_encoding(TextCodec::Encoder& encoder, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus)
+String Parser::percent_encode_after_encoding(TextCodec::Encoder const& encoder, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus)
{
// 1. Let encodeOutput be an empty I/O queue.
StringBuilder output;
@@ -36,26 +36,28 @@ String Parser::percent_encode_after_encoding(TextCodec::Encoder& encoder, String
Utf8View(input),
// 3. For each byte of encodeOutput converted to a byte sequence:
- [&](u8 byte) -> ErrorOr<void> {
- // 1. If spaceAsPlus is true and byte is 0x20 (SP), then append U+002B (+) to output and continue.
- if (space_as_plus && byte == ' ') {
- output.append('+');
- return {};
- }
-
- // 2. Let isomorph be a code point whose value is byte’s value.
- u32 isomorph = byte;
-
- // 3. Assert: percentEncodeSet includes all non-ASCII code points.
-
- // 4. If isomorphic is not in percentEncodeSet, then append isomorph to output.
- if (!code_point_is_in_percent_encode_set(isomorph, percent_encode_set)) {
- output.append_code_point(isomorph);
- }
-
- // 5. Otherwise, percent-encode byte and append the result to output.
- else {
- output.appendff("%{:02X}", byte);
+ [&](ReadonlyBytes bytes) -> ErrorOr<void> {
+ for (auto byte : bytes) {
+ // 1. If spaceAsPlus is true and byte is 0x20 (SP), then append U+002B (+) to output and continue.
+ if (space_as_plus && byte == ' ') {
+ output.append('+');
+ continue;
+ }
+
+ // 2. Let isomorph be a code point whose value is byte’s value.
+ u32 isomorph = byte;
+
+ // 3. Assert: percentEncodeSet includes all non-ASCII code points.
+
+ // 4. If isomorphic is not in percentEncodeSet, then append isomorph to output.
+ if (!code_point_is_in_percent_encode_set(isomorph, percent_encode_set)) {
+ output.append_code_point(isomorph);
+ }
+
+ // 5. Otherwise, percent-encode byte and append the result to output.
+ else {
+ output.appendff("%{:02X}", byte);
+ }
}
return {};
diff --git a/Libraries/LibURL/Parser.h b/Libraries/LibURL/Parser.h
index d27583410f5..9358dd44c00 100644
--- a/Libraries/LibURL/Parser.h
+++ b/Libraries/LibURL/Parser.h
@@ -48,7 +48,7 @@ public:
static bool basic_parse(Utf16View input, URL& url, State state_override, Optional<StringView> encoding = {});
// https://url.spec.whatwg.org/#string-percent-encode-after-encoding
- static String percent_encode_after_encoding(TextCodec::Encoder&, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus = false);
+ static String percent_encode_after_encoding(TextCodec::Encoder const&, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus = false);
static Optional<Host> parse_host(StringView input, bool is_opaque = false);
static Optional<Host> parse_host(Utf16View input, bool is_opaque = false);
diff --git a/Libraries/LibWeb/DOMURL/URLSearchParams.cpp b/Libraries/LibWeb/DOMURL/URLSearchParams.cpp
index 6bd248256c1..5272a47d254 100644
--- a/Libraries/LibWeb/DOMURL/URLSearchParams.cpp
+++ b/Libraries/LibWeb/DOMURL/URLSearchParams.cpp
@@ -40,10 +40,7 @@ String url_encode(Vector<QueryParam> const& tuples, StringView encoding)
encoding = TextCodec::get_output_encoding(encoding);
auto encoder = TextCodec::encoder_for(encoding);
- if (!encoder.has_value()) {
- // NOTE: Fallback to default utf-8 encoder.
- encoder = TextCodec::encoder_for("utf-8"sv);
- }
+ VERIFY(encoder.has_value());
// 2. Let output be the empty string.
StringBuilder output;
diff --git a/Tests/LibTextCodec/TestTextEncoders.cpp b/Tests/LibTextCodec/TestTextEncoders.cpp
index a210d51bfba..6825d407f59 100644
--- a/Tests/LibTextCodec/TestTextEncoders.cpp
+++ b/Tests/LibTextCodec/TestTextEncoders.cpp
@@ -10,14 +10,14 @@
TEST_CASE(test_utf8_encode)
{
- auto& encoder = TextCodec::encoder_for_exact_name("UTF-8"sv).value();
+ auto encoder = TextCodec::encoder_for("UTF-8"sv).value();
// Unicode character U+1F600 GRINNING FACE
auto test_string = "\U0001F600"sv;
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 4);
EXPECT(processed_bytes[0] == 0xF0);
@@ -28,7 +28,7 @@ TEST_CASE(test_utf8_encode)
TEST_CASE(test_euc_jp_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("EUC-JP"sv).value();
+ auto encoder = TextCodec::encoder_for("EUC-JP"sv).value();
// U+A5 Yen Sign
// U+3088 Hiragana Letter Yo
// U+30C4 Katakana Letter Tu
@@ -37,7 +37,7 @@ TEST_CASE(test_euc_jp_encoder)
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 5);
EXPECT(processed_bytes[0] == 0x5C);
@@ -49,7 +49,7 @@ TEST_CASE(test_euc_jp_encoder)
TEST_CASE(test_iso_2022_jp_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("ISO-2022-JP"sv).value();
+ auto encoder = TextCodec::encoder_for("ISO-2022-JP"sv).value();
// U+A5 Yen Sign
// U+3088 Hiragana Letter Yo
// U+30C4 Katakana Letter Tu
@@ -58,7 +58,7 @@ TEST_CASE(test_iso_2022_jp_encoder)
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 14);
EXPECT(processed_bytes[0] == 0x1B);
@@ -79,7 +79,7 @@ TEST_CASE(test_iso_2022_jp_encoder)
TEST_CASE(test_shift_jis_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("Shift_JIS"sv).value();
+ auto encoder = TextCodec::encoder_for("Shift_JIS"sv).value();
// U+A5 Yen Sign
// U+3088 Hiragana Letter Yo
// U+30C4 Katakana Letter Tu
@@ -88,7 +88,7 @@ TEST_CASE(test_shift_jis_encoder)
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 5);
EXPECT(processed_bytes[0] == 0x5C);
@@ -100,7 +100,7 @@ TEST_CASE(test_shift_jis_encoder)
TEST_CASE(test_euc_kr_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("EUC-KR"sv).value();
+ auto encoder = TextCodec::encoder_for("EUC-KR"sv).value();
// U+B29F Hangul Syllable Neulh
// U+7C97 CJK Unified Ideograph-7C97
auto test_string = "\U0000B29F\U00007C97"sv;
@@ -108,7 +108,7 @@ TEST_CASE(test_euc_kr_encoder)
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 4);
EXPECT(processed_bytes[0] == 0x88);
@@ -119,7 +119,7 @@ TEST_CASE(test_euc_kr_encoder)
TEST_CASE(test_big5_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("Big5"sv).value();
+ auto encoder = TextCodec::encoder_for("Big5"sv).value();
// U+A7 Section Sign
// U+70D7 CJK Unified Ideograph-70D7
auto test_string = "\U000000A7\U000070D7"sv;
@@ -127,7 +127,7 @@ TEST_CASE(test_big5_encoder)
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 4);
EXPECT(processed_bytes[0] == 0xA1);
@@ -138,7 +138,7 @@ TEST_CASE(test_big5_encoder)
TEST_CASE(test_gb18030_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("gb18030"sv).value();
+ auto encoder = TextCodec::encoder_for("gb18030"sv).value();
// U+20AC Euro Sign
// U+E4C5 Private Use Area
auto test_string = "\U000020AC\U0000E4C5"sv;
@@ -146,7 +146,7 @@ TEST_CASE(test_gb18030_encoder)
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 4);
EXPECT(processed_bytes[0] == 0xA2);
@@ -157,12 +157,12 @@ TEST_CASE(test_gb18030_encoder)
TEST_CASE(test_windows1252_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("windows-1252"sv).value();
+ auto encoder = TextCodec::encoder_for("windows-1252"sv).value();
auto test_string = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏfoo€"sv;
Vector<u8> processed_bytes;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
EXPECT(processed_bytes.size() == 20);
for (u8 i = 0; i < 15; i++) {
@@ -176,13 +176,13 @@ TEST_CASE(test_windows1252_encoder)
TEST_CASE(test_x_user_defined_encoder)
{
- auto& encoder = TextCodec::encoder_for_exact_name("x-user-defined"sv).value();
+ auto encoder = TextCodec::encoder_for("x-user-defined"sv).value();
auto test_string = "a\uF780\uF7FF\u0100"sv;
Vector<u8> processed_bytes;
Vector<u32> unmappable_code_points;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
[&](u32 code_point) { return unmappable_code_points.try_append(code_point); }));
EXPECT_EQ(processed_bytes, (Vector<u8> { 0x61, 0x80, 0xFF }));
EXPECT_EQ(unmappable_code_points, (Vector<u32> { 0x100 }));
@@ -190,7 +190,7 @@ TEST_CASE(test_x_user_defined_encoder)
TEST_CASE(test_encoder_output_larger_than_buffer)
{
- auto& encoder = TextCodec::encoder_for_exact_name("windows-1252"sv).value();
+ auto encoder = TextCodec::encoder_for("windows-1252"sv).value();
StringBuilder builder;
builder.append_repeated('a', 10000);
builder.append("\u0100"sv);
@@ -201,8 +201,21 @@ TEST_CASE(test_encoder_output_larger_than_buffer)
Vector<u32> unmappable_code_points;
MUST(encoder.process(
Utf8View(test_string),
- [&](u8 byte) { return processed_bytes.try_append(byte); },
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(StringView { bytes }); },
[&](u32 code_point) { return unmappable_code_points.try_append(code_point); }));
EXPECT_EQ(processed_bytes.string_view(), test_string.view().replace("\u0100"sv, ""sv, ReplaceMode::FirstOnly));
EXPECT_EQ(unmappable_code_points, (Vector<u32> { 0x100 }));
}
+
+TEST_CASE(test_encoder_uses_output_encoding)
+{
+ for (auto label : { "UTF-16LE"sv, "UTF-16BE"sv, "replacement"sv }) {
+ auto encoder = TextCodec::encoder_for(label).value();
+ Vector<u8> processed_bytes;
+ MUST(encoder.process(
+ Utf8View("\u00e9"sv),
+ [&](ReadonlyBytes bytes) { return processed_bytes.try_append(bytes.data(), bytes.size()); },
+ [&](u32) -> ErrorOr<void> { EXPECT(false); return {}; }));
+ EXPECT_EQ(processed_bytes, (Vector<u8> { 0xC3, 0xA9 }));
+ }
+}