From bb548f51d3460d1d89701a09a32623228d56d1e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Sun, 9 Dec 2018 12:17:10 -0500 Subject: [PATCH] encoding_converter: overload for C strings --- src/opustags.h | 4 +++- src/system.cc | 13 +++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/opustags.h b/src/opustags.h index 2ae5ce0..d9db029 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -153,7 +153,9 @@ public: * abort the processing. If some character could not be converted perfectly, keep converting * the string and finally return #st::information_lost. */ - status operator()(const std::string& in, std::string& out); + status operator()(const std::string& in, std::string& out) + { return (*this)(in.data(), in.size(), out); } + status operator()(const char* in, size_t n, std::string& out); private: iconv_t cd; /**< conversion descriptor */ }; diff --git a/src/system.cc b/src/system.cc index e62c25b..e9d6b01 100644 --- a/src/system.cc +++ b/src/system.cc @@ -63,13 +63,13 @@ ot::encoding_converter::~encoding_converter() iconv_close(cd); } -ot::status ot::encoding_converter::operator()(const std::string& in, std::string& out) +ot::status ot::encoding_converter::operator()(const char* in, size_t n, std::string& out) { iconv(cd, nullptr, nullptr, nullptr, nullptr); out.clear(); - out.reserve(in.size()); - char* in_cursor = const_cast(in.data()); - size_t in_left = in.size(); + out.reserve(n); + char* in_cursor = const_cast(in); + size_t in_left = n; constexpr size_t chunk_size = 1024; char chunk[chunk_size]; bool lost_information = false; @@ -79,7 +79,8 @@ ot::status ot::encoding_converter::operator()(const std::string& in, std::string size_t rc = iconv(cd, &in_cursor, &in_left, &out_cursor, &out_left); if (rc == (size_t) -1 && errno != E2BIG) return {ot::st::badly_encoded, - "Could not convert string '" + in + "': " + strerror(errno)}; + "Could not convert string '" + std::string(in, n) + "': " + + strerror(errno)}; if (rc != 0) lost_information = true; out.append(chunk, out_cursor - chunk); @@ -91,6 +92,6 @@ ot::status ot::encoding_converter::operator()(const std::string& in, std::string if (lost_information) return {ot::st::information_lost, "Some characters could not be converted into the target encoding " - "in string '" + in + "'."}; + "in string '" + std::string(in, n) + "'."}; return ot::st::ok; }