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

Replaced util::split(a,b,c) and util::split(a,b) with
	util::divide()
	* src/ExpatMetalinkProcessor.cc
	* src/FtpConnection.cc
	* src/HttpHeader.cc
	* src/HttpResponse.cc
	* src/HttpServer.cc
	* src/OptionParser.cc
	* src/ParameterizedStringParser.cc
	* src/ServerStatMan.cc
	* src/magnet.cc
	* src/util.cc
	* src/util.h
	* test/UtilTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2010-10-10 03:39:00 +00:00
parent 983b6006fd
commit 1875d7382f
13 changed files with 66 additions and 57 deletions

View File

@ -1,3 +1,20 @@
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Replaced util::split(a,b,c) and util::split(a,b) with
util::divide()
* src/ExpatMetalinkProcessor.cc
* src/FtpConnection.cc
* src/HttpHeader.cc
* src/HttpResponse.cc
* src/HttpServer.cc
* src/OptionParser.cc
* src/ParameterizedStringParser.cc
* src/ServerStatMan.cc
* src/magnet.cc
* src/util.cc
* src/util.h
* test/UtilTest.cc
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Append 'u' to hex mask.

View File

@ -65,7 +65,7 @@ static void splitNsName
const std::string& nsName)
{
std::pair<std::string, std::string> nsNamePair;
util::split(nsNamePair, nsName, '\t');
util::divide(nsNamePair, nsName, '\t');
if(nsNamePair.second.empty()) {
localname = nsNamePair.first;
} else {
@ -88,7 +88,7 @@ static void mlStartElement(void* userData, const char* nsName, const char** attr
}
std::string value = *p++;
std::pair<std::string, std::string> nsNamePair;
util::split(nsNamePair, attrNsName, '\t');
util::divide(nsNamePair, attrNsName, '\t');
XmlAttr xa;
if(nsNamePair.second.empty()) {
xa.localname = nsNamePair.first;

View File

@ -451,7 +451,8 @@ unsigned int FtpConnection::receiveSizeResponse(uint64_t& size)
std::pair<unsigned int, std::string> response;
if(bulkReceiveResponse(response)) {
if(response.first == 213) {
std::pair<std::string, std::string> rp = util::split(response.second," ");
std::pair<std::string, std::string> rp;
util::divide(rp, response.second, ' ');
size = util::parseULLInt(rp.second);
}
return response.first;

View File

@ -169,7 +169,7 @@ RangeHandle HttpHeader::getRange() const
// but some server returns '100-199/100', omitting bytes-unit sepcifier
// 'bytes'.
std::pair<std::string, std::string> splist;
util::split(splist, rangeStr, ' ');
util::divide(splist, rangeStr, ' ');
if(splist.second.empty()) {
// we assume bytes-unit specifier omitted.
byteRangeSpec = splist.first;
@ -178,10 +178,10 @@ RangeHandle HttpHeader::getRange() const
}
}
std::pair<std::string, std::string> byteRangeSpecPair;
util::split(byteRangeSpecPair, byteRangeSpec, '/');
util::divide(byteRangeSpecPair, byteRangeSpec, '/');
std::pair<std::string, std::string> byteRangeRespSpecPair;
util::split(byteRangeRespSpecPair, byteRangeSpecPair.first, '-');
util::divide(byteRangeRespSpecPair, byteRangeSpecPair.first, '-');
off_t startByte = util::parseLLInt(byteRangeRespSpecPair.first);
off_t endByte = util::parseLLInt(byteRangeRespSpecPair.second);
@ -219,7 +219,7 @@ void HttpHeader::fill(std::istream& in)
continue;
}
std::pair<std::string, std::string> hp;
util::split(hp, line, ':');
util::divide(hp, line, ':');
put(hp.first, hp.second);
}
}

View File

@ -238,8 +238,9 @@ std::string HttpResponse::getContentType() const
if(httpHeader_.isNull()) {
return A2STR::NIL;
} else {
return
util::split(httpHeader_->getFirst(HttpHeader::CONTENT_TYPE), ";").first;
std::pair<std::string, std::string> p;
util::divide(p, httpHeader_->getFirst(HttpHeader::CONTENT_TYPE), ';');
return p.first;
}
}

View File

@ -192,12 +192,14 @@ bool HttpServer::authenticate()
if(authHeader.empty()) {
return false;
}
std::pair<std::string, std::string> p = util::split(authHeader, " ");
std::pair<std::string, std::string> p;
util::divide(p, authHeader, ' ');
if(p.first != "Basic") {
return false;
}
std::string userpass = Base64::decode(p.second);
std::pair<std::string, std::string> userpassPair = util::split(userpass, ":");
std::pair<std::string, std::string> userpassPair;
util::divide(userpassPair, userpass, ':');
return username_ == userpassPair.first && password_ == userpassPair.second;
}

View File

@ -169,7 +169,8 @@ void OptionParser::parse(Option& option, std::istream& is)
if(util::startsWith(line, A2STR::SHARP_C)) {
continue;
}
std::pair<std::string, std::string> nv = util::split(line, A2STR::EQUAL_C);
std::pair<std::string, std::string> nv;
util::divide(nv, line, '=');
if(nv.first.empty()) {
continue;
}

View File

@ -124,7 +124,8 @@ ParameterizedStringParser::createLoop(const std::string& src, int& offset)
}
loopStr.erase(colonIndex);
}
std::pair<std::string, std::string> range = util::split(loopStr, "-");
std::pair<std::string, std::string> range;
util::divide(range, loopStr, '-');
if(range.first.empty() || range.second.empty()) {
throw DL_ABORT_EX("Loop range missing.");
}

View File

@ -107,7 +107,8 @@ bool ServerStatMan::load(std::istream& in)
std::map<std::string, std::string> m;
for(std::vector<std::string>::const_iterator i = items.begin(),
eoi = items.end(); i != eoi; ++i) {
std::pair<std::string, std::string> p = util::split(*i, "=");
std::pair<std::string, std::string> p;
util::divide(p, *i, '=');
p.first = util::strip(p.first);
p.second = util::strip(p.second);
m[p.first] = p.second;

View File

@ -52,7 +52,7 @@ SharedHandle<Dict> parse(const std::string& magnet)
for(std::vector<std::string>::const_iterator i = queries.begin(),
eoi = queries.end(); i != eoi; ++i) {
std::pair<std::string, std::string> kv;
util::split(kv, *i, '=');
util::divide(kv, *i, '=');
std::string value = util::percentDecode(kv.second);
List* l = asList(dict->get(kv.first));
if(l) {

View File

@ -99,36 +99,21 @@ std::string strip(const std::string& str, const std::string& chars)
return stripIter(str.begin(), str.end(), chars);
}
void split(std::pair<std::string, std::string>& hp, const std::string& src, char delim)
void divide
(std::pair<std::string, std::string>& hp, const std::string& src, char delim)
{
hp.first = A2STR::NIL;
hp.second = A2STR::NIL;
std::string::size_type p = src.find(delim);
if(p == std::string::npos) {
std::string::const_iterator first = src.begin();
std::string::const_iterator last = src.end();
std::string::const_iterator dpos = std::find(first, last, delim);
if(dpos == last) {
hp.first = strip(src);
hp.second = A2STR::NIL;
} else {
hp.first = strip(src.substr(0, p));
hp.second = strip(src.substr(p+1));
hp.first = stripIter(first, dpos);
hp.second = stripIter(dpos+1, last);
}
}
std::pair<std::string, std::string> split(const std::string& src, const std::string& delims)
{
std::pair<std::string, std::string> hp;
hp.first = A2STR::NIL;
hp.second = A2STR::NIL;
std::string::size_type p = src.find_first_of(delims);
if(p == std::string::npos) {
hp.first = strip(src);
hp.second = A2STR::NIL;
} else {
hp.first = strip(src.substr(0, p));
hp.second = strip(src.substr(p+1));
}
return hp;
}
std::string itos(int64_t value, bool comma)
{
bool flag = false;
@ -625,7 +610,8 @@ IntSequence parseIntRange(const std::string& src)
IntSequence::Values values;
std::string temp = src;
while(temp.size()) {
std::pair<std::string, std::string> p = split(temp, ",");
std::pair<std::string, std::string> p;
divide(p, temp, ',');
temp = p.second;
if(p.first.empty()) {
continue;
@ -634,7 +620,8 @@ IntSequence parseIntRange(const std::string& src)
int32_t v = parseInt(p.first.c_str());
values.push_back(IntSequence::Value(v, v+1));
} else {
std::pair<std::string, std::string> vp = split(p.first.c_str(), "-");
std::pair<std::string, std::string> vp;
divide(vp, p.first.c_str(), '-');
if(vp.first.empty() || vp.second.empty()) {
throw DL_ABORT_EX
(StringFormat(MSG_INCOMPLETE_RANGE, p.first.c_str()).str());
@ -816,7 +803,7 @@ std::string getContentDispositionFilename(const std::string& header)
continue;
}
std::pair<std::string, std::string> paramPair;
split(paramPair, param, '=');
divide(paramPair, param, '=');
std::string value = paramPair.second;
std::vector<std::string> extValues;
split(value, std::back_inserter(extValues), "'", false, true);
@ -876,7 +863,7 @@ std::string getContentDispositionFilename(const std::string& header)
continue;
}
std::pair<std::string, std::string> paramPair;
split(paramPair, param, '=');
divide(paramPair, param, '=');
std::string value = paramPair.second;
if(value.empty()) {
continue;
@ -1228,7 +1215,8 @@ std::string htmlEscape(const std::string& src)
std::map<size_t, std::string>::value_type
parseIndexPath(const std::string& line)
{
std::pair<std::string, std::string> p = split(line, "=");
std::pair<std::string, std::string> p;
divide(p, line, '=');
size_t index = parseUInt(p.first);
if(p.second.empty()) {
throw DL_ABORT_EX(StringFormat("Path with index=%u is empty.",

View File

@ -88,11 +88,8 @@ inline uint64_t hton64(uint64_t x) { return byteswap64(x); }
namespace util {
void split(std::pair<std::string, std::string>& hp,
const std::string& src, char delim);
std::pair<std::string, std::string>
split(const std::string& src, const std::string& delims);
void divide
(std::pair<std::string, std::string>& hp, const std::string& src, char delim);
template<typename T>
std::string uitos(T value, bool comma = false)

View File

@ -20,8 +20,8 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(UtilTest);
CPPUNIT_TEST(testStrip);
CPPUNIT_TEST(testDivide);
CPPUNIT_TEST(testSplit);
CPPUNIT_TEST(testSplit_many);
CPPUNIT_TEST(testEndsWith);
CPPUNIT_TEST(testReplace);
CPPUNIT_TEST(testStartsWith);
@ -78,8 +78,8 @@ public:
}
void testStrip();
void testDivide();
void testSplit();
void testSplit_many();
void testEndsWith();
void testReplace();
void testStartsWith();
@ -154,26 +154,26 @@ void UtilTest::testStrip()
CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
}
void UtilTest::testSplit() {
void UtilTest::testDivide() {
std::pair<std::string, std::string> p1;
util::split(p1, "name=value", '=');
util::divide(p1, "name=value", '=');
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
util::split(p1, " name = value ", '=');
util::divide(p1, " name = value ", '=');
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
util::split(p1, "=value", '=');
util::divide(p1, "=value", '=');
CPPUNIT_ASSERT_EQUAL(std::string(""), p1.first);
CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
util::split(p1, "name=", '=');
util::divide(p1, "name=", '=');
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
util::split(p1, "name", '=');
util::divide(p1, "name", '=');
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
}
void UtilTest::testSplit_many() {
void UtilTest::testSplit() {
std::vector<std::string> v1;
util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
";", true);