/* * Copyright (c) 2024, Ben Jilks * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include namespace TextCodec { class Encoder { public: virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) = 0; protected: virtual ~Encoder() = default; }; class UTF8Encoder final : public Encoder { public: virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; }; class EUCJPEncoder final : public Encoder { public: virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; }; class ISO2022JPEncoder final : public Encoder { public: virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; private: enum class State { ASCII, Roman, jis0208, }; ErrorOr process_item(u32 item, State, Function(u8)>& on_byte, Function(u32)>& on_error); }; class ShiftJISEncoder final : public Encoder { public: virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; }; class EUCKREncoder final : public Encoder { public: virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; }; class Big5Encoder final : public Encoder { public: virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; }; class GB18030Encoder final : public Encoder { public: enum class IsGBK { Yes, No, }; GB18030Encoder(IsGBK is_gbk = IsGBK::No); virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; private: IsGBK m_is_gbk { IsGBK::No }; }; template class SingleByteEncoder final : public Encoder { public: SingleByteEncoder(Array translation_table) : m_translation_table(translation_table) { } virtual ErrorOr process(Utf8View, Function(u8)> on_byte, Function(u32)> on_error) override; private: Array m_translation_table; }; Optional encoder_for_exact_name(StringView encoding); Optional encoder_for(StringView label); }