From fd5fa3cd5f04bda11585db31c155bac487e114f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano?= Date: Sat, 26 Dec 2020 12:42:37 +0100 Subject: [PATCH] Make ot::encoding_converter use string views --- src/cli.cc | 6 +++--- src/opustags.h | 4 +--- src/system.cc | 10 +++++----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/cli.cc b/src/cli.cc index db8d8cc..805bd2c 100644 --- a/src/cli.cc +++ b/src/cli.cc @@ -86,14 +86,14 @@ ot::status ot::parse_options(int argc, char** argv, ot::options& opt, FILE* comm opt.overwrite = true; break; case 'd': - rc = to_utf8(optarg, strlen(optarg), utf8); + rc = to_utf8(optarg, utf8); if (rc != ot::st::ok) return {st::bad_arguments, "Could not encode argument into UTF-8: " + rc.message}; opt.to_delete.emplace_back(std::move(utf8)); break; case 'a': case 's': - rc = to_utf8(optarg, strlen(optarg), utf8); + rc = to_utf8(optarg, utf8); if (rc != ot::st::ok) return {st::bad_arguments, "Could not encode argument into UTF-8: " + rc.message}; if ((equal = utf8.find('=')) == std::string::npos) @@ -225,7 +225,7 @@ ot::status ot::read_comments(FILE* input, std::list& comments) return rc; } std::string utf8; - ot::status rc = to_utf8(line, nread, utf8); + ot::status rc = to_utf8(std::string_view(line, nread), utf8); if (rc == ot::st::ok) { comments.emplace_back(std::move(utf8)); } else { diff --git a/src/opustags.h b/src/opustags.h index f46ac7b..85c87b5 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -161,9 +161,7 @@ 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) - { return (*this)(in.data(), in.size(), out); } - status operator()(const char* in, size_t n, std::string& out); + status operator()(std::string_view in, std::string& out); private: iconv_t cd; /**< conversion descriptor */ }; diff --git a/src/system.cc b/src/system.cc index abcdb44..6a7ba32 100644 --- a/src/system.cc +++ b/src/system.cc @@ -104,13 +104,13 @@ ot::encoding_converter::~encoding_converter() iconv_close(cd); } -ot::status ot::encoding_converter::operator()(const char* in, size_t n, std::string& out) +ot::status ot::encoding_converter::operator()(std::string_view in, std::string& out) { iconv(cd, nullptr, nullptr, nullptr, nullptr); out.clear(); - out.reserve(n); - char* in_cursor = const_cast(in); - size_t in_left = n; + out.reserve(in.size()); + char* in_cursor = const_cast(in.data()); + size_t in_left = in.size(); constexpr size_t chunk_size = 1024; char chunk[chunk_size]; bool lost_information = false; @@ -130,7 +130,7 @@ ot::status ot::encoding_converter::operator()(const char* in, size_t n, std::str break; } else if (rc == (size_t) -1 && errno != E2BIG) { return {ot::st::badly_encoded, - "Could not convert string '" + std::string(in, n) + "': " + + "Could not convert string '" + std::string(in) + "': " + strerror(errno)}; } else if (rc != 0 && rc != (size_t) -1) { lost_information = true;