mirror of https://github.com/aria2/aria2
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added util::strip() * src/util.cc * src/util.h * test/UtilTest.ccpull/1/head
parent
f816434d06
commit
dbea00fa9d
|
@ -1,3 +1,10 @@
|
|||
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Added util::strip()
|
||||
* src/util.cc
|
||||
* src/util.h
|
||||
* test/UtilTest.cc
|
||||
|
||||
2010-10-09 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Added tests to HttpRequestTest about trailing slash of cookie
|
||||
|
|
|
@ -113,6 +113,11 @@ void trimSelf(std::string& str, const std::string& trimCharset)
|
|||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
hp.first = A2STR::NIL;
|
||||
|
|
22
src/util.h
22
src/util.h
|
@ -134,6 +134,28 @@ std::string trim(const std::string& src,
|
|||
void trimSelf(std::string& str,
|
||||
const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
|
||||
|
||||
template<typename InputIterator>
|
||||
std::string stripIter
|
||||
(InputIterator first, InputIterator last,
|
||||
const std::string& chars = DEFAULT_TRIM_CHARSET)
|
||||
{
|
||||
if(std::distance(first, last) == 0) {
|
||||
return A2STR::NIL;
|
||||
}
|
||||
for(; first != last &&
|
||||
std::find(chars.begin(), chars.end(), *first) != chars.end(); ++first);
|
||||
if(first == last) {
|
||||
return A2STR::NIL;
|
||||
}
|
||||
InputIterator left = last-1;
|
||||
for(; left != first &&
|
||||
std::find(chars.begin(), chars.end(), *left) != chars.end(); --left);
|
||||
return std::string(first, left+1);
|
||||
}
|
||||
|
||||
std::string strip
|
||||
(const std::string& str, const std::string& chars = DEFAULT_TRIM_CHARSET);
|
||||
|
||||
bool startsWith(const std::string& target, const std::string& part);
|
||||
|
||||
bool endsWith(const std::string& target, const std::string& part);
|
||||
|
|
|
@ -20,6 +20,7 @@ class UtilTest:public CppUnit::TestFixture {
|
|||
|
||||
CPPUNIT_TEST_SUITE(UtilTest);
|
||||
CPPUNIT_TEST(testTrim);
|
||||
CPPUNIT_TEST(testStrip);
|
||||
CPPUNIT_TEST(testSplit);
|
||||
CPPUNIT_TEST(testSplit_many);
|
||||
CPPUNIT_TEST(testEndsWith);
|
||||
|
@ -74,6 +75,7 @@ public:
|
|||
}
|
||||
|
||||
void testTrim();
|
||||
void testStrip();
|
||||
void testSplit();
|
||||
void testSplit_many();
|
||||
void testEndsWith();
|
||||
|
@ -144,6 +146,27 @@ void UtilTest::testTrim() {
|
|||
CPPUNIT_ASSERT_EQUAL(str4, util::trim(" A "));
|
||||
}
|
||||
|
||||
void UtilTest::testStrip()
|
||||
{
|
||||
std::string str1 = "aria2";
|
||||
CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2"));
|
||||
CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2"));
|
||||
CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2 "));
|
||||
CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
|
||||
CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
|
||||
std::string str2 = "aria2 debut";
|
||||
CPPUNIT_ASSERT_EQUAL(str2, util::strip("aria2 debut"));
|
||||
CPPUNIT_ASSERT_EQUAL(str2, util::strip(" aria2 debut "));
|
||||
std::string str3 = "";
|
||||
CPPUNIT_ASSERT_EQUAL(str3, util::strip(""));
|
||||
CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
|
||||
CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
|
||||
std::string str4 = "A";
|
||||
CPPUNIT_ASSERT_EQUAL(str4, util::strip("A"));
|
||||
CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
|
||||
CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
|
||||
}
|
||||
|
||||
void UtilTest::testSplit() {
|
||||
std::pair<std::string, std::string> p1;
|
||||
util::split(p1, "name=value", '=');
|
||||
|
|
Loading…
Reference in New Issue