2024-08-05 15:03:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024, Ben Jilks <benjyjilks@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <AK/Function.h>
|
|
|
|
|
|
|
|
namespace TextCodec {
|
|
|
|
|
|
|
|
class Encoder {
|
|
|
|
public:
|
2024-08-06 10:06:05 +00:00
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) = 0;
|
2024-08-05 15:03:53 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~Encoder() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UTF8Encoder final : public Encoder {
|
|
|
|
public:
|
2024-08-06 10:06:05 +00:00
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
2024-08-05 15:03:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class EUCJPEncoder final : public Encoder {
|
|
|
|
public:
|
2024-08-06 10:06:05 +00:00
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ISO2022JPEncoder final : public Encoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class State {
|
|
|
|
ASCII,
|
|
|
|
Roman,
|
|
|
|
jis0208,
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorOr<State> process_item(u32 item, State, Function<ErrorOr<void>(u8)>& on_byte, Function<ErrorOr<void>(u32)>& on_error);
|
2024-08-05 15:03:53 +00:00
|
|
|
};
|
|
|
|
|
2024-08-05 21:38:07 +00:00
|
|
|
class ShiftJISEncoder final : public Encoder {
|
|
|
|
public:
|
2024-08-06 10:06:05 +00:00
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
2024-08-05 21:38:07 +00:00
|
|
|
};
|
|
|
|
|
2024-08-05 19:00:47 +00:00
|
|
|
class EUCKREncoder final : public Encoder {
|
|
|
|
public:
|
2024-08-06 10:06:05 +00:00
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
2024-08-05 19:00:47 +00:00
|
|
|
};
|
|
|
|
|
2024-08-05 19:23:59 +00:00
|
|
|
class Big5Encoder final : public Encoder {
|
|
|
|
public:
|
2024-08-06 10:06:05 +00:00
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
2024-08-05 19:23:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-05 20:14:51 +00:00
|
|
|
class GB18030Encoder final : public Encoder {
|
|
|
|
public:
|
|
|
|
enum class IsGBK {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
|
|
|
GB18030Encoder(IsGBK is_gbk = IsGBK::No);
|
|
|
|
|
2024-08-06 10:06:05 +00:00
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
2024-08-05 20:14:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
IsGBK m_is_gbk { IsGBK::No };
|
|
|
|
};
|
2024-10-09 21:06:06 +00:00
|
|
|
template<Integral ArrayType = u32>
|
|
|
|
class SingleByteEncoder final : public Encoder {
|
|
|
|
public:
|
|
|
|
SingleByteEncoder(Array<ArrayType, 128> translation_table)
|
|
|
|
: m_translation_table(translation_table)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ErrorOr<void> process(Utf8View, Function<ErrorOr<void>(u8)> on_byte, Function<ErrorOr<void>(u32)> on_error) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Array<ArrayType, 128> m_translation_table;
|
|
|
|
};
|
2024-08-05 20:14:51 +00:00
|
|
|
|
2024-08-05 15:03:53 +00:00
|
|
|
Optional<Encoder&> encoder_for_exact_name(StringView encoding);
|
|
|
|
Optional<Encoder&> encoder_for(StringView label);
|
|
|
|
|
|
|
|
}
|