From 7d68c40a7746121c9e709c173f738c64cfca7603 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 5 Nov 2011 12:15:34 +0900 Subject: [PATCH] Made decoderawstring return pair of iterators, not string. --- src/ValueBase.h | 11 +++++++++++ src/bencode2.cc | 14 ++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/ValueBase.h b/src/ValueBase.h index 763114d4..b7b340d9 100644 --- a/src/ValueBase.h +++ b/src/ValueBase.h @@ -86,6 +86,11 @@ public: String(const unsigned char* data, size_t length); + template + String(InputIterator first, InputIterator last) + : str_(first, last) + {} + String(); ~String(); @@ -104,6 +109,12 @@ public: static SharedHandle g(const unsigned char* data, size_t length); + template + static SharedHandle g(InputIterator first, InputIterator last) + { + return SharedHandle(new String(first, last)); + } + virtual void accept(ValueBaseVisitor& visitor) const; private: ValueType str_; diff --git a/src/bencode2.cc b/src/bencode2.cc index 1db0ddbe..74198b0d 100644 --- a/src/bencode2.cc +++ b/src/bencode2.cc @@ -57,7 +57,7 @@ decodeiter namespace { template -std::pair +std::pair, InputIterator> decoderawstring(InputIterator first, InputIterator last) { InputIterator i = first; @@ -77,7 +77,7 @@ decoderawstring(InputIterator first, InputIterator last) len, static_cast(last-i)), error_code::BENCODE_PARSE_ERROR); } - return std::make_pair(std::string(i, i+len), i+len); + return std::make_pair(std::make_pair(i, i+len), i+len); } } // namespace @@ -86,8 +86,9 @@ template std::pair, InputIterator> decodestring(InputIterator first, InputIterator last) { - std::pair r = decoderawstring(first, last); - return std::make_pair(String::g(r.first), r.second); + std::pair, InputIterator> r = + decoderawstring(first, last); + return std::make_pair(String::g(r.first.first, r.first.second), r.second); } } // namespace @@ -121,10 +122,11 @@ decodedict if(*first == 'e') { return std::make_pair(dict, ++first); } else { - std::pair keyp = decoderawstring(first, last); + std::pair, InputIterator> keyp = + decoderawstring(first, last); std::pair, InputIterator> r = decodeiter(keyp.second, last, depth); - dict->put(keyp.first, r.first); + dict->put(std::string(keyp.first.first, keyp.first.second), r.first); first = r.second; } }