diff options
author | Max Wipfli <mail@maxwipfli.ch> | 2021-05-11 15:52:25 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-18 21:02:07 +0200 |
commit | d325403cb599da640d5e8313c96155c16799c87a (patch) | |
tree | d05fbfac84e297b273ee8bcf0fd1d5678f2b295c /Userland/Libraries/LibTextCodec | |
parent | f51b0729f519116c288b9bd9a1251daf66d7fa49 (diff) | |
download | serenity-d325403cb599da640d5e8313c96155c16799c87a.zip |
LibTextCodec: Use Optional<String> for get_standardized_encoding
This patch changes get_standardized_encoding to use an Optional<String>
return type instead of just returning the null string when unable to
match the provided encoding to one of the canonical encoding names.
This is part of an effort to move away from using null strings towards
explicitly using Optional<String> to indicate that the String may not
have a value.
Diffstat (limited to 'Userland/Libraries/LibTextCodec')
-rw-r--r-- | Userland/Libraries/LibTextCodec/Decoder.cpp | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibTextCodec/Decoder.h | 2 |
2 files changed, 18 insertions, 15 deletions
diff --git a/Userland/Libraries/LibTextCodec/Decoder.cpp b/Userland/Libraries/LibTextCodec/Decoder.cpp index 37a28d5999..bebdb004a3 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.cpp +++ b/Userland/Libraries/LibTextCodec/Decoder.cpp @@ -64,24 +64,26 @@ CyrillicDecoder& cyrillic_decoder() Decoder* decoder_for(const String& a_encoding) { auto encoding = get_standardized_encoding(a_encoding); - if (encoding.equals_ignoring_case("windows-1252")) - return &latin1_decoder(); - if (encoding.equals_ignoring_case("utf-8")) - return &utf8_decoder(); - if (encoding.equals_ignoring_case("utf-16be")) - return &utf16be_decoder(); - if (encoding.equals_ignoring_case("iso-8859-2")) - return &latin2_decoder(); - if (encoding.equals_ignoring_case("windows-1255")) - return &hebrew_decoder(); - if (encoding.equals_ignoring_case("windows-1251")) - return &cyrillic_decoder(); + if (encoding.has_value()) { + if (encoding.value().equals_ignoring_case("windows-1252")) + return &latin1_decoder(); + if (encoding.value().equals_ignoring_case("utf-8")) + return &utf8_decoder(); + if (encoding.value().equals_ignoring_case("utf-16be")) + return &utf16be_decoder(); + if (encoding.value().equals_ignoring_case("iso-8859-2")) + return &latin2_decoder(); + if (encoding.value().equals_ignoring_case("windows-1255")) + return &hebrew_decoder(); + if (encoding.value().equals_ignoring_case("windows-1251")) + return &cyrillic_decoder(); + } dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding); return nullptr; } // https://encoding.spec.whatwg.org/#concept-encoding-get -String get_standardized_encoding(const String& encoding) +Optional<String> get_standardized_encoding(const String& encoding) { String trimmed_lowercase_encoding = encoding.trim_whitespace().to_lowercase(); @@ -172,7 +174,8 @@ String get_standardized_encoding(const String& encoding) bool is_standardized_encoding(const String& encoding) { - return encoding.equals_ignoring_case(get_standardized_encoding(encoding)); + auto standardized_encoding = get_standardized_encoding(encoding); + return standardized_encoding.has_value() && encoding.equals_ignoring_case(standardized_encoding.value()); } String UTF8Decoder::to_utf8(const StringView& input) diff --git a/Userland/Libraries/LibTextCodec/Decoder.h b/Userland/Libraries/LibTextCodec/Decoder.h index 6ea4147b7e..585f05ff0d 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.h +++ b/Userland/Libraries/LibTextCodec/Decoder.h @@ -49,7 +49,7 @@ public: }; Decoder* decoder_for(const String& encoding); -String get_standardized_encoding(const String& encoding); +Optional<String> get_standardized_encoding(const String& encoding); bool is_standardized_encoding(const String& encoding); } |