From dbea00fa9d8d4b9ca32b7bbea6ecc4e6348a5c12 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 9 Oct 2010 16:22:56 +0000 Subject: [PATCH] 2010-10-10 Tatsuhiro Tsujikawa Added util::strip() * src/util.cc * src/util.h * test/UtilTest.cc --- ChangeLog | 7 +++++++ src/util.cc | 5 +++++ src/util.h | 22 ++++++++++++++++++++++ test/UtilTest.cc | 23 +++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/ChangeLog b/ChangeLog index a94a7284..a10b7a42 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-10-10 Tatsuhiro Tsujikawa + + Added util::strip() + * src/util.cc + * src/util.h + * test/UtilTest.cc + 2010-10-09 Tatsuhiro Tsujikawa Added tests to HttpRequestTest about trailing slash of cookie diff --git a/src/util.cc b/src/util.cc index d0e3d9a8..3846e4f9 100644 --- a/src/util.cc +++ b/src/util.cc @@ -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& hp, const std::string& src, char delim) { hp.first = A2STR::NIL; diff --git a/src/util.h b/src/util.h index 49c7c578..12af8ed7 100644 --- a/src/util.h +++ b/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 +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); diff --git a/test/UtilTest.cc b/test/UtilTest.cc index dabc5332..a9cf78dc 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -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 p1; util::split(p1, "name=value", '=');