mirror of https://github.com/aria2/aria2
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Rewritten util::split() * src/util.h * test/UtilTest.ccpull/1/head
parent
8f173868dc
commit
0abd4a2f7b
|
@ -1,3 +1,9 @@
|
||||||
|
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Rewritten util::split()
|
||||||
|
* src/util.h
|
||||||
|
* test/UtilTest.cc
|
||||||
|
|
||||||
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Code cleanup
|
Code cleanup
|
||||||
|
|
39
src/util.h
39
src/util.h
|
@ -338,30 +338,27 @@ OutputIterator split(const std::string& src, OutputIterator out,
|
||||||
const std::string& delims, bool doStrip = false,
|
const std::string& delims, bool doStrip = false,
|
||||||
bool allowEmpty = false)
|
bool allowEmpty = false)
|
||||||
{
|
{
|
||||||
std::string::size_type p = 0;
|
std::string::const_iterator first = src.begin();
|
||||||
while(1) {
|
std::string::const_iterator last = src.end();
|
||||||
std::string::size_type np = src.find_first_of(delims, p);
|
for(std::string::const_iterator i = first; i != last;) {
|
||||||
if(np == std::string::npos) {
|
std::string::const_iterator j = i;
|
||||||
std::string term = src.substr(p);
|
for(; j != last &&
|
||||||
if(doStrip) {
|
std::find(delims.begin(), delims.end(), *j) == delims.end(); ++j);
|
||||||
term = util::strip(term);
|
std::string t = doStrip?util::stripIter(i, j):std::string(i, j);
|
||||||
}
|
if(allowEmpty || !t.empty()) {
|
||||||
if(allowEmpty || !term.empty()) {
|
*out++ = t;
|
||||||
*out = term;
|
|
||||||
++out;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
std::string term = src.substr(p, np-p);
|
i = j;
|
||||||
if(doStrip) {
|
if(j != last) {
|
||||||
term = util::strip(term);
|
++i;
|
||||||
}
|
|
||||||
p = np+1;
|
|
||||||
if(allowEmpty || !term.empty()) {
|
|
||||||
*out = term;
|
|
||||||
++out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(allowEmpty &&
|
||||||
|
(src.empty() ||
|
||||||
|
std::find(delims.begin(), delims.end(),
|
||||||
|
src[src.size()-1]) != delims.end())) {
|
||||||
|
*out++ = A2STR::NIL;
|
||||||
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
107
test/UtilTest.cc
107
test/UtilTest.cc
|
@ -174,41 +174,94 @@ void UtilTest::testDivide() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtilTest::testSplit() {
|
void UtilTest::testSplit() {
|
||||||
std::vector<std::string> v1;
|
std::vector<std::string> v;
|
||||||
util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
|
util::split("k1; k2;; k3", std::back_inserter(v), ";", true);
|
||||||
";", true);
|
CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)3, v1.size());
|
std::vector<std::string>::iterator itr = v.begin();
|
||||||
std::vector<std::string>::iterator itr = v1.begin();
|
CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
|
CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name2=value2"), *itr++);
|
CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name3=value3"), *itr++);
|
|
||||||
|
|
||||||
v1.clear();
|
v.clear();
|
||||||
|
|
||||||
util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
|
util::split("k1; k2; k3",
|
||||||
";", false);
|
std::back_inserter(v), ";");
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)3, v1.size());
|
CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
|
||||||
itr = v1.begin();
|
itr = v.begin();
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
|
CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(" name2=value2"), *itr++);
|
CPPUNIT_ASSERT_EQUAL(std::string(" k2"), *itr++);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(" name3=value3"), *itr++);
|
CPPUNIT_ASSERT_EQUAL(std::string(" k3"), *itr++);
|
||||||
|
|
||||||
v1.clear();
|
v.clear();
|
||||||
|
|
||||||
util::split("k=v", std::back_inserter(v1), ";", false, true);
|
util::split("k=v", std::back_inserter(v), ";", false, true);
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)1, v1.size());
|
CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("k=v"), v1[0]);
|
itr = v.begin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
|
||||||
|
|
||||||
v1.clear();
|
v.clear();
|
||||||
|
|
||||||
util::split(" ", std::back_inserter(v1), ";", true, true);
|
util::split(";;k1;;k2;", std::back_inserter(v), ";", false, true);
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)1, v1.size());
|
CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(""), v1[0]);
|
itr = v.begin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
|
||||||
|
|
||||||
v1.clear();
|
v.clear();
|
||||||
|
|
||||||
util::split(" ", std::back_inserter(v1), ";", true);
|
util::split(";;k1;;k2;", std::back_inserter(v), ";");
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)0, v1.size());
|
CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
|
||||||
|
itr = v.begin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
|
||||||
|
|
||||||
|
v.clear();
|
||||||
|
|
||||||
|
util::split("k; ", std::back_inserter(v), ";");
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
|
||||||
|
itr = v.begin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
|
||||||
|
|
||||||
|
v.clear();
|
||||||
|
|
||||||
|
util::split(" ", std::back_inserter(v), ";", true, true);
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
|
||||||
|
|
||||||
|
v.clear();
|
||||||
|
|
||||||
|
util::split(" ", std::back_inserter(v), ";", true);
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
|
||||||
|
|
||||||
|
v.clear();
|
||||||
|
|
||||||
|
util::split(" ", std::back_inserter(v), ";");
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
|
||||||
|
|
||||||
|
v.clear();
|
||||||
|
|
||||||
|
util::split(";", std::back_inserter(v), ";");
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
|
||||||
|
|
||||||
|
v.clear();
|
||||||
|
|
||||||
|
util::split(";", std::back_inserter(v), ";", false, true);
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
|
||||||
|
itr = v.begin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
|
||||||
|
|
||||||
|
v.clear();
|
||||||
|
|
||||||
|
util::split("", std::back_inserter(v), ";", false, true);
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtilTest::testEndsWith() {
|
void UtilTest::testEndsWith() {
|
||||||
|
|
Loading…
Reference in New Issue