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>
|
||||
|
||||
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,
|
||||
bool allowEmpty = 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(doStrip) {
|
||||
term = util::strip(term);
|
||||
}
|
||||
if(allowEmpty || !term.empty()) {
|
||||
*out = term;
|
||||
++out;
|
||||
}
|
||||
break;
|
||||
std::string::const_iterator first = src.begin();
|
||||
std::string::const_iterator last = src.end();
|
||||
for(std::string::const_iterator i = first; i != last;) {
|
||||
std::string::const_iterator j = i;
|
||||
for(; j != last &&
|
||||
std::find(delims.begin(), delims.end(), *j) == delims.end(); ++j);
|
||||
std::string t = doStrip?util::stripIter(i, j):std::string(i, j);
|
||||
if(allowEmpty || !t.empty()) {
|
||||
*out++ = t;
|
||||
}
|
||||
std::string term = src.substr(p, np-p);
|
||||
if(doStrip) {
|
||||
term = util::strip(term);
|
||||
}
|
||||
p = np+1;
|
||||
if(allowEmpty || !term.empty()) {
|
||||
*out = term;
|
||||
++out;
|
||||
i = j;
|
||||
if(j != last) {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
if(allowEmpty &&
|
||||
(src.empty() ||
|
||||
std::find(delims.begin(), delims.end(),
|
||||
src[src.size()-1]) != delims.end())) {
|
||||
*out++ = A2STR::NIL;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
107
test/UtilTest.cc
107
test/UtilTest.cc
|
@ -174,41 +174,94 @@ void UtilTest::testDivide() {
|
|||
}
|
||||
|
||||
void UtilTest::testSplit() {
|
||||
std::vector<std::string> v1;
|
||||
util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
|
||||
";", true);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)3, v1.size());
|
||||
std::vector<std::string>::iterator itr = v1.begin();
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("name2=value2"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("name3=value3"), *itr++);
|
||||
std::vector<std::string> v;
|
||||
util::split("k1; k2;; k3", std::back_inserter(v), ";", true);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
|
||||
std::vector<std::string>::iterator itr = v.begin();
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
|
||||
|
||||
v1.clear();
|
||||
v.clear();
|
||||
|
||||
util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
|
||||
";", false);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)3, v1.size());
|
||||
itr = v1.begin();
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string(" name2=value2"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string(" name3=value3"), *itr++);
|
||||
util::split("k1; k2; k3",
|
||||
std::back_inserter(v), ";");
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
|
||||
itr = v.begin();
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string(" k2"), *itr++);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string(" k3"), *itr++);
|
||||
|
||||
v1.clear();
|
||||
v.clear();
|
||||
|
||||
util::split("k=v", std::back_inserter(v1), ";", false, true);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)1, v1.size());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("k=v"), v1[0]);
|
||||
util::split("k=v", std::back_inserter(v), ";", false, true);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
|
||||
itr = v.begin();
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
|
||||
|
||||
v1.clear();
|
||||
v.clear();
|
||||
|
||||
util::split(" ", std::back_inserter(v1), ";", true, true);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)1, v1.size());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string(""), v1[0]);
|
||||
util::split(";;k1;;k2;", std::back_inserter(v), ";", false, true);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
|
||||
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);
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)0, v1.size());
|
||||
util::split(";;k1;;k2;", std::back_inserter(v), ";");
|
||||
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() {
|
||||
|
|
Loading…
Reference in New Issue