diff --git a/Userland/Libraries/LibTextCodec/Decoder.cpp b/Userland/Libraries/LibTextCodec/Decoder.cpp index 7eb089c962..c1cbb1d036 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.cpp +++ b/Userland/Libraries/LibTextCodec/Decoder.cpp @@ -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()); diff --git a/Userland/Libraries/LibTextCodec/Decoder.h b/Userland/Libraries/LibTextCodec/Decoder.h index 84dd388ea5..3acb23f533 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.h +++ b/Userland/Libraries/LibTextCodec/Decoder.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * 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;