2009-10-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

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
pull/1/head
Tatsuhiro Tsujikawa 2009-10-18 12:31:07 +00:00
parent 1a7064f6f6
commit c8d34a46be
17 changed files with 100 additions and 100 deletions

View File

@ -1,3 +1,23 @@
2009-10-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
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 <t-tujikawa@users.sourceforge.net> 2009-10-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Use request->getDir() instead of "/" for embedded user/pass in Use request->getDir() instead of "/" for embedded user/pass in

View File

@ -414,8 +414,8 @@ public:
static bool inNoProxy(const SharedHandle<Request>& req, static bool inNoProxy(const SharedHandle<Request>& req,
const std::string& noProxy) const std::string& noProxy)
{ {
std::deque<std::string> entries; std::vector<std::string> entries;
Util::slice(entries, noProxy, ',', true); split(noProxy, std::back_inserter(entries), ",", true);
if(entries.empty()) { if(entries.empty()) {
return false; return false;
} }

View File

@ -39,6 +39,7 @@
#include <utility> #include <utility>
#include <istream> #include <istream>
#include <map> #include <map>
#include <vector>
#include "Util.h" #include "Util.h"
#include "A2STR.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 Cookie CookieParser::parse(const std::string& cookieStr, const std::string& defaultDomain, const std::string& defaultPath) const
{ {
std::deque<std::string> terms; std::vector<std::string> terms;
Util::slice(terms, Util::trim(cookieStr), ';', true); split(Util::trim(cookieStr), std::back_inserter(terms), ";", true);
if(terms.empty()) { if(terms.empty()) {
return Cookie(); return Cookie();
} }
@ -73,7 +74,7 @@ Cookie CookieParser::parse(const std::string& cookieStr, const std::string& defa
values[C_DOMAIN] = defaultDomain; values[C_DOMAIN] = defaultDomain;
values[C_PATH] = defaultPath; values[C_PATH] = defaultPath;
for(std::deque<std::string>::iterator itr = terms.begin()+1; for(std::vector<std::string>::iterator itr = terms.begin()+1;
itr != terms.end(); ++itr) { itr != terms.end(); ++itr) {
std::pair<std::string, std::string> nv; std::pair<std::string, std::string> nv;
Util::split(nv, *itr, '='); Util::split(nv, *itr, '=');

View File

@ -39,7 +39,7 @@
#include <utime.h> #include <utime.h>
#include <unistd.h> #include <unistd.h>
#include <deque> #include <vector>
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
@ -105,8 +105,8 @@ bool File::mkdirs() {
if(isDir()) { if(isDir()) {
return false; return false;
} }
std::deque<std::string> dirs; std::vector<std::string> dirs;
Util::slice(dirs, name, '/'); split(name, std::back_inserter(dirs), "/");
if(!dirs.size()) { if(!dirs.size()) {
return true; return true;
} }
@ -115,8 +115,8 @@ bool File::mkdirs() {
if(Util::startsWith(name, A2STR::SLASH_C)) { if(Util::startsWith(name, A2STR::SLASH_C)) {
accDir = A2STR::SLASH_C; accDir = A2STR::SLASH_C;
} }
for(std::deque<std::string>::const_iterator itr = dirs.begin(); itr != dirs.end(); for(std::vector<std::string>::const_iterator itr = dirs.begin();
++itr, accDir += A2STR::SLASH_C) { itr != dirs.end(); ++itr, accDir += A2STR::SLASH_C) {
accDir += *itr; accDir += *itr;
if(File(accDir).isDir()) { if(File(accDir).isDir()) {
continue; continue;

View File

@ -35,6 +35,7 @@
#include "HttpHeaderProcessor.h" #include "HttpHeaderProcessor.h"
#include <sstream> #include <sstream>
#include <vector>
#include "HttpHeader.h" #include "HttpHeader.h"
#include "message.h" #include "message.h"
@ -124,8 +125,8 @@ SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpRequestHeader()
delimpos < 14) { delimpos < 14) {
throw DL_RETRY_EX(EX_NO_STATUS_HEADER); throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
} }
std::deque<std::string> firstLine; std::vector<std::string> firstLine;
Util::slice(firstLine, _buf.substr(0, delimpos), ' ', true); split(_buf.substr(0, delimpos), std::back_inserter(firstLine), " ", true);
if(firstLine.size() != 3) { if(firstLine.size() != 3) {
throw DL_ABORT_EX("Malformed HTTP request header."); throw DL_ABORT_EX("Malformed HTTP request header.");
} }

View File

@ -36,6 +36,7 @@
#include <cassert> #include <cassert>
#include <numeric> #include <numeric>
#include <vector>
#include "Segment.h" #include "Segment.h"
#include "Range.h" #include "Range.h"
@ -276,8 +277,8 @@ void HttpRequest::disableContentEncoding()
void HttpRequest::addHeader(const std::string& headersString) void HttpRequest::addHeader(const std::string& headersString)
{ {
std::deque<std::string> headers; std::vector<std::string> headers;
Util::slice(headers, headersString, '\n', true); split(headersString, std::back_inserter(headers), "\n", true);
_headers.insert(_headers.end(), headers.begin(), headers.end()); _headers.insert(_headers.end(), headers.begin(), headers.end());
} }

View File

@ -143,7 +143,8 @@ Metalink2RequestGroup::createRequestGroup
SharedHandle<MetalinkEntry>& entry = *itr; SharedHandle<MetalinkEntry>& entry = *itr;
if(option->defined(PREF_METALINK_LOCATION)) { if(option->defined(PREF_METALINK_LOCATION)) {
std::deque<std::string> locations; std::deque<std::string> 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); entry->setLocationPreference(locations, 100);
} }
if(option->get(PREF_METALINK_PREFERRED_PROTOCOL) != V_NONE) { if(option->get(PREF_METALINK_PREFERRED_PROTOCOL) != V_NONE) {

View File

@ -35,6 +35,7 @@
#include "MetalinkParserController.h" #include "MetalinkParserController.h"
#include <algorithm> #include <algorithm>
#include <vector>
#include "Metalinker.h" #include "Metalinker.h"
#include "MetalinkEntry.h" #include "MetalinkEntry.h"
@ -75,8 +76,8 @@ void MetalinkParserController::setFileNameOfEntry(const std::string& filename)
if(_tEntry.isNull()) { if(_tEntry.isNull()) {
return; return;
} }
std::deque<std::string> elements; std::vector<std::string> elements;
Util::slice(elements, filename, '/'); split(filename, std::back_inserter(elements), "/");
std::string path = Util::joinPath(elements.begin(), elements.end()); std::string path = Util::joinPath(elements.begin(), elements.end());
if(_tEntry->file.isNull()) { if(_tEntry->file.isNull()) {

View File

@ -93,7 +93,7 @@ void Netrc::parse(const std::string& path)
continue; continue;
} }
std::vector<std::string> tokens; std::vector<std::string> tokens;
Util::split(line, std::back_inserter(tokens), " \t", true); split(line, std::back_inserter(tokens), " \t", true);
for(std::vector<std::string>::const_iterator iter = tokens.begin(); for(std::vector<std::string>::const_iterator iter = tokens.begin();
iter != tokens.end(); ++iter) { iter != tokens.end(); ++iter) {
const std::string& token = *iter; const std::string& token = *iter;

View File

@ -35,6 +35,7 @@
#include "NsCookieParser.h" #include "NsCookieParser.h"
#include <fstream> #include <fstream>
#include <vector>
#include "Util.h" #include "Util.h"
#include "A2STR.h" #include "A2STR.h"
@ -51,8 +52,8 @@ static const std::string C_TRUE("TRUE");
static Cookie parseNsCookie(const std::string& nsCookieStr) static Cookie parseNsCookie(const std::string& nsCookieStr)
{ {
std::deque<std::string> vs; std::vector<std::string> vs;
Util::slice(vs, nsCookieStr, '\t', true); split(nsCookieStr, std::back_inserter(vs), "\t", true);
if(vs.size() < 6 ) { if(vs.size() < 6 ) {
return Cookie(); return Cookie();
} }

View File

@ -91,7 +91,8 @@ PStringDatumHandle ParameterizedStringParser::createSelect(const std::string& sr
throw DL_ABORT_EX("Missing '}' in the parameterized string."); throw DL_ABORT_EX("Missing '}' in the parameterized string.");
} }
std::deque<std::string> values; std::deque<std::string> values;
Util::slice(values, src.substr(offset, rightParenIndex-offset), ',', true); split(src.substr(offset, rightParenIndex-offset), std::back_inserter(values),
",", true);
if(values.empty()) { if(values.empty()) {
throw DL_ABORT_EX("Empty {} is not allowed."); throw DL_ABORT_EX("Empty {} is not allowed.");
} }

View File

@ -38,6 +38,7 @@
#include <ostream> #include <ostream>
#include <iterator> #include <iterator>
#include <map> #include <map>
#include <vector>
#include "ServerStat.h" #include "ServerStat.h"
#include "Util.h" #include "Util.h"
@ -101,10 +102,10 @@ bool ServerStatMan::load(std::istream& in)
if(line.empty()) { if(line.empty()) {
continue; continue;
} }
std::deque<std::string> items; std::vector<std::string> items;
Util::slice(items, line, ','); split(line, std::back_inserter(items), ",");
std::map<std::string, std::string> m; std::map<std::string, std::string> m;
for(std::deque<std::string>::const_iterator i = items.begin(); for(std::vector<std::string>::const_iterator i = items.begin();
i != items.end(); ++i) { i != items.end(); ++i) {
std::pair<std::string, std::string> p = Util::split(*i, "="); std::pair<std::string, std::string> p = Util::split(*i, "=");
Util::trimSelf(p.first); Util::trimSelf(p.first);

View File

@ -74,7 +74,7 @@ void UriListParser::parseNext(std::deque<std::string>& uris, Option& op)
} }
do { do {
if(!Util::trim(_line).empty()) { if(!Util::trim(_line).empty()) {
Util::slice(uris, _line, '\t', true); split(_line, std::back_inserter(uris), "\t", true);
getOptions(op); getOptions(op);
return; return;
} }

View File

@ -141,31 +141,6 @@ int32_t Util::difftvsec(struct timeval tv1, struct timeval tv2) {
return tv1.tv_sec-tv2.tv_sec; return tv1.tv_sec-tv2.tv_sec;
} }
void Util::slice(std::deque<std::string>& 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) { bool Util::startsWith(const std::string& target, const std::string& part) {
if(target.size() < part.size()) { if(target.size() < part.size()) {
return false; return false;

View File

@ -133,43 +133,6 @@ public:
*/ */
static int64_t difftv(struct timeval tv1, struct timeval tv2); static int64_t difftv(struct timeval tv1, struct timeval tv2);
static int32_t difftvsec(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<std::string>& result, const std::string& src,
char delim, bool trim = false);
template<typename OutputIterator>
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; static const std::string DEFAULT_TRIM_CHARSET;
@ -334,6 +297,41 @@ public:
static std::map<size_t, std::string> createIndexPathMap(std::istream& i); static std::map<size_t, std::string> 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<typename OutputIterator>
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 } // namespace aria2
#endif // _D_UTIL_H_ #endif // _D_UTIL_H_

View File

@ -195,9 +195,9 @@ static void extractFileEntries
} }
const BDE& nameData = infoDict[nameKey]; const BDE& nameData = infoDict[nameKey];
if(nameData.isString()) { if(nameData.isString()) {
// Slice path by '/' just in case nasty ".." is included in name // Split path by '/' just in case nasty ".." is included in name
std::deque<std::string> pathelems; std::vector<std::string> pathelems;
Util::slice(pathelems, nameData.s(), '/'); split(nameData.s(), std::back_inserter(pathelems), "/");
name = Util::joinPath(pathelems.begin(), pathelems.end()); name = Util::joinPath(pathelems.begin(), pathelems.end());
torrent[NAME] = nameData.s(); torrent[NAME] = nameData.s();
} else { } else {
@ -248,8 +248,8 @@ static void extractFileEntries
strappend(path, "/", Util::joinPath(pathelem.begin(), pathelem.end())); strappend(path, "/", Util::joinPath(pathelem.begin(), pathelem.end()));
// Split path with '/' again because each pathList element can // Split path with '/' again because each pathList element can
// contain "/" inside. // contain "/" inside.
std::deque<std::string> elements; std::vector<std::string> elements;
Util::slice(elements, path, '/'); split(path, std::back_inserter(elements), "/");
path = Util::joinPath(elements.begin(), elements.end()); path = Util::joinPath(elements.begin(), elements.end());
std::deque<std::string> uris; std::deque<std::string> uris;

View File

@ -20,7 +20,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(UtilTest); CPPUNIT_TEST_SUITE(UtilTest);
CPPUNIT_TEST(testTrim); CPPUNIT_TEST(testTrim);
CPPUNIT_TEST(testSplit); CPPUNIT_TEST(testSplit);
CPPUNIT_TEST(testSlice); CPPUNIT_TEST(testSplit_many);
CPPUNIT_TEST(testEndsWith); CPPUNIT_TEST(testEndsWith);
CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testReplace);
CPPUNIT_TEST(testStartsWith); CPPUNIT_TEST(testStartsWith);
@ -63,7 +63,7 @@ public:
void testTrim(); void testTrim();
void testSplit(); void testSplit();
void testSlice(); void testSplit_many();
void testEndsWith(); void testEndsWith();
void testReplace(); void testReplace();
void testStartsWith(); void testStartsWith();
@ -140,12 +140,10 @@ void UtilTest::testSplit() {
CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second); CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
} }
void UtilTest::testSlice() { void UtilTest::testSplit_many() {
std::deque<std::string> v1; std::deque<std::string> v1;
Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true); split("name1=value1; name2=value2; name3=value3", std::back_inserter(v1),
CPPUNIT_ASSERT_EQUAL(3, (int)v1.size()); ";", true);
v1.clear();
Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
CPPUNIT_ASSERT_EQUAL(3, (int)v1.size()); CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
std::deque<std::string>::iterator itr = v1.begin(); std::deque<std::string>::iterator itr = v1.begin();
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++); CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
@ -154,7 +152,8 @@ void UtilTest::testSlice() {
v1.clear(); 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()); CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
itr = v1.begin(); itr = v1.begin();
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++); CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);