summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibTextCodec
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-02-17 17:45:08 +0000
committerAndreas Kling <kling@serenityos.org>2023-02-19 17:15:47 +0100
commitf2a94268850b56d9cde98e6096eb19f3108cd1b4 (patch)
tree62f0b4875fabdda97bad215d9a99b63089890726 /Userland/Libraries/LibTextCodec
parent2f4c4639203dd6ef1462741c5e25d1ee60d0287b (diff)
downloadserenity-f2a94268850b56d9cde98e6096eb19f3108cd1b4.zip
LibTextCodec+Everywhere: Return Optional<Decoder&> from `decoder_for()`
Diffstat (limited to 'Userland/Libraries/LibTextCodec')
-rw-r--r--Userland/Libraries/LibTextCodec/Decoder.cpp29
-rw-r--r--Userland/Libraries/LibTextCodec/Decoder.h4
2 files changed, 18 insertions, 15 deletions
diff --git a/Userland/Libraries/LibTextCodec/Decoder.cpp b/Userland/Libraries/LibTextCodec/Decoder.cpp
index 3563f19c73..e69d7abef7 100644
--- a/Userland/Libraries/LibTextCodec/Decoder.cpp
+++ b/Userland/Libraries/LibTextCodec/Decoder.cpp
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -30,37 +31,37 @@ TurkishDecoder s_turkish_decoder;
XUserDefinedDecoder s_x_user_defined_decoder;
}
-Decoder* decoder_for(StringView a_encoding)
+Optional<Decoder&> decoder_for(StringView a_encoding)
{
auto encoding = get_standardized_encoding(a_encoding);
if (encoding.has_value()) {
if (encoding.value().equals_ignoring_case("windows-1252"sv))
- return &s_latin1_decoder;
+ return s_latin1_decoder;
if (encoding.value().equals_ignoring_case("utf-8"sv))
- return &s_utf8_decoder;
+ return s_utf8_decoder;
if (encoding.value().equals_ignoring_case("utf-16be"sv))
- return &s_utf16be_decoder;
+ return s_utf16be_decoder;
if (encoding.value().equals_ignoring_case("utf-16le"sv))
- return &s_utf16le_decoder;
+ return s_utf16le_decoder;
if (encoding.value().equals_ignoring_case("iso-8859-2"sv))
- return &s_latin2_decoder;
+ return s_latin2_decoder;
if (encoding.value().equals_ignoring_case("windows-1255"sv))
- return &s_hebrew_decoder;
+ return s_hebrew_decoder;
if (encoding.value().equals_ignoring_case("windows-1251"sv))
- return &s_cyrillic_decoder;
+ return s_cyrillic_decoder;
if (encoding.value().equals_ignoring_case("koi8-r"sv))
- return &s_koi8r_decoder;
+ return s_koi8r_decoder;
if (encoding.value().equals_ignoring_case("iso-8859-15"sv))
- return &s_latin9_decoder;
+ return s_latin9_decoder;
if (encoding.value().equals_ignoring_case("macintosh"sv))
- return &s_mac_roman_decoder;
+ return s_mac_roman_decoder;
if (encoding.value().equals_ignoring_case("windows-1254"sv))
- return &s_turkish_decoder;
+ return s_turkish_decoder;
if (encoding.value().equals_ignoring_case("x-user-defined"sv))
- return &s_x_user_defined_decoder;
+ return s_x_user_defined_decoder;
}
dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
- return nullptr;
+ return {};
}
// https://encoding.spec.whatwg.org/#concept-encoding-get
diff --git a/Userland/Libraries/LibTextCodec/Decoder.h b/Userland/Libraries/LibTextCodec/Decoder.h
index 7a426d35e1..f8e8205544 100644
--- a/Userland/Libraries/LibTextCodec/Decoder.h
+++ b/Userland/Libraries/LibTextCodec/Decoder.h
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -9,6 +10,7 @@
#include <AK/Forward.h>
#include <AK/Function.h>
+#include <AK/Optional.h>
namespace TextCodec {
@@ -84,7 +86,7 @@ public:
virtual void process(StringView, Function<void(u32)> on_code_point) override;
};
-Decoder* decoder_for(StringView encoding);
+Optional<Decoder&> decoder_for(StringView encoding);
Optional<StringView> get_standardized_encoding(StringView encoding);
// This returns the appropriate Unicode decoder for the sniffed BOM or nullptr if there is no appropriate decoder.