lib/icu/Converter: use libfmt
This commit is contained in:
@@ -19,10 +19,10 @@
|
|||||||
|
|
||||||
#include "Converter.hxx"
|
#include "Converter.hxx"
|
||||||
#include "util/AllocatedString.hxx"
|
#include "util/AllocatedString.hxx"
|
||||||
#include "util/AllocatedArray.hxx"
|
|
||||||
#include "util/FormatString.hxx"
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#ifdef HAVE_ICU
|
#ifdef HAVE_ICU
|
||||||
#include "Util.hxx"
|
#include "Util.hxx"
|
||||||
|
#include "util/AllocatedArray.hxx"
|
||||||
#include <unicode/ucnv.h>
|
#include <unicode/ucnv.h>
|
||||||
#elif defined(HAVE_ICONV)
|
#elif defined(HAVE_ICONV)
|
||||||
#include "system/Error.hxx"
|
#include "system/Error.hxx"
|
||||||
@@ -53,8 +54,8 @@ IcuConverter::Create(const char *charset)
|
|||||||
UErrorCode code = U_ZERO_ERROR;
|
UErrorCode code = U_ZERO_ERROR;
|
||||||
UConverter *converter = ucnv_open(charset, &code);
|
UConverter *converter = ucnv_open(charset, &code);
|
||||||
if (converter == nullptr)
|
if (converter == nullptr)
|
||||||
throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s",
|
throw std::runtime_error(fmt::format(FMT_STRING("Failed to initialize charset '{}': {}"),
|
||||||
charset, u_errorName(code)).c_str());
|
charset, u_errorName(code)));
|
||||||
|
|
||||||
return std::unique_ptr<IcuConverter>(new IcuConverter(converter));
|
return std::unique_ptr<IcuConverter>(new IcuConverter(converter));
|
||||||
#elif defined(HAVE_ICONV)
|
#elif defined(HAVE_ICONV)
|
||||||
@@ -66,8 +67,8 @@ IcuConverter::Create(const char *charset)
|
|||||||
iconv_close(to);
|
iconv_close(to);
|
||||||
if (from != (iconv_t)-1)
|
if (from != (iconv_t)-1)
|
||||||
iconv_close(from);
|
iconv_close(from);
|
||||||
throw FormatErrno(e, "Failed to initialize charset '%s'",
|
throw MakeErrno(e, fmt::format(FMT_STRING("Failed to initialize charset '{}'"),
|
||||||
charset);
|
charset).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::unique_ptr<IcuConverter>(new IcuConverter(to, from));
|
return std::unique_ptr<IcuConverter>(new IcuConverter(to, from));
|
||||||
@@ -118,8 +119,8 @@ IcuConverter::ToUTF8(std::string_view s) const
|
|||||||
&source, source + s.size(),
|
&source, source + s.size(),
|
||||||
nullptr, true, &code);
|
nullptr, true, &code);
|
||||||
if (code != U_ZERO_ERROR)
|
if (code != U_ZERO_ERROR)
|
||||||
throw std::runtime_error(FormatString("Failed to convert to Unicode: %s",
|
throw std::runtime_error(fmt::format(FMT_STRING("Failed to convert to Unicode: {}"),
|
||||||
u_errorName(code)).c_str());
|
u_errorName(code)));
|
||||||
|
|
||||||
const size_t target_length = target - buffer;
|
const size_t target_length = target - buffer;
|
||||||
return UCharToUTF8({buffer, target_length});
|
return UCharToUTF8({buffer, target_length});
|
||||||
@@ -148,8 +149,8 @@ IcuConverter::FromUTF8(std::string_view s) const
|
|||||||
nullptr, true, &code);
|
nullptr, true, &code);
|
||||||
|
|
||||||
if (code != U_ZERO_ERROR)
|
if (code != U_ZERO_ERROR)
|
||||||
throw std::runtime_error(FormatString("Failed to convert from Unicode: %s",
|
throw std::runtime_error(fmt::format(FMT_STRING("Failed to convert from Unicode: {}"),
|
||||||
u_errorName(code)).c_str());
|
u_errorName(code)));
|
||||||
|
|
||||||
return AllocatedString({buffer, size_t(target - buffer)});
|
return AllocatedString({buffer, size_t(target - buffer)});
|
||||||
|
|
||||||
|
@@ -31,6 +31,7 @@ icu = static_library(
|
|||||||
include_directories: inc,
|
include_directories: inc,
|
||||||
dependencies: [
|
dependencies: [
|
||||||
icu_dep,
|
icu_dep,
|
||||||
|
fmt_dep,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user