Decoder.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <AK/Function.h>
  10. namespace TextCodec {
  11. class Decoder {
  12. public:
  13. virtual void process(StringView, Function<void(u32)> on_code_point) = 0;
  14. virtual String to_utf8(StringView);
  15. protected:
  16. virtual ~Decoder() = default;
  17. };
  18. class UTF8Decoder final : public Decoder {
  19. public:
  20. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  21. virtual String to_utf8(StringView) override;
  22. };
  23. class UTF16BEDecoder final : public Decoder {
  24. public:
  25. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  26. virtual String to_utf8(StringView) override;
  27. };
  28. class UTF16LEDecoder final : public Decoder {
  29. public:
  30. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  31. virtual String to_utf8(StringView) override;
  32. };
  33. class Latin1Decoder final : public Decoder {
  34. public:
  35. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  36. };
  37. class Latin2Decoder final : public Decoder {
  38. public:
  39. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  40. };
  41. class HebrewDecoder final : public Decoder {
  42. public:
  43. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  44. };
  45. class CyrillicDecoder final : public Decoder {
  46. public:
  47. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  48. };
  49. class Koi8RDecoder final : public Decoder {
  50. public:
  51. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  52. };
  53. class Latin9Decoder final : public Decoder {
  54. public:
  55. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  56. };
  57. class TurkishDecoder final : public Decoder {
  58. public:
  59. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  60. };
  61. class XUserDefinedDecoder final : public Decoder {
  62. public:
  63. virtual void process(StringView, Function<void(u32)> on_code_point) override;
  64. };
  65. Decoder* decoder_for(String const& encoding);
  66. Optional<StringView> get_standardized_encoding(StringView encoding);
  67. // This returns the appropriate Unicode decoder for the sniffed BOM or nullptr if there is no appropriate decoder.
  68. Decoder* bom_sniff_to_decoder(StringView);
  69. // NOTE: This has an obnoxious name to discourage usage. Only use this if you absolutely must! For example, XHR in LibWeb uses this.
  70. // 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.
  71. String convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView);
  72. }