From 53ff169db12c2a5a0cc2d8fb21a6981d67463c84 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sun, 3 Sep 2017 16:24:16 +0200 Subject: [PATCH] use string_view in find_metric_idx --- bindings/python/src/session.cpp | 7 ++++++- include/libtorrent/session_stats.hpp | 4 ++-- src/session_stats.cpp | 6 ++---- test/test_session.cpp | 4 ++++ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index c600de714..b0d980f7a 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -561,6 +561,11 @@ namespace return p; } + int find_metric_idx_wrap(char const* name) + { + return lt::find_metric_idx(name); + } + } // namespace unnamed struct dummy1 {}; @@ -994,7 +999,7 @@ void bind_session() ; def("session_stats_metrics", session_stats_metrics); - def("find_metric_idx", find_metric_idx); + def("find_metric_idx", find_metric_idx_wrap); scope().attr("create_ut_metadata_plugin") = "ut_metadata"; scope().attr("create_ut_pex_plugin") = "ut_pex"; diff --git a/include/libtorrent/session_stats.hpp b/include/libtorrent/session_stats.hpp index b22dec75e..dccd50899 100644 --- a/include/libtorrent/session_stats.hpp +++ b/include/libtorrent/session_stats.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_SESSION_STATS_HPP_INCLUDED #include "libtorrent/config.hpp" +#include "libtorrent/string_view.hpp" #include @@ -59,8 +60,7 @@ namespace libtorrent { // given a name of a metric, this function returns the counter index of it, // or -1 if it could not be found. The counter index is the index into the // values array returned by session_stats_alert. - TORRENT_EXPORT int find_metric_idx(char const* name); - + TORRENT_EXPORT int find_metric_idx(string_view name); } #endif diff --git a/src/session_stats.cpp b/src/session_stats.cpp index 3d56e1d50..d8b95bbd6 100644 --- a/src/session_stats.cpp +++ b/src/session_stats.cpp @@ -571,15 +571,13 @@ namespace { return stats; } - // TODO: 3 use string_view for name - int find_metric_idx(char const* name) + int find_metric_idx(string_view name) { auto const i = std::find_if(std::begin(metrics), std::end(metrics) , [name](stats_metric_impl const& metr) - { return std::strcmp(metr.name, name) == 0; }); + { return metr.name == name; }); if (i == std::end(metrics)) return -1; return i->value_index; } - } diff --git a/test/test_session.cpp b/test/test_session.cpp index bcb444f1a..e27c1b8af 100644 --- a/test/test_session.cpp +++ b/test/test_session.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bdecode.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/torrent_info.hpp" +#include "libtorrent/session_stats.hpp" #include "settings.hpp" #include @@ -218,6 +219,9 @@ TORRENT_TEST(session_stats) { TEST_EQUAL(stats[i].value_index, i); } + + TEST_EQUAL(lt::find_metric_idx("peer.incoming_connections") + , lt::counters::incoming_connections); } TORRENT_TEST(paused_session)