diff --git a/ChangeLog b/ChangeLog index 52f5df0b..abc9b8ad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2010-10-10 Tatsuhiro Tsujikawa + + 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 Append 'u' to hex mask. diff --git a/src/ExpatMetalinkProcessor.cc b/src/ExpatMetalinkProcessor.cc index 474144ed..2f5bfbee 100644 --- a/src/ExpatMetalinkProcessor.cc +++ b/src/ExpatMetalinkProcessor.cc @@ -65,7 +65,7 @@ static void splitNsName const std::string& nsName) { std::pair 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 nsNamePair; - util::split(nsNamePair, attrNsName, '\t'); + util::divide(nsNamePair, attrNsName, '\t'); XmlAttr xa; if(nsNamePair.second.empty()) { xa.localname = nsNamePair.first; diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index 6dbdcb5e..74f2f654 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -451,7 +451,8 @@ unsigned int FtpConnection::receiveSizeResponse(uint64_t& size) std::pair response; if(bulkReceiveResponse(response)) { if(response.first == 213) { - std::pair rp = util::split(response.second," "); + std::pair rp; + util::divide(rp, response.second, ' '); size = util::parseULLInt(rp.second); } return response.first; diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index 2a5e8e47..cbd0739b 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -169,7 +169,7 @@ RangeHandle HttpHeader::getRange() const // but some server returns '100-199/100', omitting bytes-unit sepcifier // 'bytes'. std::pair 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 byteRangeSpecPair; - util::split(byteRangeSpecPair, byteRangeSpec, '/'); + util::divide(byteRangeSpecPair, byteRangeSpec, '/'); std::pair 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 hp; - util::split(hp, line, ':'); + util::divide(hp, line, ':'); put(hp.first, hp.second); } } diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index 1f5ea7a9..b2961491 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -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 p; + util::divide(p, httpHeader_->getFirst(HttpHeader::CONTENT_TYPE), ';'); + return p.first; } } diff --git a/src/HttpServer.cc b/src/HttpServer.cc index fb577cf6..416329fd 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -192,12 +192,14 @@ bool HttpServer::authenticate() if(authHeader.empty()) { return false; } - std::pair p = util::split(authHeader, " "); + std::pair p; + util::divide(p, authHeader, ' '); if(p.first != "Basic") { return false; } std::string userpass = Base64::decode(p.second); - std::pair userpassPair = util::split(userpass, ":"); + std::pair userpassPair; + util::divide(userpassPair, userpass, ':'); return username_ == userpassPair.first && password_ == userpassPair.second; } diff --git a/src/OptionParser.cc b/src/OptionParser.cc index f454f448..ec75605f 100644 --- a/src/OptionParser.cc +++ b/src/OptionParser.cc @@ -169,7 +169,8 @@ void OptionParser::parse(Option& option, std::istream& is) if(util::startsWith(line, A2STR::SHARP_C)) { continue; } - std::pair nv = util::split(line, A2STR::EQUAL_C); + std::pair nv; + util::divide(nv, line, '='); if(nv.first.empty()) { continue; } diff --git a/src/ParameterizedStringParser.cc b/src/ParameterizedStringParser.cc index d342838e..97594d9d 100644 --- a/src/ParameterizedStringParser.cc +++ b/src/ParameterizedStringParser.cc @@ -124,7 +124,8 @@ ParameterizedStringParser::createLoop(const std::string& src, int& offset) } loopStr.erase(colonIndex); } - std::pair range = util::split(loopStr, "-"); + std::pair range; + util::divide(range, loopStr, '-'); if(range.first.empty() || range.second.empty()) { throw DL_ABORT_EX("Loop range missing."); } diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc index 7fe11708..d6663cb8 100644 --- a/src/ServerStatMan.cc +++ b/src/ServerStatMan.cc @@ -107,7 +107,8 @@ bool ServerStatMan::load(std::istream& in) std::map m; for(std::vector::const_iterator i = items.begin(), eoi = items.end(); i != eoi; ++i) { - std::pair p = util::split(*i, "="); + std::pair p; + util::divide(p, *i, '='); p.first = util::strip(p.first); p.second = util::strip(p.second); m[p.first] = p.second; diff --git a/src/magnet.cc b/src/magnet.cc index 389a4c3f..086330c6 100644 --- a/src/magnet.cc +++ b/src/magnet.cc @@ -52,7 +52,7 @@ SharedHandle parse(const std::string& magnet) for(std::vector::const_iterator i = queries.begin(), eoi = queries.end(); i != eoi; ++i) { std::pair 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) { diff --git a/src/util.cc b/src/util.cc index 1ef72250..5f0eef94 100644 --- a/src/util.cc +++ b/src/util.cc @@ -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& hp, const std::string& src, char delim) +void divide +(std::pair& 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 split(const std::string& src, const std::string& delims) -{ - std::pair 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 p = split(temp, ","); + std::pair 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 vp = split(p.first.c_str(), "-"); + std::pair 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 paramPair; - split(paramPair, param, '='); + divide(paramPair, param, '='); std::string value = paramPair.second; std::vector extValues; split(value, std::back_inserter(extValues), "'", false, true); @@ -876,7 +863,7 @@ std::string getContentDispositionFilename(const std::string& header) continue; } std::pair 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::value_type parseIndexPath(const std::string& line) { - std::pair p = split(line, "="); + std::pair 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.", diff --git a/src/util.h b/src/util.h index 0e344bfc..5efde239 100644 --- a/src/util.h +++ b/src/util.h @@ -88,11 +88,8 @@ inline uint64_t hton64(uint64_t x) { return byteswap64(x); } namespace util { -void split(std::pair& hp, - const std::string& src, char delim); - -std::pair -split(const std::string& src, const std::string& delims); +void divide +(std::pair& hp, const std::string& src, char delim); template std::string uitos(T value, bool comma = false) diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 4c673d60..599d422e 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -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 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 v1; util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1), ";", true);