added string length to high performance bdecoder

This commit is contained in:
Arvid Norberg
2008-04-12 01:58:55 +00:00
parent 42f55adcce
commit a70789872e
3 changed files with 12 additions and 6 deletions

View File

@@ -71,11 +71,12 @@ namespace libtorrent
// ================ // ================
// start is a null terminated string // start is a null terminated string
void construct_string(char* start) void construct_string(char* start, int length)
{ {
TORRENT_ASSERT(m_type == none_t); TORRENT_ASSERT(m_type == none_t);
m_type = string_t; m_type = string_t;
m_data.start = start; m_data.start = start;
m_size =length;
} }
char const* string_value() const char const* string_value() const
@@ -84,6 +85,9 @@ namespace libtorrent
return m_data.start; return m_data.start;
} }
int string_length() const
{ return m_size; }
// dictionary functions // dictionary functions
// ==================== // ====================

View File

@@ -76,8 +76,7 @@ namespace libtorrent
if (stack.size() > depth_limit) return fail_bdecode(); if (stack.size() > depth_limit) return fail_bdecode();
if (start == end) return fail_bdecode(); if (start == end) return fail_bdecode();
char t = *start; char t = *start;
*start = 0; // null terminate any previous string *start++ = 0; // null terminate any previous string
++start;
if (start == end && t != 'e') return fail_bdecode(); if (start == end && t != 'e') return fail_bdecode();
switch (top->type()) switch (top->type())
@@ -97,8 +96,7 @@ namespace libtorrent
start += len; start += len;
stack.push_back(ent); stack.push_back(ent);
t = *start; t = *start;
*start = 0; // null terminate any previous string *start++ = 0; // null terminate any previous string
++start;
break; break;
} }
case lazy_entry::list_t: case lazy_entry::list_t:
@@ -140,7 +138,7 @@ namespace libtorrent
start = parse_int(start, end, ':', len); start = parse_int(start, end, ':', len);
if (start == 0 || start + len + 1 > end || *start != ':') return fail_bdecode(); if (start == 0 || start + len + 1 > end || *start != ':') return fail_bdecode();
++start; ++start;
top->construct_string(start); top->construct_string(start, int(len));
stack.pop_back(); stack.pop_back();
start += len; start += len;
continue; continue;

View File

@@ -86,6 +86,7 @@ int test_main()
TORRENT_ASSERT(ret == 0); TORRENT_ASSERT(ret == 0);
TORRENT_ASSERT(e.type() == lazy_entry::string_t); TORRENT_ASSERT(e.type() == lazy_entry::string_t);
TORRENT_ASSERT(e.string_value() == std::string("abcdefghijklmnopqrstuvwxyz")); 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(1)->type() == lazy_entry::string_t);
TORRENT_ASSERT(e.list_at(0)->int_value() == 12453); 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_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("a")->int_value() == 12453);
TORRENT_ASSERT(e.dict_find("b")->type() == lazy_entry::string_t); 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_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")->type() == lazy_entry::string_t);
TORRENT_ASSERT(e.dict_find("c")->string_value() == std::string("bbb")); TORRENT_ASSERT(e.dict_find("c")->string_value() == std::string("bbb"));
TORRENT_ASSERT(e.dict_find("c")->string_length() == 3);
} }
return 0; return 0;
} }