diff --git a/ChangeLog b/ChangeLog index 5ee73960..89771276 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2009-10-18 Tatsuhiro Tsujikawa + + Replaced Util::slice() with split() + * src/AbstractCommand.cc + * src/CookieParser.cc + * src/File.cc + * src/HttpHeaderProcessor.cc + * src/HttpRequest.cc + * src/Metalink2RequestGroup.cc + * src/MetalinkParserController.cc + * src/Netrc.cc + * src/NsCookieParser.cc + * src/ParameterizedStringParser.cc + * src/ServerStatMan.cc + * src/UriListParser.cc + * src/Util.cc + * src/Util.h + * src/bittorrent_helper.cc + * test/UtilTest.cc + 2009-10-18 Tatsuhiro Tsujikawa Use request->getDir() instead of "/" for embedded user/pass in diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index b0e74a42..0f0511d5 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -414,8 +414,8 @@ public: static bool inNoProxy(const SharedHandle& req, const std::string& noProxy) { - std::deque entries; - Util::slice(entries, noProxy, ',', true); + std::vector entries; + split(noProxy, std::back_inserter(entries), ",", true); if(entries.empty()) { return false; } diff --git a/src/CookieParser.cc b/src/CookieParser.cc index 0f49f6e5..c0f2775d 100644 --- a/src/CookieParser.cc +++ b/src/CookieParser.cc @@ -39,6 +39,7 @@ #include #include #include +#include #include "Util.h" #include "A2STR.h" @@ -61,8 +62,8 @@ Cookie CookieParser::parse(const std::string& cookieStr) const Cookie CookieParser::parse(const std::string& cookieStr, const std::string& defaultDomain, const std::string& defaultPath) const { - std::deque terms; - Util::slice(terms, Util::trim(cookieStr), ';', true); + std::vector terms; + split(Util::trim(cookieStr), std::back_inserter(terms), ";", true); if(terms.empty()) { return Cookie(); } @@ -73,7 +74,7 @@ Cookie CookieParser::parse(const std::string& cookieStr, const std::string& defa values[C_DOMAIN] = defaultDomain; values[C_PATH] = defaultPath; - for(std::deque::iterator itr = terms.begin()+1; + for(std::vector::iterator itr = terms.begin()+1; itr != terms.end(); ++itr) { std::pair nv; Util::split(nv, *itr, '='); diff --git a/src/File.cc b/src/File.cc index bcd593a1..8957a954 100644 --- a/src/File.cc +++ b/src/File.cc @@ -39,7 +39,7 @@ #include #include -#include +#include #include #include @@ -105,8 +105,8 @@ bool File::mkdirs() { if(isDir()) { return false; } - std::deque dirs; - Util::slice(dirs, name, '/'); + std::vector dirs; + split(name, std::back_inserter(dirs), "/"); if(!dirs.size()) { return true; } @@ -115,8 +115,8 @@ bool File::mkdirs() { if(Util::startsWith(name, A2STR::SLASH_C)) { accDir = A2STR::SLASH_C; } - for(std::deque::const_iterator itr = dirs.begin(); itr != dirs.end(); - ++itr, accDir += A2STR::SLASH_C) { + for(std::vector::const_iterator itr = dirs.begin(); + itr != dirs.end(); ++itr, accDir += A2STR::SLASH_C) { accDir += *itr; if(File(accDir).isDir()) { continue; diff --git a/src/HttpHeaderProcessor.cc b/src/HttpHeaderProcessor.cc index 298b936e..10e472a2 100644 --- a/src/HttpHeaderProcessor.cc +++ b/src/HttpHeaderProcessor.cc @@ -35,6 +35,7 @@ #include "HttpHeaderProcessor.h" #include +#include #include "HttpHeader.h" #include "message.h" @@ -124,8 +125,8 @@ SharedHandle HttpHeaderProcessor::getHttpRequestHeader() delimpos < 14) { throw DL_RETRY_EX(EX_NO_STATUS_HEADER); } - std::deque firstLine; - Util::slice(firstLine, _buf.substr(0, delimpos), ' ', true); + std::vector firstLine; + split(_buf.substr(0, delimpos), std::back_inserter(firstLine), " ", true); if(firstLine.size() != 3) { throw DL_ABORT_EX("Malformed HTTP request header."); } diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index 63bdc557..7bf407dc 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -36,6 +36,7 @@ #include #include +#include #include "Segment.h" #include "Range.h" @@ -276,8 +277,8 @@ void HttpRequest::disableContentEncoding() void HttpRequest::addHeader(const std::string& headersString) { - std::deque headers; - Util::slice(headers, headersString, '\n', true); + std::vector headers; + split(headersString, std::back_inserter(headers), "\n", true); _headers.insert(_headers.end(), headers.begin(), headers.end()); } diff --git a/src/Metalink2RequestGroup.cc b/src/Metalink2RequestGroup.cc index 9ca59766..dabd04a6 100644 --- a/src/Metalink2RequestGroup.cc +++ b/src/Metalink2RequestGroup.cc @@ -143,7 +143,8 @@ Metalink2RequestGroup::createRequestGroup SharedHandle& entry = *itr; if(option->defined(PREF_METALINK_LOCATION)) { std::deque locations; - Util::slice(locations, option->get(PREF_METALINK_LOCATION), ',', true); + split(option->get(PREF_METALINK_LOCATION), std::back_inserter(locations), + ",", true); entry->setLocationPreference(locations, 100); } if(option->get(PREF_METALINK_PREFERRED_PROTOCOL) != V_NONE) { diff --git a/src/MetalinkParserController.cc b/src/MetalinkParserController.cc index 94a78d09..71b45e75 100644 --- a/src/MetalinkParserController.cc +++ b/src/MetalinkParserController.cc @@ -35,6 +35,7 @@ #include "MetalinkParserController.h" #include +#include #include "Metalinker.h" #include "MetalinkEntry.h" @@ -75,8 +76,8 @@ void MetalinkParserController::setFileNameOfEntry(const std::string& filename) if(_tEntry.isNull()) { return; } - std::deque elements; - Util::slice(elements, filename, '/'); + std::vector elements; + split(filename, std::back_inserter(elements), "/"); std::string path = Util::joinPath(elements.begin(), elements.end()); if(_tEntry->file.isNull()) { diff --git a/src/Netrc.cc b/src/Netrc.cc index 453cddbd..d5332945 100644 --- a/src/Netrc.cc +++ b/src/Netrc.cc @@ -93,7 +93,7 @@ void Netrc::parse(const std::string& path) continue; } std::vector tokens; - Util::split(line, std::back_inserter(tokens), " \t", true); + split(line, std::back_inserter(tokens), " \t", true); for(std::vector::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter) { const std::string& token = *iter; diff --git a/src/NsCookieParser.cc b/src/NsCookieParser.cc index 4a146932..962dcef1 100644 --- a/src/NsCookieParser.cc +++ b/src/NsCookieParser.cc @@ -35,6 +35,7 @@ #include "NsCookieParser.h" #include +#include #include "Util.h" #include "A2STR.h" @@ -51,8 +52,8 @@ static const std::string C_TRUE("TRUE"); static Cookie parseNsCookie(const std::string& nsCookieStr) { - std::deque vs; - Util::slice(vs, nsCookieStr, '\t', true); + std::vector vs; + split(nsCookieStr, std::back_inserter(vs), "\t", true); if(vs.size() < 6 ) { return Cookie(); } diff --git a/src/ParameterizedStringParser.cc b/src/ParameterizedStringParser.cc index 870f4c1b..74f81ce4 100644 --- a/src/ParameterizedStringParser.cc +++ b/src/ParameterizedStringParser.cc @@ -91,7 +91,8 @@ PStringDatumHandle ParameterizedStringParser::createSelect(const std::string& sr throw DL_ABORT_EX("Missing '}' in the parameterized string."); } std::deque values; - Util::slice(values, src.substr(offset, rightParenIndex-offset), ',', true); + split(src.substr(offset, rightParenIndex-offset), std::back_inserter(values), + ",", true); if(values.empty()) { throw DL_ABORT_EX("Empty {} is not allowed."); } diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc index 8948fa2c..f8422d7a 100644 --- a/src/ServerStatMan.cc +++ b/src/ServerStatMan.cc @@ -38,6 +38,7 @@ #include #include #include +#include #include "ServerStat.h" #include "Util.h" @@ -101,10 +102,10 @@ bool ServerStatMan::load(std::istream& in) if(line.empty()) { continue; } - std::deque items; - Util::slice(items, line, ','); + std::vector items; + split(line, std::back_inserter(items), ","); std::map m; - for(std::deque::const_iterator i = items.begin(); + for(std::vector::const_iterator i = items.begin(); i != items.end(); ++i) { std::pair p = Util::split(*i, "="); Util::trimSelf(p.first); diff --git a/src/UriListParser.cc b/src/UriListParser.cc index f1bcf4a2..14d6d52c 100644 --- a/src/UriListParser.cc +++ b/src/UriListParser.cc @@ -74,7 +74,7 @@ void UriListParser::parseNext(std::deque& uris, Option& op) } do { if(!Util::trim(_line).empty()) { - Util::slice(uris, _line, '\t', true); + split(_line, std::back_inserter(uris), "\t", true); getOptions(op); return; } diff --git a/src/Util.cc b/src/Util.cc index d52cdd3f..08412da8 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -141,31 +141,6 @@ int32_t Util::difftvsec(struct timeval tv1, struct timeval tv2) { return tv1.tv_sec-tv2.tv_sec; } -void Util::slice(std::deque& result, const std::string& src, char delim, bool doTrim) { - std::string::size_type p = 0; - while(1) { - std::string::size_type np = src.find(delim, p); - if(np == std::string::npos) { - std::string term = src.substr(p); - if(doTrim) { - term = trim(term); - } - if(term.size()) { - result.push_back(term); - } - break; - } - std::string term = src.substr(p, np-p); - if(doTrim) { - term = trim(term); - } - p = np+1; - if(term.size()) { - result.push_back(term); - } - } -} - bool Util::startsWith(const std::string& target, const std::string& part) { if(target.size() < part.size()) { return false; diff --git a/src/Util.h b/src/Util.h index 4508f14c..06ddaf1f 100644 --- a/src/Util.h +++ b/src/Util.h @@ -133,43 +133,6 @@ public: */ static int64_t difftv(struct timeval tv1, struct timeval tv2); static int32_t difftvsec(struct timeval tv1, struct timeval tv2); - /** - * Take a string src which is a deliminated list and add its elements - * into result. result is not cleared before conversion begins. - */ - static void slice(std::deque& result, const std::string& src, - char delim, bool trim = false); - - template - static OutputIterator split(const std::string& src, OutputIterator out, - const std::string& delims, bool doTrim = false) - { - std::string::size_type p = 0; - while(1) { - std::string::size_type np = src.find_first_of(delims, p); - if(np == std::string::npos) { - std::string term = src.substr(p); - if(doTrim) { - term = trim(term); - } - if(!term.empty()) { - *out = term; - ++out; - } - break; - } - std::string term = src.substr(p, np-p); - if(doTrim) { - term = trim(term); - } - p = np+1; - if(!term.empty()) { - *out = term; - ++out; - } - } - return out; - } static const std::string DEFAULT_TRIM_CHARSET; @@ -334,6 +297,41 @@ public: static std::map createIndexPathMap(std::istream& i); }; +/** + * Take a string src which is a deliminated list and add its elements + * into result. result is stored in out. + */ +template +static OutputIterator split(const std::string& src, OutputIterator out, + const std::string& delims, bool doTrim = false) +{ + std::string::size_type p = 0; + while(1) { + std::string::size_type np = src.find_first_of(delims, p); + if(np == std::string::npos) { + std::string term = src.substr(p); + if(doTrim) { + term = Util::trim(term); + } + if(!term.empty()) { + *out = term; + ++out; + } + break; + } + std::string term = src.substr(p, np-p); + if(doTrim) { + term = Util::trim(term); + } + p = np+1; + if(!term.empty()) { + *out = term; + ++out; + } + } + return out; +} + } // namespace aria2 #endif // _D_UTIL_H_ diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index a161a98a..27400212 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -195,9 +195,9 @@ static void extractFileEntries } const BDE& nameData = infoDict[nameKey]; if(nameData.isString()) { - // Slice path by '/' just in case nasty ".." is included in name - std::deque pathelems; - Util::slice(pathelems, nameData.s(), '/'); + // Split path by '/' just in case nasty ".." is included in name + std::vector pathelems; + split(nameData.s(), std::back_inserter(pathelems), "/"); name = Util::joinPath(pathelems.begin(), pathelems.end()); torrent[NAME] = nameData.s(); } else { @@ -248,8 +248,8 @@ static void extractFileEntries strappend(path, "/", Util::joinPath(pathelem.begin(), pathelem.end())); // Split path with '/' again because each pathList element can // contain "/" inside. - std::deque elements; - Util::slice(elements, path, '/'); + std::vector elements; + split(path, std::back_inserter(elements), "/"); path = Util::joinPath(elements.begin(), elements.end()); std::deque uris; diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 3196e125..dfb03ec8 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -20,7 +20,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(UtilTest); CPPUNIT_TEST(testTrim); CPPUNIT_TEST(testSplit); - CPPUNIT_TEST(testSlice); + CPPUNIT_TEST(testSplit_many); CPPUNIT_TEST(testEndsWith); CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testStartsWith); @@ -63,7 +63,7 @@ public: void testTrim(); void testSplit(); - void testSlice(); + void testSplit_many(); void testEndsWith(); void testReplace(); void testStartsWith(); @@ -140,12 +140,10 @@ void UtilTest::testSplit() { CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second); } -void UtilTest::testSlice() { +void UtilTest::testSplit_many() { std::deque v1; - Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true); - CPPUNIT_ASSERT_EQUAL(3, (int)v1.size()); - v1.clear(); - Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true); + split("name1=value1; name2=value2; name3=value3", std::back_inserter(v1), + ";", true); CPPUNIT_ASSERT_EQUAL(3, (int)v1.size()); std::deque::iterator itr = v1.begin(); CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++); @@ -154,7 +152,8 @@ void UtilTest::testSlice() { v1.clear(); - Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false); + split("name1=value1; name2=value2; name3=value3", std::back_inserter(v1), + ";", false); CPPUNIT_ASSERT_EQUAL(3, (int)v1.size()); itr = v1.begin(); CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);