From 227b630bd184e2106e6232cf02b5cc6b844bdefb Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Fri, 26 Aug 2016 12:36:09 -0400 Subject: [PATCH] added string_ends_with to replace boost (#1034) added string_ends_with to replace boost --- include/libtorrent/parse_url.hpp | 1 - include/libtorrent/string_util.hpp | 8 +++++--- src/string_util.cpp | 16 +++++++++++++--- src/torrent.cpp | 9 +++------ test/test_string.cpp | 21 +++++++++++++++++++++ 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/include/libtorrent/parse_url.hpp b/include/libtorrent/parse_url.hpp index 5fa129c17..efac49966 100644 --- a/include/libtorrent/parse_url.hpp +++ b/include/libtorrent/parse_url.hpp @@ -51,4 +51,3 @@ namespace libtorrent } #endif - diff --git a/include/libtorrent/string_util.hpp b/include/libtorrent/string_util.hpp index 71e80aa7b..58261d8f6 100644 --- a/include/libtorrent/string_util.hpp +++ b/include/libtorrent/string_util.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_STRING_UTIL_HPP_INCLUDED #include "libtorrent/config.hpp" +#include "libtorrent/string_view.hpp" #include #include @@ -63,6 +64,8 @@ namespace libtorrent TORRENT_EXTRA_EXPORT void url_random(char* begin, char* end); + TORRENT_EXTRA_EXPORT bool string_ends_with(string_view s1, string_view s2); + struct listen_interface_t { std::string device; @@ -79,7 +82,7 @@ namespace libtorrent TORRENT_EXTRA_EXPORT std::string print_listen_interfaces( std::vector const& in); - // this parses the string that's used as the liste_interfaces setting. + // this parses the string that's used as the listen_interfaces setting. // it is a comma-separated list of IP or device names with ports. For // example: "eth0:6881,eth1:6881" or "127.0.0.1:6881" TORRENT_EXTRA_EXPORT void parse_comma_separated_string_port( @@ -112,10 +115,9 @@ namespace libtorrent #if TORRENT_USE_I2P - bool is_i2p_url(std::string const& url); + TORRENT_EXTRA_EXPORT bool is_i2p_url(std::string const& url); #endif } #endif - diff --git a/src/string_util.cpp b/src/string_util.cpp index cb031635e..42337201e 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -36,8 +36,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/error_code.hpp" #include "libtorrent/parse_url.hpp" #include "libtorrent/address.hpp" +#include "libtorrent/assert.hpp" -#include // for tie #include // for malloc #include // for memmov/strcpy/strlen @@ -113,6 +113,9 @@ namespace libtorrent bool string_begins_no_case(char const* s1, char const* s2) { + TORRENT_ASSERT(s1 != nullptr); + TORRENT_ASSERT(s2 != nullptr); + while (*s1 != 0) { if (to_lower(*s1) != to_lower(*s2)) return false; @@ -124,6 +127,9 @@ namespace libtorrent bool string_equal_no_case(char const* s1, char const* s2) { + TORRENT_ASSERT(s1 != nullptr); + TORRENT_ASSERT(s2 != nullptr); + while (to_lower(*s1) == to_lower(*s2)) { if (*s1 == 0) return true; @@ -146,6 +152,11 @@ namespace libtorrent *begin++ = printable[random(sizeof(printable) - 2)]; } + bool string_ends_with(string_view s1, string_view s2) + { + return s1.size() >= s2.size() && std::equal(s2.rbegin(), s2.rend(), s1.rbegin()); + } + char* allocate_string_copy(char const* str) { if (str == nullptr) return nullptr; @@ -398,8 +409,7 @@ namespace libtorrent error_code ec; std::tie(ignore, ignore, hostname, ignore, ignore) = parse_url_components(url, ec); - char const* top_domain = std::strrchr(hostname.c_str(), '.'); - return top_domain && std::strcmp(top_domain, ".i2p") == 0; + return string_ends_with(hostname, ".i2p"); } #endif diff --git a/src/torrent.cpp b/src/torrent.cpp index ac312053c..5f15d0408 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -47,9 +47,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_push.hpp" #include -#if TORRENT_USE_I2P -# include -#endif #ifdef TORRENT_USE_OPENSSL #include "libtorrent/ssl_stream.hpp" @@ -3317,11 +3314,11 @@ namespace libtorrent continue; #if TORRENT_USE_I2P - if (r.i2pconn && boost::algorithm::ends_with(i->hostname, ".i2p")) + if (r.i2pconn && string_ends_with(i->hostname, ".i2p")) { - // this is an i2p name, we need to use the sam connection + // this is an i2p name, we need to use the SAM connection // to do the name lookup - if (boost::algorithm::ends_with(i->hostname, ".b32.i2p")) + if (string_ends_with(i->hostname, ".b32.i2p")) { ADD_OUTSTANDING_ASYNC("torrent::on_i2p_resolve"); r.i2pconn->async_name_lookup(i->hostname.c_str() diff --git a/test/test_string.cpp b/test/test_string.cpp index 6cb5a22db..0e07b6797 100644 --- a/test/test_string.cpp +++ b/test/test_string.cpp @@ -106,9 +106,21 @@ TORRENT_TEST(string_equal_no_case) TEST_CHECK(string_equal_no_case("foobar", "foobar")); TEST_CHECK(!string_equal_no_case("foobar", "foobar ")); TEST_CHECK(!string_equal_no_case("foobar", "F00")); + TEST_CHECK(!string_equal_no_case("foobar", "foo")); + TEST_CHECK(!string_equal_no_case("foo", "foobar")); TEST_CHECK(string_begins_no_case("foobar", "FoobAR --")); + TEST_CHECK(string_begins_no_case("foo", "foobar")); TEST_CHECK(!string_begins_no_case("foobar", "F00")); + TEST_CHECK(!string_begins_no_case("foobar", "foo")); + + TEST_CHECK(string_ends_with("foobar", "bar")); + TEST_CHECK(string_ends_with("name.txt", ".txt")); + TEST_CHECK(string_ends_with("name.a.b", ".a.b")); + TEST_CHECK(!string_ends_with("-- FoobAR", "foobar")); + TEST_CHECK(!string_ends_with("foobar", "F00")); + TEST_CHECK(!string_ends_with("foobar", "foo")); + TEST_CHECK(!string_ends_with("foo", "foobar")); } TORRENT_TEST(to_string) @@ -366,3 +378,12 @@ TORRENT_TEST(tokenize) , convert_to_native("foo") + convert_to_native("bar")); } +#if TORRENT_USE_I2P +TORRENT_TEST(i2p_url) +{ + TEST_CHECK(is_i2p_url("http://a.i2p/a")); + TEST_CHECK(!is_i2p_url("http://a.I2P/a")); + TEST_CHECK(!is_i2p_url("http://c.i3p")); + TEST_CHECK(!is_i2p_url("http://i2p/foo bar")); +} +#endif