diff --git a/include/libtorrent/utf8.hpp b/include/libtorrent/utf8.hpp index ef783a970..8fafc12ee 100644 --- a/include/libtorrent/utf8.hpp +++ b/include/libtorrent/utf8.hpp @@ -78,7 +78,7 @@ namespace libtorrent { // TODO: 3 take a string_view here TORRENT_EXTRA_EXPORT std::pair - parse_utf8_codepoint(char const* str, int len); + parse_utf8_codepoint(string_view str); } #endif diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 0508c06dc..faf2ba692 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -236,8 +236,7 @@ namespace libtorrent { for (std::size_t i = 0; i < element.size(); i += std::size_t(seq_len)) { std::int32_t code_point; - std::tie(code_point, seq_len) = parse_utf8_codepoint(element.data() + i - , int(element.size() - i)); + std::tie(code_point, seq_len) = parse_utf8_codepoint(element.substr(i)); if (code_point >= 0 && filter_path_character(code_point)) { diff --git a/src/utf8.cpp b/src/utf8.cpp index 1224aec5b..b99e91f1b 100644 --- a/src/utf8.cpp +++ b/src/utf8.cpp @@ -267,17 +267,17 @@ namespace { // returns the unicode codepoint and the number of bytes of the utf8 sequence // that was parsed. The codepoint is -1 if it's invalid - std::pair parse_utf8_codepoint(char const* str, int const len) + std::pair parse_utf8_codepoint(string_view str) { - int const sequence_len = trailingBytesForUTF8[static_cast(*str)] + 1; - if (sequence_len > len) return std::make_pair(-1, len); + int const sequence_len = trailingBytesForUTF8[static_cast(str[0])] + 1; + if (sequence_len > int(str.size())) return std::make_pair(-1, static_cast(str.size())); if (sequence_len > 4) { return std::make_pair(-1, sequence_len); } - if (!isLegalUTF8(reinterpret_cast(str), sequence_len)) + if (!isLegalUTF8(reinterpret_cast(str.data()), sequence_len)) { return std::make_pair(-1, sequence_len); } @@ -286,7 +286,7 @@ namespace { for (int i = 0; i < sequence_len; ++i) { ch <<= 6; - ch += static_cast(str[i]); + ch += static_cast(str[static_cast(i)]); } ch -= offsetsFromUTF8[sequence_len-1];