LibTextCodec+LibWeb: Move isomorphic coders to LibTextCodec

This will be used outside of LibWeb.
This commit is contained in:
Timothy Flynn 2025-11-24 12:20:51 -05:00 committed by Jelle Raaijmakers
parent 0480934afb
commit 0fd80a8f99
Notes: github-actions[bot] 2025-11-27 13:59:06 +00:00
17 changed files with 65 additions and 64 deletions

View file

@ -1299,4 +1299,18 @@ ErrorOr<void> ReplacementDecoder::process(StringView input, Function<ErrorOr<voi
return {};
}
// https://infra.spec.whatwg.org/#isomorphic-decode
String isomorphic_decode(StringView input)
{
// To isomorphic decode a byte sequence input, return a string whose code point length is equal to inputs length
// and whose code points have the same values as the values of inputs bytes, in the same order.
// NB: This is essentially spec-speak for "Decode as ISO-8859-1 / Latin-1".
StringBuilder builder(input.length());
for (auto byte : input.bytes())
builder.append_code_point(byte);
return builder.to_string_without_validation();
}
}

View file

@ -135,4 +135,6 @@ TEXTCODEC_API ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_t
TEXTCODEC_API StringView get_output_encoding(StringView encoding);
TEXTCODEC_API String isomorphic_decode(StringView);
}

View file

@ -667,4 +667,23 @@ ErrorOr<void> SingleByteEncoder<ArrayType>::process(Utf8View input, Function<Err
return {};
}
// https://infra.spec.whatwg.org/#isomorphic-encode
ByteString isomorphic_encode(StringView input)
{
// To isomorphic encode an isomorphic string input: return a byte sequence whose length is equal to inputs code
// point length and whose bytes have the same values as the values of inputs code points, in the same order.
// NB: This is essentially spec-speak for "Encode as ISO-8859-1 / Latin-1".
StringBuilder builder(input.length());
for (auto code_point : Utf8View { input }) {
// VERIFY(code_point <= 0xFF);
if (code_point > 0xFF)
dbgln("FIXME: Trying to isomorphic encode a string with code points > U+00FF.");
builder.append(static_cast<u8>(code_point));
}
return builder.to_byte_string();
}
}

View file

@ -90,4 +90,6 @@ private:
TEXTCODEC_API Optional<Encoder&> encoder_for_exact_name(StringView encoding);
TEXTCODEC_API Optional<Encoder&> encoder_for(StringView label);
TEXTCODEC_API ByteString isomorphic_encode(StringView);
}