/* * Copyright (c) 2020-2021, Andreas Kling * Copyright (c) 2022, Jelle Raaijmakers * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace TextCodec { class TEXTCODEC_API Decoder { public: virtual bool validate(StringView); virtual ErrorOr to_utf8(StringView); virtual ErrorOr length_in_utf16_code_units(StringView); ErrorOr process_code_points(StringView, Function(u32)>); protected: virtual ~Decoder() = default; virtual ErrorOr process(StringView, Function(u32)> on_code_point) = 0; }; class TEXTCODEC_API StreamingDecoder final { AK_MAKE_NONCOPYABLE(StreamingDecoder); public: explicit StreamingDecoder(StringView encoding); ~StreamingDecoder(); ErrorOr to_utf8(ReadonlyBytes); ErrorOr finish(); private: void* m_decoder { nullptr }; }; // This will return a decoder for the exact name specified, skipping get_standardized_encoding. // Use this when you want ISO-8859-1 instead of windows-1252. TEXTCODEC_API Optional decoder_for_exact_name(StringView encoding); TEXTCODEC_API Optional decoder_for(StringView encoding); TEXTCODEC_API Optional get_standardized_encoding(StringView encoding); // This returns the appropriate Unicode decoder for the sniffed BOM or nothing if there is no appropriate decoder. TEXTCODEC_API Optional bom_sniff_to_decoder(StringView); // NOTE: This has an obnoxious name to discourage usage. Only use this if you absolutely must! For example, XHR in LibWeb uses this. // This will use the given decoder unless there is a byte order mark in the input, in which we will instead use the appropriate Unicode decoder. TEXTCODEC_API ErrorOr convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView); TEXTCODEC_API ErrorOr convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView); TEXTCODEC_API StringView get_output_encoding(StringView encoding); TEXTCODEC_API String isomorphic_decode(StringView); }