Make ot::encoding_converter use string views

This commit is contained in:
Frédéric Mangano 2020-12-26 12:42:37 +01:00
parent c43704a0a7
commit fd5fa3cd5f
3 changed files with 9 additions and 11 deletions

View File

@ -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<std::string>& 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 {

View File

@ -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 */
};

View File

@ -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<char*>(in);
size_t in_left = n;
out.reserve(in.size());
char* in_cursor = const_cast<char*>(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;