LibTextCodec: Add a simple UTF-16BE decoder

This commit is contained in:
Andreas Kling 2021-02-16 17:31:22 +01:00
parent 01e00bac9d
commit 0538dc4e28
Notes: sideshowbarker 2024-07-18 22:12:55 +09:00
2 changed files with 26 additions and 1 deletions

View file

@ -47,6 +47,14 @@ UTF8Decoder& utf8_decoder()
return *decoder;
}
UTF16BEDecoder& utf16be_decoder()
{
static UTF16BEDecoder* decoder;
if (!decoder)
decoder = new UTF16BEDecoder;
return *decoder;
}
Latin2Decoder& latin2_decoder()
{
static Latin2Decoder* decoder = nullptr;
@ -64,6 +72,8 @@ Decoder* decoder_for(const String& a_encoding)
return &latin1_decoder();
if (encoding.equals_ignoring_case("utf-8"))
return &utf8_decoder();
if (encoding.equals_ignoring_case("utf-16be"))
return &utf16be_decoder();
if (encoding.equals_ignoring_case("iso-8859-2"))
return &latin2_decoder();
dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
@ -170,6 +180,16 @@ String UTF8Decoder::to_utf8(const StringView& input)
return input;
}
String UTF16BEDecoder::to_utf8(const StringView& input)
{
StringBuilder builder(input.length() / 2);
for (size_t i = 0; i < input.length(); i += 2) {
u16 code_point = (input[i] << 8) | input[i + 1];
builder.append_code_point(code_point);
}
return builder.to_string();
}
String Latin1Decoder::to_utf8(const StringView& input)
{
StringBuilder builder(input.length());

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -40,6 +40,11 @@ public:
virtual String to_utf8(const StringView&) override;
};
class UTF16BEDecoder final : public Decoder {
public:
virtual String to_utf8(const StringView&) override;
};
class Latin1Decoder final : public Decoder {
public:
virtual String to_utf8(const StringView&) override;