lib/icu/Converter: Create() returns std::unique_ptr

This commit is contained in:
Max Kellermann 2019-11-04 15:44:06 +01:00
parent 496f43e25d
commit ed327c597a
6 changed files with 11 additions and 16 deletions

View File

@ -32,7 +32,7 @@
void void
IcyMetaDataParser::SetCharset(const char *charset) IcyMetaDataParser::SetCharset(const char *charset)
{ {
icu_converter.reset(IcuConverter::Create(charset)); icu_converter = IcuConverter::Create(charset);
} }
#endif #endif

View File

@ -456,10 +456,7 @@ Windows1252ToUTF8(const char *s) noexcept
{ {
#ifdef HAVE_ICU_CONVERTER #ifdef HAVE_ICU_CONVERTER
try { try {
std::unique_ptr<IcuConverter> return IcuConverter::Create("windows-1252")->ToUTF8(s);
converter(IcuConverter::Create("windows-1252"));
return converter->ToUTF8(s);
} catch (...) { } } catch (...) { }
#endif #endif

View File

@ -38,7 +38,7 @@
static std::string fs_charset; static std::string fs_charset;
static IcuConverter *fs_converter; static std::unique_ptr<IcuConverter> fs_converter;
void void
SetFSCharset(const char *charset) SetFSCharset(const char *charset)
@ -59,8 +59,7 @@ void
DeinitFSCharset() noexcept DeinitFSCharset() noexcept
{ {
#ifdef HAVE_ICU_CONVERTER #ifdef HAVE_ICU_CONVERTER
delete fs_converter; fs_converter.reset();
fs_converter = nullptr;
#endif #endif
} }

View File

@ -46,7 +46,7 @@ IcuConverter::~IcuConverter()
#ifdef HAVE_ICU_CONVERTER #ifdef HAVE_ICU_CONVERTER
IcuConverter * std::unique_ptr<IcuConverter>
IcuConverter::Create(const char *charset) IcuConverter::Create(const char *charset)
{ {
#ifdef HAVE_ICU #ifdef HAVE_ICU
@ -56,7 +56,7 @@ IcuConverter::Create(const char *charset)
throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s", throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s",
charset, u_errorName(code)).c_str()); charset, u_errorName(code)).c_str());
return new IcuConverter(converter); return std::unique_ptr<IcuConverter>(new IcuConverter(converter));
#elif defined(HAVE_ICONV) #elif defined(HAVE_ICONV)
iconv_t to = iconv_open("utf-8", charset); iconv_t to = iconv_open("utf-8", charset);
iconv_t from = iconv_open(charset, "utf-8"); iconv_t from = iconv_open(charset, "utf-8");
@ -70,7 +70,7 @@ IcuConverter::Create(const char *charset)
charset); charset);
} }
return new IcuConverter(to, from); return std::unique_ptr<IcuConverter>(new IcuConverter(to, from));
#endif #endif
} }

View File

@ -33,6 +33,8 @@
#ifdef HAVE_ICU_CONVERTER #ifdef HAVE_ICU_CONVERTER
#include <memory>
#ifdef HAVE_ICU #ifdef HAVE_ICU
struct UConverter; struct UConverter;
#endif #endif
@ -74,7 +76,7 @@ public:
/** /**
* Throws std::runtime_error on error. * Throws std::runtime_error on error.
*/ */
static IcuConverter *Create(const char *charset); static std::unique_ptr<IcuConverter> Create(const char *charset);
/** /**
* Convert the string to UTF-8. * Convert the string to UTF-8.

View File

@ -30,8 +30,7 @@ TEST(IcuConverter, InvalidCharset)
TEST(IcuConverter, Latin1) TEST(IcuConverter, Latin1)
{ {
IcuConverter *const converter = const auto converter = IcuConverter::Create("iso-8859-1");
IcuConverter::Create("iso-8859-1");
ASSERT_NE(converter, nullptr); ASSERT_NE(converter, nullptr);
for (const auto i : invalid_utf8) { for (const auto i : invalid_utf8) {
@ -45,8 +44,6 @@ TEST(IcuConverter, Latin1)
auto t = converter->ToUTF8(i.other); auto t = converter->ToUTF8(i.other);
EXPECT_STREQ(t.c_str(), i.utf8); EXPECT_STREQ(t.c_str(), i.utf8);
} }
delete converter;
} }
#endif #endif