From 1dd0e9b280f01750d5e204cca475bd06d30540c4 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 18 Mar 2017 12:19:49 -0400 Subject: [PATCH] use string_view instead of strcmp --- examples/client_test.cpp | 6 ++-- examples/connection_tester.cpp | 15 ++++---- include/libtorrent/enum_net.hpp | 1 + include/libtorrent/string_view.hpp | 8 +++++ simulation/test_fast_extensions.cpp | 6 ++-- src/http_connection.cpp | 2 +- src/i2p_stream.cpp | 53 +++++++++++++++-------------- src/peer_list.cpp | 1 + src/session_stats.cpp | 1 + test/main.cpp | 10 +++--- test/test_file.cpp | 30 ++++++++-------- test/test_http_parser.cpp | 2 +- test/test_stack_allocator.cpp | 12 ++++--- test/web_seed_suite.cpp | 3 +- tools/dht_put.cpp | 12 +++---- 15 files changed, 91 insertions(+), 71 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index fa73325ea..fb0105834 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -66,6 +66,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/create_torrent.hpp" #include "libtorrent/read_resume_data.hpp" #include "libtorrent/write_resume_data.hpp" +#include "libtorrent/string_view.hpp" #include "torrent_view.hpp" #include "session_view.hpp" @@ -1228,7 +1229,7 @@ int main(int argc, char* argv[]) continue; } - if (std::strcmp(argv[i], "--list-settings") == 0) + if (argv[i] == "--list-settings"_sv) { // print all libtorrent settings and exit print_settings(settings_pack::string_type_base @@ -1265,8 +1266,7 @@ int main(int argc, char* argv[]) settings.set_str(sett_name, value); break; case settings_pack::bool_type_base: - if (std::strcmp(value, "0") == 0 - || std::strcmp(value, "1") == 0) + if (value == "0"_sv || value == "1"_sv) { settings.set_bool(sett_name, atoi(value) != 0); } diff --git a/examples/connection_tester.cpp b/examples/connection_tester.cpp index 4cdb41903..107e3bd3c 100644 --- a/examples/connection_tester.cpp +++ b/examples/connection_tester.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/hasher.hpp" #include "libtorrent/socket_io.hpp" #include "libtorrent/file_pool.hpp" +#include "libtorrent/string_view.hpp" #include #include #include @@ -948,7 +949,7 @@ int main(int argc, char* argv[]) } } - if (strcmp(command, "gen-torrent") == 0) + if (command == "gen-torrent"_sv) { std::vector tmp; std::string name = leaf_path(torrent_file); @@ -958,7 +959,7 @@ int main(int argc, char* argv[]) , name.c_str()); FILE* output = stdout; - if (strcmp("-", torrent_file) != 0) + if ("-"_sv != torrent_file) { if( (output = std::fopen(torrent_file, "wb+")) == nullptr) { @@ -974,7 +975,7 @@ int main(int argc, char* argv[]) return 0; } - else if (strcmp(command, "gen-data") == 0) + else if (command == "gen-data"_sv) { error_code ec; torrent_info ti(torrent_file, ec); @@ -986,7 +987,7 @@ int main(int argc, char* argv[]) generate_data(data_path, ti); return 0; } - else if (strcmp(command, "gen-test-torrents") == 0) + else if (command == "gen-test-torrents"_sv) { std::vector buf; for (int i = 0; i < num_torrents; ++i) @@ -1030,15 +1031,15 @@ int main(int argc, char* argv[]) } return 0; } - else if (strcmp(command, "upload") == 0) + else if (command == "upload"_sv) { test_mode = upload_test; } - else if (strcmp(command, "download") == 0) + else if (command == "download"_sv) { test_mode = download_test; } - else if (strcmp(command, "dual") == 0) + else if (command == "dual"_sv) { test_mode = dual_test; } diff --git a/include/libtorrent/enum_net.hpp b/include/libtorrent/enum_net.hpp index a80199144..3ea7d598a 100644 --- a/include/libtorrent/enum_net.hpp +++ b/include/libtorrent/enum_net.hpp @@ -100,6 +100,7 @@ namespace libtorrent // verify the local endpoint of the socket once the connection is established. // the returned address is the ip the socket was bound to (or address_v4::any() // in case SO_BINDTODEVICE succeeded and we don't need to verify it). + // TODO: 3 use string_view for device_name template address bind_socket_to_device(io_service& ios, Socket& sock , boost::asio::ip::tcp const& protocol diff --git a/include/libtorrent/string_view.hpp b/include/libtorrent/string_view.hpp index 37e17438d..9b70a03af 100644 --- a/include/libtorrent/string_view.hpp +++ b/include/libtorrent/string_view.hpp @@ -51,6 +51,14 @@ using wstring_view = boost::wstring_view; } #endif +namespace libtorrent { +inline namespace literals { + constexpr string_view operator""_sv(char const* str, std::size_t len) + { return string_view(str, len); } +} +} + + #include "libtorrent/aux_/disable_warnings_pop.hpp" #endif diff --git a/simulation/test_fast_extensions.cpp b/simulation/test_fast_extensions.cpp index d16ab339a..efcf2f079 100644 --- a/simulation/test_fast_extensions.cpp +++ b/simulation/test_fast_extensions.cpp @@ -41,7 +41,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "fake_peer.hpp" #include "setup_transfer.hpp" // for ep() #include "simulator/utils.hpp" +#include "libtorrent/string_view.hpp" +using namespace libtorrent::literals; namespace lt = libtorrent; template @@ -120,7 +122,7 @@ TORRENT_TEST(allow_fast) } else if (auto l = lt::alert_cast(a)) { - if (strcmp(l->event_type, "ALLOWED_FAST") != 0) return; + if (l->event_type != "ALLOWED_FAST"_sv) return; int const piece = atoi(l->log_message()); // make sure we don't get the same allowed piece more than once @@ -182,7 +184,7 @@ TORRENT_TEST(allow_fast_stress) } else if (auto l = lt::alert_cast(a)) { - if (strcmp(l->event_type, "ALLOWED_FAST") != 0) return; + if (l->event_type != "ALLOWED_FAST"_sv) return; int const piece = atoi(l->log_message()); diff --git a/src/http_connection.cpp b/src/http_connection.cpp index a1cdf58c5..12dadee0b 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -281,7 +281,7 @@ void http_connection::start(std::string const& hostname, int port #if TORRENT_USE_I2P bool is_i2p = false; char const* top_domain = strrchr(hostname.c_str(), '.'); - if (top_domain && strcmp(top_domain, ".i2p") == 0 && i2p_conn) + if (top_domain && top_domain == ".i2p"_sv && i2p_conn) { // this is an i2p name, we need to use the sam connection // to do the name lookup diff --git a/src/i2p_stream.cpp b/src/i2p_stream.cpp index 85e9c7d86..7ef15fe9a 100644 --- a/src/i2p_stream.cpp +++ b/src/i2p_stream.cpp @@ -312,39 +312,42 @@ namespace libtorrent , i2p_category()); // 0-terminate the string and parse it + // TODO: 3 once we've transitioned to string_view, remove the + // 0-termination m_buffer.push_back(0); char* ptr = m_buffer.data(); char* next = ptr; - char const* expect1 = nullptr; - char const* expect2 = nullptr; + string_view expect1; + string_view expect2; switch (m_state) { case read_hello_response: - expect1 = "HELLO"; - expect2 = "REPLY"; + expect1 = "HELLO"_sv; + expect2 = "REPLY"_sv; break; case read_connect_response: case read_accept_response: - expect1 = "STREAM"; - expect2 = "STATUS"; + expect1 = "STREAM"_sv; + expect2 = "STATUS"_sv; break; case read_session_create_response: - expect1 = "SESSION"; - expect2 = "STATUS"; + expect1 = "SESSION"_sv; + expect2 = "STATUS"_sv; break; case read_name_lookup_response: - expect1 = "NAMING"; - expect2 = "REPLY"; + expect1 = "NAMING"_sv; + expect2 = "REPLY"_sv; break; } + // TODO: 3 make string_tokenize return string_views instead ptr = string_tokenize(next, ' ', &next); - if (ptr == nullptr || expect1 == nullptr || std::strcmp(expect1, ptr) != 0) + if (ptr == nullptr || expect1.empty() || expect1 != ptr) { handle_error(invalid_response, h); return; } ptr = string_tokenize(next, ' ', &next); - if (ptr == nullptr || expect2 == nullptr || std::strcmp(expect2, ptr) != 0) + if (ptr == nullptr || expect2.empty() || expect2 != ptr) { handle_error(invalid_response, h); return; } int result = 0; @@ -356,38 +359,38 @@ namespace libtorrent char const* const ptr2 = string_tokenize(next, ' ', &next); if (ptr2 == nullptr) { handle_error(invalid_response, h); return; } - if (std::strcmp("RESULT", name) == 0) + if ("RESULT"_sv == name) { - if (std::strcmp("OK", ptr2) == 0) + if ("OK"_sv == ptr2) result = i2p_error::no_error; - else if (std::strcmp("CANT_REACH_PEER", ptr2) == 0) + else if ("CANT_REACH_PEER"_sv == ptr2) result = i2p_error::cant_reach_peer; - else if (std::strcmp("I2P_ERROR", ptr2) == 0) + else if ("I2P_ERROR"_sv == ptr2) result = i2p_error::i2p_error; - else if (std::strcmp("INVALID_KEY", ptr2) == 0) + else if ("INVALID_KEY"_sv == ptr2) result = i2p_error::invalid_key; - else if (std::strcmp("INVALID_ID", ptr2) == 0) + else if ("INVALID_ID"_sv == ptr2) result = i2p_error::invalid_id; - else if (std::strcmp("TIMEOUT", ptr2) == 0) + else if ("TIMEOUT"_sv == ptr2) result = i2p_error::timeout; - else if (std::strcmp("KEY_NOT_FOUND", ptr2) == 0) + else if ("KEY_NOT_FOUND"_sv == ptr2) result = i2p_error::key_not_found; - else if (std::strcmp("DUPLICATED_ID", ptr2) == 0) + else if ("DUPLICATED_ID"_sv == ptr2) result = i2p_error::duplicated_id; else result = i2p_error::num_errors; // unknown error } - /*else if (std::strcmp("MESSAGE", name) == 0) + /*else if ("MESSAGE" == name) { } - else if (std::strcmp("VERSION", name) == 0) + else if ("VERSION"_sv == name) { }*/ - else if (std::strcmp("VALUE", name) == 0) + else if ("VALUE"_sv == name) { m_name_lookup = ptr2; } - else if (std::strcmp("DESTINATION", name) == 0) + else if ("DESTINATION"_sv == name) { m_dest = ptr2; } diff --git a/src/peer_list.cpp b/src/peer_list.cpp index cf729cd52..a7f991168 100644 --- a/src/peer_list.cpp +++ b/src/peer_list.cpp @@ -1006,6 +1006,7 @@ namespace libtorrent } #if TORRENT_USE_I2P + // TODO: 3 use string_view for destination torrent_peer* peer_list::add_i2p_peer(char const* destination, int src, char flags, torrent_state* state) { TORRENT_ASSERT(is_single_thread()); diff --git a/src/session_stats.cpp b/src/session_stats.cpp index 4a3d445c9..be6453f44 100644 --- a/src/session_stats.cpp +++ b/src/session_stats.cpp @@ -568,6 +568,7 @@ namespace libtorrent return stats; } + // TODO: 3 use string_view for name int find_metric_idx(char const* name) { auto const i = std::find_if(std::begin(metrics), std::end(metrics) diff --git a/test/main.cpp b/test/main.cpp index fe7b79a52..5b695239c 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -273,13 +273,13 @@ EXPORT int main(int argc, char const* argv[]) // pick up options while (argc > 0 && argv[0][0] == '-') { - if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) + if (argv[0] == "-h"_sv || argv[0] == "--help"_sv) { print_usage(executable); return 0; } - if (strcmp(argv[0], "-l") == 0 || strcmp(argv[0], "--list") == 0) + if (argv[0] == "-l"_sv || argv[0] == "--list"_sv) { std::printf("TESTS:\n"); for (int i = 0; i < _g_num_unit_tests; ++i) @@ -289,17 +289,17 @@ EXPORT int main(int argc, char const* argv[]) return 0; } - if (strcmp(argv[0], "-n") == 0 || strcmp(argv[0], "--no-redirect") == 0) + if (argv[0] == "-n"_sv || argv[0] == "--no-redirect"_sv) { redirect_stdout = false; } - if (strcmp(argv[0], "--stderr-redirect") == 0) + if (argv[0] == "--stderr-redirect"_sv) { redirect_stderr = true; } - if (strcmp(argv[0], "-k") == 0 || strcmp(argv[0], "--keep") == 0) + if (argv[0] == "-k"_sv || argv[0] == "--keep"_sv) { keep_files = true; } diff --git a/test/test_file.cpp b/test/test_file.cpp index cfb73199d..80a912d4d 100644 --- a/test/test_file.cpp +++ b/test/test_file.cpp @@ -32,8 +32,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/file.hpp" #include "libtorrent/string_util.hpp" // for split_string +#include "libtorrent/string_view.hpp" #include "test.hpp" -#include // for std::strcmp #include #include #include @@ -252,16 +252,16 @@ TORRENT_TEST(split_string) int ret = split_string(tags, 10, tags_str); TEST_CHECK(ret == 10); - TEST_CHECK(std::strcmp(tags[0], "this") == 0); - TEST_CHECK(std::strcmp(tags[1], "is") == 0); - TEST_CHECK(std::strcmp(tags[2], "a") == 0); - TEST_CHECK(std::strcmp(tags[3], "test") == 0); - TEST_CHECK(std::strcmp(tags[4], "string") == 0); - TEST_CHECK(std::strcmp(tags[5], "to") == 0); - TEST_CHECK(std::strcmp(tags[6], "be") == 0); - TEST_CHECK(std::strcmp(tags[7], "split") == 0); - TEST_CHECK(std::strcmp(tags[8], "and") == 0); - TEST_CHECK(std::strcmp(tags[9], "it") == 0); + TEST_CHECK(tags[0] == "this"_sv); + TEST_CHECK(tags[1] == "is"_sv); + TEST_CHECK(tags[2] == "a"_sv); + TEST_CHECK(tags[3] == "test"_sv); + TEST_CHECK(tags[4] == "string"_sv); + TEST_CHECK(tags[5] == "to"_sv); + TEST_CHECK(tags[6] == "be"_sv); + TEST_CHECK(tags[7] == "split"_sv); + TEST_CHECK(tags[8] == "and"_sv); + TEST_CHECK(tags[9] == "it"_sv); // replace_extension std::string test = "foo.bar"; @@ -303,7 +303,7 @@ TORRENT_TEST(file) if (ec) std::printf("readv failed: [%s] %s\n", ec.category().name(), ec.message().c_str()); TEST_EQUAL(ec, error_code()); - TEST_CHECK(std::strcmp(test_buf, "test") == 0); + TEST_CHECK(test_buf == "test"_sv); f.close(); } @@ -347,7 +347,7 @@ TORRENT_TEST(hard_link) if (ec) std::printf("readv failed: [%s] %s\n", ec.category().name(), ec.message().c_str()); TEST_EQUAL(ec, error_code()); - TEST_CHECK(std::strcmp(test_buf, "abcdefghijklmnopqrstuvwxyz") == 0); + TEST_CHECK(test_buf == "abcdefghijklmnopqrstuvwxyz"_sv); f.close(); remove("original_file", ec); @@ -386,8 +386,8 @@ TORRENT_TEST(coalesce_buffer) , ec.category().name(), ec.message().c_str()); } TEST_EQUAL(ec, error_code()); - TEST_CHECK(std::strcmp(test_buf1, "test") == 0); - TEST_CHECK(std::strcmp(test_buf2, "foobar") == 0); + TEST_CHECK(test_buf1 == "test"_sv); + TEST_CHECK(test_buf2 == "foobar"_sv); f.close(); } diff --git a/test/test_http_parser.cpp b/test/test_http_parser.cpp index 4e369baca..8e260b4e1 100644 --- a/test/test_http_parser.cpp +++ b/test/test_http_parser.cpp @@ -50,7 +50,7 @@ std::tuple feed_bytes(http_parser& parser, char const* str) span recv_buf(str, 0); for (;;) { - int chunk_size = (std::min)(chunks, int(strlen(recv_buf.end()))); + int chunk_size = std::min(chunks, int(strlen(recv_buf.end()))); if (chunk_size == 0) break; recv_buf = span(recv_buf.data(), recv_buf.size() + chunk_size); int payload, protocol; diff --git a/test/test_stack_allocator.cpp b/test/test_stack_allocator.cpp index 23b509ddc..9f31b50d7 100644 --- a/test/test_stack_allocator.cpp +++ b/test/test_stack_allocator.cpp @@ -32,9 +32,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "test.hpp" #include "libtorrent/stack_allocator.hpp" +#include "libtorrent/string_view.hpp" using libtorrent::aux::stack_allocator; using libtorrent::aux::allocation_slot; +using namespace libtorrent::literals; TORRENT_TEST(copy_string) { @@ -46,8 +48,8 @@ TORRENT_TEST(copy_string) allocation_slot const idx2 = a.copy_string(std::string("foobar")); - TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0); - TEST_CHECK(strcmp(a.ptr(idx2), "foobar") == 0); + TEST_CHECK(a.ptr(idx1) == "testing"_sv); + TEST_CHECK(a.ptr(idx2) == "foobar"_sv); } TORRENT_TEST(copy_buffer) @@ -58,7 +60,7 @@ TORRENT_TEST(copy_buffer) // attempt to trigger a reallocation a.allocate(100000); - TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0); + TEST_CHECK(a.ptr(idx1) == "testing"_sv); // attempt zero size allocation allocation_slot const idx2 = a.copy_buffer({}); @@ -101,7 +103,7 @@ TORRENT_TEST(swap) a1.swap(a2); - TEST_CHECK(strcmp(a1.ptr(idx2), "foobar") == 0); - TEST_CHECK(strcmp(a2.ptr(idx1), "testing") == 0); + TEST_CHECK(a1.ptr(idx2) == "foobar"_sv); + TEST_CHECK(a2.ptr(idx1) == "testing"_sv); } diff --git a/test/web_seed_suite.cpp b/test/web_seed_suite.cpp index 12acc8ca1..726c7e4f6 100644 --- a/test/web_seed_suite.cpp +++ b/test/web_seed_suite.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alert_types.hpp" #include "libtorrent/torrent_info.hpp" #include "libtorrent/announce_entry.hpp" +#include "libtorrent/string_view.hpp" #include "test.hpp" #include "setup_transfer.hpp" @@ -283,7 +284,7 @@ int EXPORT run_http_suite(int proxy, char const* protocol, bool test_url_seed save_path += proxy_name[proxy]; error_code ec; - int const port = start_web_server(strcmp(protocol, "https") == 0, chunked_encoding, keepalive); + int const port = start_web_server(protocol == "https"_sv, chunked_encoding, keepalive); std::vector test_cases; diff --git a/tools/dht_put.cpp b/tools/dht_put.cpp index f675c9471..c966a0b1e 100644 --- a/tools/dht_put.cpp +++ b/tools/dht_put.cpp @@ -251,7 +251,7 @@ int main(int argc, char* argv[]) if (argc < 1) usage(); - if (strcmp(argv[0], "dump-key") == 0) + if (argv[0] == "dump-key"_sv) { ++argv; --argc; @@ -260,7 +260,7 @@ int main(int argc, char* argv[]) return dump_key(argv[0]); } - if (strcmp(argv[0], "gen-key") == 0) + if (argv[0] == "gen-key"_sv) { ++argv; --argc; @@ -279,7 +279,7 @@ int main(int argc, char* argv[]) load_dht_state(s); - if (strcmp(argv[0], "get") == 0) + if (argv[0] == "get"_sv) { ++argv; --argc; @@ -311,7 +311,7 @@ int main(int argc, char* argv[]) std::string str = item->item.to_string(); std::printf("%s", str.c_str()); } - else if (strcmp(argv[0], "put") == 0) + else if (argv[0] == "put"_sv) { ++argv; --argc; @@ -329,7 +329,7 @@ int main(int argc, char* argv[]) dht_put_alert* pa = alert_cast(a); std::printf("%s\n", pa->message().c_str()); } - else if (strcmp(argv[0], "mput") == 0) + else if (argv[0] == "mput"_sv) { ++argv; --argc; @@ -365,7 +365,7 @@ int main(int argc, char* argv[]) dht_put_alert* pa = alert_cast(a); std::printf("%s\n", pa->message().c_str()); } - else if (strcmp(argv[0], "mget") == 0) + else if (argv[0] == "mget"_sv) { ++argv; --argc;