Skip to content

Commit

Permalink
Make ot::encoding_converter use string views
Browse files Browse the repository at this point in the history
  • Loading branch information
fmang committed Dec 26, 2020
1 parent c43704a commit fd5fa3c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions src/opustags.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ class encoding_converter {
* 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 */
};
Expand Down
10 changes: 5 additions & 5 deletions src/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit fd5fa3c

Please sign in to comment.