From ed327c597a51bd3b51daa736b7ba8759a42dd5f5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 4 Nov 2019 15:44:06 +0100 Subject: [PATCH] lib/icu/Converter: Create() returns std::unique_ptr --- src/IcyMetaDataParser.cxx | 2 +- src/decoder/plugins/SidplayDecoderPlugin.cxx | 5 +---- src/fs/Charset.cxx | 5 ++--- src/lib/icu/Converter.cxx | 6 +++--- src/lib/icu/Converter.hxx | 4 +++- test/TestIcu.cxx | 5 +---- 6 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/IcyMetaDataParser.cxx b/src/IcyMetaDataParser.cxx index 2c8f1c9db..a9630bdc8 100644 --- a/src/IcyMetaDataParser.cxx +++ b/src/IcyMetaDataParser.cxx @@ -32,7 +32,7 @@ void IcyMetaDataParser::SetCharset(const char *charset) { - icu_converter.reset(IcuConverter::Create(charset)); + icu_converter = IcuConverter::Create(charset); } #endif diff --git a/src/decoder/plugins/SidplayDecoderPlugin.cxx b/src/decoder/plugins/SidplayDecoderPlugin.cxx index ca527ee6a..625962f86 100644 --- a/src/decoder/plugins/SidplayDecoderPlugin.cxx +++ b/src/decoder/plugins/SidplayDecoderPlugin.cxx @@ -456,10 +456,7 @@ Windows1252ToUTF8(const char *s) noexcept { #ifdef HAVE_ICU_CONVERTER try { - std::unique_ptr - converter(IcuConverter::Create("windows-1252")); - - return converter->ToUTF8(s); + return IcuConverter::Create("windows-1252")->ToUTF8(s); } catch (...) { } #endif diff --git a/src/fs/Charset.cxx b/src/fs/Charset.cxx index 941021fa7..880a0101c 100644 --- a/src/fs/Charset.cxx +++ b/src/fs/Charset.cxx @@ -38,7 +38,7 @@ static std::string fs_charset; -static IcuConverter *fs_converter; +static std::unique_ptr fs_converter; void SetFSCharset(const char *charset) @@ -59,8 +59,7 @@ void DeinitFSCharset() noexcept { #ifdef HAVE_ICU_CONVERTER - delete fs_converter; - fs_converter = nullptr; + fs_converter.reset(); #endif } diff --git a/src/lib/icu/Converter.cxx b/src/lib/icu/Converter.cxx index 7c46c726c..60b7da2d7 100644 --- a/src/lib/icu/Converter.cxx +++ b/src/lib/icu/Converter.cxx @@ -46,7 +46,7 @@ IcuConverter::~IcuConverter() #ifdef HAVE_ICU_CONVERTER -IcuConverter * +std::unique_ptr IcuConverter::Create(const char *charset) { #ifdef HAVE_ICU @@ -56,7 +56,7 @@ IcuConverter::Create(const char *charset) throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s", charset, u_errorName(code)).c_str()); - return new IcuConverter(converter); + return std::unique_ptr(new IcuConverter(converter)); #elif defined(HAVE_ICONV) iconv_t to = iconv_open("utf-8", charset); iconv_t from = iconv_open(charset, "utf-8"); @@ -70,7 +70,7 @@ IcuConverter::Create(const char *charset) charset); } - return new IcuConverter(to, from); + return std::unique_ptr(new IcuConverter(to, from)); #endif } diff --git a/src/lib/icu/Converter.hxx b/src/lib/icu/Converter.hxx index 29cdab7aa..41d45cbe6 100644 --- a/src/lib/icu/Converter.hxx +++ b/src/lib/icu/Converter.hxx @@ -33,6 +33,8 @@ #ifdef HAVE_ICU_CONVERTER +#include + #ifdef HAVE_ICU struct UConverter; #endif @@ -74,7 +76,7 @@ public: /** * Throws std::runtime_error on error. */ - static IcuConverter *Create(const char *charset); + static std::unique_ptr Create(const char *charset); /** * Convert the string to UTF-8. diff --git a/test/TestIcu.cxx b/test/TestIcu.cxx index d5345556d..adda08ac5 100644 --- a/test/TestIcu.cxx +++ b/test/TestIcu.cxx @@ -30,8 +30,7 @@ TEST(IcuConverter, InvalidCharset) TEST(IcuConverter, Latin1) { - IcuConverter *const converter = - IcuConverter::Create("iso-8859-1"); + const auto converter = IcuConverter::Create("iso-8859-1"); ASSERT_NE(converter, nullptr); for (const auto i : invalid_utf8) { @@ -45,8 +44,6 @@ TEST(IcuConverter, Latin1) auto t = converter->ToUTF8(i.other); EXPECT_STREQ(t.c_str(), i.utf8); } - - delete converter; } #endif