From 269e384c2c1fa0649aa83a9f82485916cf5bc123 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 21 Sep 2016 19:59:43 -0700 Subject: [PATCH] use string_view in create_torrent (#1120) use string_view in create_torrent --- include/libtorrent/aux_/escape_string.hpp | 4 ++-- include/libtorrent/create_torrent.hpp | 16 ++++++------- src/create_torrent.cpp | 29 ++++++++++++----------- src/escape_string.cpp | 2 +- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/include/libtorrent/aux_/escape_string.hpp b/include/libtorrent/aux_/escape_string.hpp index 7245319f4..90abcad58 100644 --- a/include/libtorrent/aux_/escape_string.hpp +++ b/include/libtorrent/aux_/escape_string.hpp @@ -93,7 +93,7 @@ namespace libtorrent TORRENT_EXTRA_EXPORT std::wstring convert_to_wstring(std::string const& s); TORRENT_EXTRA_EXPORT std::string convert_from_wstring(std::wstring const& s); #endif - + #if TORRENT_USE_ICONV || TORRENT_USE_LOCALE || defined TORRENT_WINDOWS TORRENT_EXTRA_EXPORT std::string convert_to_native(std::string const& s); TORRENT_EXTRA_EXPORT std::string convert_from_native(std::string const& s); @@ -102,7 +102,7 @@ namespace libtorrent inline std::string const& convert_to_native(std::string const& s) { return s; } // internal inline std::string const& convert_from_native(std::string const& s) { return s; } -#endif +#endif } #endif // TORRENT_ESCAPE_STRING_HPP_INCLUDED diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 37ca0b688..ed15b9c84 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -38,7 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/storage.hpp" #include "libtorrent/hasher.hpp" -#include "libtorrent/file.hpp" // for combine_path etc. +#include "libtorrent/string_view.hpp" #include #include @@ -233,8 +233,8 @@ namespace libtorrent // files of the torrent in it. // // The second function, ``add_http_seed()`` adds an HTTP seed instead. - void add_url_seed(std::string const& url); - void add_http_seed(std::string const& url); + void add_url_seed(string_view url); + void add_http_seed(string_view url); // This adds a DHT node to the torrent. This especially useful if you're creating a // tracker less torrent. It can be used by clients to bootstrap their DHT node from. @@ -248,16 +248,16 @@ namespace libtorrent // info-hash. The tier is the fallback priority of the tracker. All trackers with tier 0 are // tried first (in any order). If all fail, trackers with tier 1 are tried. If all of those // fail, trackers with tier 2 are tried, and so on. - void add_tracker(std::string const& url, int tier = 0); + void add_tracker(string_view url, int tier = 0); // This function sets an X.509 certificate in PEM format to the torrent. This makes the // torrent an *SSL torrent*. An SSL torrent requires that each peer has a valid certificate // signed by this root certificate. For SSL torrents, all peers are connecting over SSL // connections. For more information, see the section on ssl-torrents_. // - // The string is not the path to the cert, it's the actual content of the certificate, - // loaded into a std::string. - void set_root_cert(std::string const& pem); + // The string is not the path to the cert, it's the actual content of the + // certificate. + void set_root_cert(string_view pem); // Sets and queries the private flag of the torrent. // Torrents with the private flag set ask clients to not use any other @@ -292,7 +292,7 @@ namespace libtorrent // // .. _`BEP 38`: http://www.bittorrent.org/beps/bep_0038.html void add_similar_torrent(sha1_hash ih); - void add_collection(std::string c); + void add_collection(string_view c); private: diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index ffda649b9..29ffd225e 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/announce_entry.hpp" #include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/alert_manager.hpp" +#include "libtorrent/file.hpp" // for combine_path etc. #include #include @@ -59,15 +60,15 @@ namespace libtorrent inline bool ignore_subdir(std::string const& leaf) { return leaf == ".." || leaf == "."; } - int get_file_attributes(std::string const& p) + int get_file_attributes(string_view p) { #ifdef TORRENT_WINDOWS WIN32_FILE_ATTRIBUTE_DATA attr; #if TORRENT_USE_WSTRING - std::wstring path = convert_to_wstring(p); + std::wstring path = convert_to_wstring(p.to_string()); GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &attr); #else - std::string path = convert_to_native(p); + std::string path = convert_to_native(p.to_string()); GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &attr); #endif // TORRENT_USE_WSTRING if (attr.dwFileAttributes == INVALID_FILE_ATTRIBUTES) return 0; @@ -75,7 +76,7 @@ namespace libtorrent return 0; #else struct stat s; - if (lstat(convert_to_native(p).c_str(), &s) < 0) return 0; + if (lstat(convert_to_native(p.to_string()).c_str(), &s) < 0) return 0; int file_attr = 0; if (s.st_mode & S_IXUSR) file_attr += file_storage::attribute_executable; @@ -665,18 +666,18 @@ namespace libtorrent return dict; } - void create_torrent::add_tracker(std::string const& url, int tier) + void create_torrent::add_tracker(string_view url, int const tier) { - m_urls.push_back(announce_entry(url, tier)); + m_urls.push_back(announce_entry(url.to_string(), tier)); std::sort(m_urls.begin(), m_urls.end() , [] (announce_entry const& lhs, announce_entry const& rhs) { return lhs.second < rhs.second; } ); } - void create_torrent::set_root_cert(std::string const& cert) + void create_torrent::set_root_cert(string_view cert) { - m_root_cert = cert; + m_root_cert.assign(cert.data(), cert.size()); } void create_torrent::add_similar_torrent(sha1_hash ih) @@ -684,9 +685,9 @@ namespace libtorrent m_similar.push_back(ih); } - void create_torrent::add_collection(std::string c) + void create_torrent::add_collection(string_view c) { - m_collections.push_back(c); + m_collections.emplace_back(c); } void create_torrent::set_hash(int index, sha1_hash const& h) @@ -709,14 +710,14 @@ namespace libtorrent m_nodes.push_back(node); } - void create_torrent::add_url_seed(std::string const& url) + void create_torrent::add_url_seed(string_view url) { - m_url_seeds.push_back(url); + m_url_seeds.emplace_back(url); } - void create_torrent::add_http_seed(std::string const& url) + void create_torrent::add_http_seed(string_view url) { - m_http_seeds.push_back(url); + m_http_seeds.emplace_back(url); } void create_torrent::set_comment(char const* str) diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 58066a935..ade676629 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -281,7 +281,7 @@ namespace libtorrent unsigned char inbuf[3]; unsigned char outbuf[4]; - + std::string ret; for (std::string::const_iterator i = s.begin(); i != s.end();) {