Selaa lähdekoodia

Added util::istartsWith()

Tatsuhiro Tsujikawa 14 vuotta sitten
vanhempi
commit
5c88f61269
3 muutettua tiedostoa jossa 32 lisäystä ja 4 poistoa
  1. 1 2
      src/util.cc
  2. 14 2
      src/util.h
  3. 17 0
      test/UtilTest.cc

+ 1 - 2
src/util.cc

@@ -765,8 +765,7 @@ std::string getContentDispositionFilename(const std::string& header)
   for(std::vector<std::string>::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;

+ 14 - 2
src/util.h

@@ -697,6 +697,19 @@ bool startsWith
   return std::equal(first2, last2, first1);
 }
 
+template<typename InputIterator1, typename InputIterator2>
+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<typename InputIterator1, typename InputIterator2>
 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);

+ 17 - 0
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));