From 3c355bb3f8a65e98f6a7c9edb076c88d500c2184 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 4 Oct 2016 23:58:26 -0400 Subject: [PATCH] use string_view in alert constructors (#1186) --- include/libtorrent/alert_types.hpp | 110 ++++++++----------------- include/libtorrent/stack_allocator.hpp | 8 +- src/alert.cpp | 74 ++++++----------- 3 files changed, 63 insertions(+), 129 deletions(-) diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index c342d28cb..69cc55711 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -50,6 +50,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/close_reason.hpp" #include "libtorrent/piece_block.hpp" #include "libtorrent/aux_/escape_string.hpp" // for convert_from_native +#include "libtorrent/string_view.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" @@ -129,7 +130,7 @@ namespace libtorrent { // internal tracker_alert(aux::stack_allocator& alloc, torrent_handle const& h - , std::string const& u); + , string_view u); static const int alert_type = 2; static const int static_category = alert::tracker_notification; @@ -248,8 +249,7 @@ namespace libtorrent { // internal file_renamed_alert(aux::stack_allocator& alloc, torrent_handle const& h - , std::string const& n - , int idx); + , string_view n, int idx); TORRENT_DEFINE_ALERT_PRIO(file_renamed_alert, 7) @@ -419,12 +419,8 @@ namespace libtorrent { // internal tracker_error_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , int times - , int status - , std::string const& u - , error_code const& e - , std::string const& m); + , torrent_handle const& h, int times, int status, string_view u + , error_code const& e, string_view m); TORRENT_DEFINE_ALERT(tracker_error_alert, 11) @@ -452,9 +448,7 @@ namespace libtorrent { // internal tracker_warning_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u - , std::string const& m); + , torrent_handle const& h, string_view u, string_view m); TORRENT_DEFINE_ALERT(tracker_warning_alert, 12) @@ -478,10 +472,7 @@ namespace libtorrent { // internal scrape_reply_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , int incomp - , int comp - , std::string const& u); + , torrent_handle const& h, int incomp, int comp, string_view u); TORRENT_DEFINE_ALERT(scrape_reply_alert, 13) @@ -500,13 +491,9 @@ namespace libtorrent { // internal scrape_failed_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u - , error_code const& e); + , torrent_handle const& h, string_view u, error_code const& e); scrape_failed_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u - , std::string const& m); + , torrent_handle const& h, string_view u, string_view m); TORRENT_DEFINE_ALERT(scrape_failed_alert, 14) @@ -538,9 +525,7 @@ namespace libtorrent { // internal tracker_reply_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , int np - , std::string const& u); + , torrent_handle const& h, int np, string_view u); TORRENT_DEFINE_ALERT(tracker_reply_alert, 15) @@ -578,7 +563,7 @@ namespace libtorrent // internal tracker_announce_alert(aux::stack_allocator& alloc , torrent_handle const& h - , std::string const& u, int e); + , string_view u, int e); TORRENT_DEFINE_ALERT(tracker_announce_alert, 17) @@ -883,7 +868,7 @@ namespace libtorrent { // internal storage_moved_alert(aux::stack_allocator& alloc - , torrent_handle const& h, std::string const& p); + , torrent_handle const& h, string_view p); TORRENT_DEFINE_ALERT(storage_moved_alert, 33) @@ -907,9 +892,7 @@ namespace libtorrent { // internal storage_moved_failed_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , error_code const& e - , std::string const& file + , torrent_handle const& h, error_code const& e, string_view file , char const* op); TORRENT_DEFINE_ALERT(storage_moved_failed_alert, 34) @@ -1068,9 +1051,9 @@ namespace libtorrent { // internal url_seed_alert(aux::stack_allocator& alloc, torrent_handle const& h - , std::string const& u, error_code const& e); + , string_view u, error_code const& e); url_seed_alert(aux::stack_allocator& alloc, torrent_handle const& h - , std::string const& u, std::string const& m); + , string_view u, string_view m); TORRENT_DEFINE_ALERT(url_seed_alert, 42) @@ -1106,11 +1089,8 @@ namespace libtorrent struct TORRENT_EXPORT file_error_alert final : torrent_alert { // internal - file_error_alert(aux::stack_allocator& alloc - , error_code const& ec - , std::string const& file - , char const* op - , torrent_handle const& h); + file_error_alert(aux::stack_allocator& alloc, error_code const& ec + , string_view file, char const* op, torrent_handle const& h); TORRENT_DEFINE_ALERT(file_error_alert, 43) @@ -1255,37 +1235,20 @@ namespace libtorrent enum socket_type_t { tcp, tcp_ssl, udp, i2p, socks5, utp_ssl }; // internal - listen_failed_alert( - aux::stack_allocator& alloc - , std::string const& iface - , libtorrent::address const& listen_addr - , int listen_port - , int op - , error_code const& ec + listen_failed_alert(aux::stack_allocator& alloc, string_view iface + , libtorrent::address const& listen_addr, int listen_port + , int op, error_code const& ec, socket_type_t t); + + listen_failed_alert(aux::stack_allocator& alloc, string_view iface + , tcp::endpoint const& ep, int op, error_code const& ec , socket_type_t t); - listen_failed_alert( - aux::stack_allocator& alloc - , std::string const& iface - , tcp::endpoint const& ep - , int op - , error_code const& ec + listen_failed_alert(aux::stack_allocator& alloc, string_view iface + , udp::endpoint const& ep, int op, error_code const& ec , socket_type_t t); - listen_failed_alert( - aux::stack_allocator& alloc - , std::string const& iface - , udp::endpoint const& ep - , int op - , error_code const& ec - , socket_type_t t); - - listen_failed_alert( - aux::stack_allocator& alloc - , std::string const& iface - , int op - , error_code const& ec - , socket_type_t t); + listen_failed_alert(aux::stack_allocator& alloc, string_view iface + , int op, error_code const& ec, socket_type_t t); TORRENT_DEFINE_ALERT_PRIO(listen_failed_alert, 48) @@ -1476,9 +1439,7 @@ namespace libtorrent { // internal fastresume_rejected_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , error_code const& ec - , std::string const& file + , torrent_handle const& h, error_code const& ec, string_view file , char const* op); TORRENT_DEFINE_ALERT(fastresume_rejected_alert, 53) @@ -1649,7 +1610,7 @@ namespace libtorrent { // internal anonymous_mode_alert(aux::stack_allocator& alloc, torrent_handle const& h - , int k, std::string const& s); + , int k, string_view s); TORRENT_DEFINE_ALERT(anonymous_mode_alert, 59) @@ -1690,8 +1651,7 @@ namespace libtorrent { // internal trackerid_alert(aux::stack_allocator& alloc, torrent_handle const& h - , std::string const& u - , const std::string& id); + , string_view u, const std::string& id); TORRENT_DEFINE_ALERT(trackerid_alert, 61) @@ -1727,7 +1687,7 @@ namespace libtorrent { // internal torrent_error_alert(aux::stack_allocator& alloc, torrent_handle const& h - , error_code const& e, std::string const& f); + , error_code const& e, string_view f); TORRENT_DEFINE_ALERT(torrent_error_alert, 64) @@ -1963,12 +1923,8 @@ namespace libtorrent struct TORRENT_EXPORT dht_mutable_item_alert final : alert { dht_mutable_item_alert(aux::stack_allocator& alloc - , std::array k - , std::array sig - , std::uint64_t sequence - , std::string const& s - , entry const& i - , bool a); + , std::array k, std::array sig + , std::uint64_t sequence, string_view s, entry const& i, bool a); TORRENT_DEFINE_ALERT_PRIO(dht_mutable_item_alert, 75) diff --git a/include/libtorrent/stack_allocator.hpp b/include/libtorrent/stack_allocator.hpp index 29c59dad8..2d2bcadcb 100644 --- a/include/libtorrent/stack_allocator.hpp +++ b/include/libtorrent/stack_allocator.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/span.hpp" +#include "libtorrent/string_view.hpp" #include @@ -48,11 +49,12 @@ namespace libtorrent { namespace aux stack_allocator(stack_allocator const&) = delete; stack_allocator& operator=(stack_allocator const&) = delete; - int copy_string(std::string const& str) + int copy_string(string_view str) { int const ret = int(m_storage.size()); - m_storage.resize(ret + str.length() + 1); - std::strcpy(&m_storage[ret], str.c_str()); + m_storage.resize(ret + str.size() + 1); + std::memcpy(&m_storage[ret], str.data(), str.size()); + m_storage[ret + str.length()] = '\0'; return ret; } diff --git a/src/alert.cpp b/src/alert.cpp index cf560d508..c3299bd5d 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -116,8 +116,7 @@ namespace libtorrent { } tracker_alert::tracker_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u) + , torrent_handle const& h, string_view u) : torrent_alert(alloc, h) #ifndef TORRENT_NO_DEPRECATE , url(u) @@ -189,9 +188,7 @@ namespace libtorrent { } file_renamed_alert::file_renamed_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& n - , int idx) + , torrent_handle const& h, string_view n, int const idx) : torrent_alert(alloc, h) #ifndef TORRENT_NO_DEPRECATE , name(n) @@ -283,12 +280,8 @@ namespace libtorrent { } tracker_error_alert::tracker_error_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , int times - , int status - , std::string const& u - , error_code const& e - , std::string const& m) + , torrent_handle const& h, int times, int status, string_view u + , error_code const& e, string_view m) : tracker_alert(alloc, h, u) , times_in_row(times) , status_code(status) @@ -321,9 +314,7 @@ namespace libtorrent { } tracker_warning_alert::tracker_warning_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u - , std::string const& m) + , torrent_handle const& h, string_view u, string_view m) : tracker_alert(alloc, h, u) #ifndef TORRENT_NO_DEPRECATE , msg(m) @@ -348,10 +339,7 @@ namespace libtorrent { } scrape_reply_alert::scrape_reply_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , int incomp - , int comp - , std::string const& u) + , torrent_handle const& h, int incomp, int comp, string_view u) : tracker_alert(alloc, h, u) , incomplete(incomp) , complete(comp) @@ -368,9 +356,7 @@ namespace libtorrent { } scrape_failed_alert::scrape_failed_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u - , error_code const& e) + , torrent_handle const& h, string_view u, error_code const& e) : tracker_alert(alloc, h, u) #ifndef TORRENT_NO_DEPRECATE , msg(convert_from_native(e.message())) @@ -382,9 +368,7 @@ namespace libtorrent { } scrape_failed_alert::scrape_failed_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u - , std::string const& m) + , torrent_handle const& h, string_view u, string_view m) : tracker_alert(alloc, h, u) #ifndef TORRENT_NO_DEPRECATE , msg(m) @@ -411,9 +395,7 @@ namespace libtorrent { } tracker_reply_alert::tracker_reply_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , int np - , std::string const& u) + , torrent_handle const& h, int np, string_view u) : tracker_alert(alloc, h, u) , num_peers(np) { @@ -444,8 +426,7 @@ namespace libtorrent { } tracker_announce_alert::tracker_announce_alert(aux::stack_allocator& alloc - , torrent_handle const& h - , std::string const& u, int e) + , torrent_handle const& h, string_view u, int e) : tracker_alert(alloc, h, u) , event(e) { @@ -656,7 +637,7 @@ namespace libtorrent { } storage_moved_alert::storage_moved_alert(aux::stack_allocator& alloc - , torrent_handle const& h, std::string const& p) + , torrent_handle const& h, string_view p) : torrent_alert(alloc, h) #ifndef TORRENT_NO_DEPRECATE , path(p) @@ -680,11 +661,8 @@ namespace libtorrent { } storage_moved_failed_alert::storage_moved_failed_alert( - aux::stack_allocator& alloc - , torrent_handle const& h - , error_code const& e - , std::string const& f - , char const* op) + aux::stack_allocator& alloc, torrent_handle const& h, error_code const& e + , string_view f, char const* op) : torrent_alert(alloc, h) , error(e) #ifndef TORRENT_NO_DEPRECATE @@ -823,7 +801,7 @@ namespace libtorrent { listen_failed_alert::listen_failed_alert( aux::stack_allocator& alloc - , std::string const& iface + , string_view iface , libtorrent::address const& listen_addr , int listen_port , int op @@ -843,7 +821,7 @@ namespace libtorrent { listen_failed_alert::listen_failed_alert( aux::stack_allocator& alloc - , std::string const& iface + , string_view iface , tcp::endpoint const& ep , int op , error_code const& ec @@ -859,7 +837,7 @@ namespace libtorrent { listen_failed_alert::listen_failed_alert( aux::stack_allocator& alloc - , std::string const& iface + , string_view iface , udp::endpoint const& ep , int op , error_code const& ec @@ -875,7 +853,7 @@ namespace libtorrent { listen_failed_alert::listen_failed_alert( aux::stack_allocator& alloc - , std::string const& iface + , string_view iface , int op , error_code const& ec , socket_type_t t) @@ -1059,7 +1037,7 @@ namespace libtorrent { aux::stack_allocator& alloc , torrent_handle const& h , error_code const& ec - , std::string const& f + , string_view f , char const* op) : torrent_alert(alloc, h) , error(ec) @@ -1204,7 +1182,7 @@ namespace libtorrent { : torrent_alert(alloc, h) {} anonymous_mode_alert::anonymous_mode_alert(aux::stack_allocator& alloc - , torrent_handle const& h, int k, std::string const& s) + , torrent_handle const& h, int k, string_view s) : torrent_alert(alloc, h) , kind(k) , str(s) @@ -1238,7 +1216,7 @@ namespace libtorrent { trackerid_alert::trackerid_alert( aux::stack_allocator& alloc , torrent_handle const& h - , std::string const& u + , string_view u , const std::string& id) : tracker_alert(alloc, h, u) #ifndef TORRENT_NO_DEPRECATE @@ -1272,7 +1250,7 @@ namespace libtorrent { torrent_error_alert::torrent_error_alert( aux::stack_allocator& alloc , torrent_handle const& h - , error_code const& e, std::string const& f) + , error_code const& e, string_view f) : torrent_alert(alloc, h) , error(e) #ifndef TORRENT_NO_DEPRECATE @@ -1550,7 +1528,7 @@ namespace libtorrent { , std::array k , std::array sig , std::uint64_t sequence - , std::string const& s + , string_view s , entry const& i , bool a) : key(k), signature(sig), seq(sequence), salt(s), item(i), authoritative(a) @@ -1756,7 +1734,7 @@ namespace libtorrent { } url_seed_alert::url_seed_alert(aux::stack_allocator& alloc, torrent_handle const& h - , std::string const& u, error_code const& e) + , string_view u, error_code const& e) : torrent_alert(alloc, h) #ifndef TORRENT_NO_DEPRECATE , url(u) @@ -1768,7 +1746,7 @@ namespace libtorrent { {} url_seed_alert::url_seed_alert(aux::stack_allocator& alloc, torrent_handle const& h - , std::string const& u, std::string const& m) + , string_view u, string_view m) : torrent_alert(alloc, h) #ifndef TORRENT_NO_DEPRECATE , url(u) @@ -1800,9 +1778,7 @@ namespace libtorrent { } file_error_alert::file_error_alert(aux::stack_allocator& alloc - , error_code const& ec - , std::string const& f - , char const* op + , error_code const& ec, string_view f, char const* op , torrent_handle const& h) : torrent_alert(alloc, h) #ifndef TORRENT_NO_DEPRECATE