use string_view instead of strcmp

This commit is contained in:
arvidn 2017-03-18 12:19:49 -04:00 committed by Arvid Norberg
parent 642cfa387d
commit 1dd0e9b280
15 changed files with 91 additions and 71 deletions

View File

@ -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);
}

View File

@ -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 <random>
#include <cstring>
#include <thread>
@ -948,7 +949,7 @@ int main(int argc, char* argv[])
}
}
if (strcmp(command, "gen-torrent") == 0)
if (command == "gen-torrent"_sv)
{
std::vector<char> 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<char> 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;
}

View File

@ -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 <class Socket>
address bind_socket_to_device(io_service& ios, Socket& sock
, boost::asio::ip::tcp const& protocol

View File

@ -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

View File

@ -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 <typename Sett, typename Alert>
@ -120,7 +122,7 @@ TORRENT_TEST(allow_fast)
}
else if (auto l = lt::alert_cast<lt::peer_log_alert>(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<lt::peer_log_alert>(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());

View File

@ -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

View File

@ -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;
}

View File

@ -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());

View File

@ -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)

View File

@ -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;
}

View File

@ -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 <cstring> // for std::strcmp
#include <vector>
#include <set>
#include <thread>
@ -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();
}

View File

@ -50,7 +50,7 @@ std::tuple<int, int, bool> feed_bytes(http_parser& parser, char const* str)
span<char const> 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<char const>(recv_buf.data(), recv_buf.size() + chunk_size);
int payload, protocol;

View File

@ -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);
}

View File

@ -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<torrent_args> test_cases;

View File

@ -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<dht_put_alert>(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<dht_put_alert>(a);
std::printf("%s\n", pa->message().c_str());
}
else if (strcmp(argv[0], "mget") == 0)
else if (argv[0] == "mget"_sv)
{
++argv;
--argc;