From a70789872e5a0a4377ca61c7399aa5573413174d Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 12 Apr 2008 01:58:55 +0000 Subject: [PATCH] added string length to high performance bdecoder --- include/libtorrent/lazy_entry.hpp | 6 +++++- src/lazy_bdecode.cpp | 8 +++----- test/test_bencoding.cpp | 4 ++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index b40e85b44..a6b27782b 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -71,11 +71,12 @@ namespace libtorrent // ================ // start is a null terminated string - void construct_string(char* start) + void construct_string(char* start, int length) { TORRENT_ASSERT(m_type == none_t); m_type = string_t; m_data.start = start; + m_size =length; } char const* string_value() const @@ -84,6 +85,9 @@ namespace libtorrent return m_data.start; } + int string_length() const + { return m_size; } + // dictionary functions // ==================== diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index 8681f452e..7e454b1f4 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -76,8 +76,7 @@ namespace libtorrent if (stack.size() > depth_limit) return fail_bdecode(); if (start == end) return fail_bdecode(); char t = *start; - *start = 0; // null terminate any previous string - ++start; + *start++ = 0; // null terminate any previous string if (start == end && t != 'e') return fail_bdecode(); switch (top->type()) @@ -97,8 +96,7 @@ namespace libtorrent start += len; stack.push_back(ent); t = *start; - *start = 0; // null terminate any previous string - ++start; + *start++ = 0; // null terminate any previous string break; } case lazy_entry::list_t: @@ -140,7 +138,7 @@ namespace libtorrent start = parse_int(start, end, ':', len); if (start == 0 || start + len + 1 > end || *start != ':') return fail_bdecode(); ++start; - top->construct_string(start); + top->construct_string(start, int(len)); stack.pop_back(); start += len; continue; diff --git a/test/test_bencoding.cpp b/test/test_bencoding.cpp index b006c45d9..34b83dc92 100644 --- a/test/test_bencoding.cpp +++ b/test/test_bencoding.cpp @@ -86,6 +86,7 @@ int test_main() TORRENT_ASSERT(ret == 0); TORRENT_ASSERT(e.type() == lazy_entry::string_t); TORRENT_ASSERT(e.string_value() == std::string("abcdefghijklmnopqrstuvwxyz")); + TORRENT_ASSERT(e.string_length() == 26); } { @@ -99,6 +100,7 @@ int test_main() TORRENT_ASSERT(e.list_at(1)->type() == lazy_entry::string_t); TORRENT_ASSERT(e.list_at(0)->int_value() == 12453); TORRENT_ASSERT(e.list_at(1)->string_value() == std::string("aaa")); + TORRENT_ASSERT(e.list_at(1)->string_length() == 3); } { @@ -112,8 +114,10 @@ int test_main() TORRENT_ASSERT(e.dict_find("a")->int_value() == 12453); TORRENT_ASSERT(e.dict_find("b")->type() == lazy_entry::string_t); TORRENT_ASSERT(e.dict_find("b")->string_value() == std::string("aaa")); + TORRENT_ASSERT(e.dict_find("b")->string_length() == 3); TORRENT_ASSERT(e.dict_find("c")->type() == lazy_entry::string_t); TORRENT_ASSERT(e.dict_find("c")->string_value() == std::string("bbb")); + TORRENT_ASSERT(e.dict_find("c")->string_length() == 3); } return 0; }