From 5c88f612694d77a5cf09f26c3922ad8c9e9f4d68 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 5 Nov 2011 00:33:35 +0900 Subject: [PATCH] Added util::istartsWith() --- src/util.cc | 3 +-- src/util.h | 16 ++++++++++++++-- test/UtilTest.cc | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/util.cc b/src/util.cc index 9a229f24..131d2c48 100644 --- a/src/util.cc +++ b/src/util.cc @@ -765,8 +765,7 @@ std::string getContentDispositionFilename(const std::string& header) for(std::vector::const_iterator i = params.begin(), eoi = params.end(); i != eoi; ++i) { const std::string& param = *i; - std::string lowparam = toLower(param); - if(!startsWith(lowparam.begin(), lowparam.end(), + if(!istartsWith(param.begin(), param.end(), A2_KEYNAME, vend(A2_KEYNAME)-1) || param.size() == sizeof(A2_KEYNAME)-1) { continue; diff --git a/src/util.h b/src/util.h index 832d6ef0..36cc0504 100644 --- a/src/util.h +++ b/src/util.h @@ -697,6 +697,19 @@ bool startsWith return std::equal(first2, last2, first1); } +template +bool istartsWith +(InputIterator1 first1, + InputIterator1 last1, + InputIterator2 first2, + InputIterator2 last2) +{ + if(last1-first1 < last2-first2) { + return false; + } + return strieq(first1, first1+(last2-first2), first2, last2); +} + template bool endsWith (InputIterator1 first1, @@ -720,8 +733,7 @@ bool iendsWith if(last1-first1 < last2-first2) { return false; } - first1 = last1-(last2-first2); - return strieq(first1, last1, first2, last2); + return strieq(last1-(last2-first2), last1, first2, last2); } void generateRandomData(unsigned char* data, size_t length); diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 5a7a5a1b..f81182f2 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -36,6 +36,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testIendsWith); CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testStartsWith); + CPPUNIT_TEST(testIstartsWith); // may be moved to other helper class in the future. CPPUNIT_TEST(testGetContentDispositionFilename); CPPUNIT_TEST(testRandomAlpha); @@ -104,6 +105,7 @@ public: void testIendsWith(); void testReplace(); void testStartsWith(); + void testIstartsWith(); // may be moved to other helper class in the future. void testGetContentDispositionFilename(); void testRandomAlpha(); @@ -780,6 +782,21 @@ void UtilTest::testStartsWith() { part.begin(), part.end())); } +void UtilTest::testIstartsWith() { + std::string target; + std::string part; + + target = "abcdefg"; + part = "aBc"; + CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), + part.begin(), part.end())); + + target = "abcdefg"; + part = "abx"; + CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), + part.begin(), part.end())); +} + void UtilTest::testGetContentDispositionFilename() { std::string h1 = "attachment; filename=\"aria2.tar.bz2\""; CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));