| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-02-16 17:31:22 +01:00
										 |  |  |  * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-03-08 14:27:11 +01:00
										 |  |  |  * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl> | 
					
						
							| 
									
										
										
										
											2023-02-17 17:45:08 +00:00
										 |  |  |  * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-29 11:44:28 +00:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-17 17:45:08 +00:00
										 |  |  | #include <AK/Optional.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace TextCodec { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) = 0; | 
					
						
							|  |  |  |     virtual ErrorOr<String> to_utf8(StringView); | 
					
						
							| 
									
										
										
										
											2021-04-15 10:43:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     virtual ~Decoder() = default; | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UTF8Decoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							|  |  |  |     virtual ErrorOr<String> to_utf8(StringView) override; | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 17:31:22 +01:00
										 |  |  | class UTF16BEDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							|  |  |  |     virtual ErrorOr<String> to_utf8(StringView) override; | 
					
						
							| 
									
										
										
										
											2021-02-16 17:31:22 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-08 14:27:11 +01:00
										 |  |  | class UTF16LEDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							|  |  |  |     virtual ErrorOr<String> to_utf8(StringView) override; | 
					
						
							| 
									
										
										
										
											2022-03-08 14:27:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | class Latin1Decoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-27 22:44:38 +01:00
										 |  |  | class Latin2Decoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2020-12-27 22:44:38 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-17 18:15:32 +03:00
										 |  |  | class HebrewDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2021-04-17 18:15:32 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-01 18:18:26 +03:00
										 |  |  | class CyrillicDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2021-05-01 18:18:26 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 01:19:56 +01:00
										 |  |  | class Koi8RDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2021-12-14 01:19:56 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 16:07:56 +03:00
										 |  |  | class Latin9Decoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2023-01-24 08:50:53 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MacRomanDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2021-06-15 16:07:56 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 16:18:50 +03:00
										 |  |  | class TurkishDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2021-06-23 16:18:50 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 20:38:44 +00:00
										 |  |  | class XUserDefinedDecoder final : public Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override; | 
					
						
							| 
									
										
										
										
											2022-02-11 20:38:44 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-17 17:45:08 +00:00
										 |  |  | Optional<Decoder&> decoder_for(StringView encoding); | 
					
						
							| 
									
										
										
										
											2022-03-21 00:09:28 +01:00
										 |  |  | Optional<StringView> get_standardized_encoding(StringView encoding); | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-17 19:53:51 +00:00
										 |  |  | // This returns the appropriate Unicode decoder for the sniffed BOM or nothing if there is no appropriate decoder.
 | 
					
						
							|  |  |  | Optional<Decoder&> bom_sniff_to_decoder(StringView); | 
					
						
							| 
									
										
										
										
											2022-02-11 20:58:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:02:29 +00:00
										 |  |  | // 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.
 | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  | ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView); | 
					
						
							| 
									
										
										
										
											2022-02-11 21:02:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:41:34 +02:00
										 |  |  | } |