From 3b1834e63347f320bfe9df6127f6f017978d122a Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 11 Jan 2012 01:50:35 +0900 Subject: [PATCH] Avoid strlen in util::streq, util::strieq, util::startsWith and util::istartsWith --- src/util.cc | 4 ++-- src/util.h | 30 ++++++++++++++++++++++++++---- test/UtilTest.cc | 20 ++++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/util.cc b/src/util.cc index db5a32a2..416adf8b 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1613,7 +1613,7 @@ bool noProxyDomainMatch bool startsWith(const std::string& a, const char* b) { - return startsWith(a.begin(), a.end(), b, b+strlen(b)); + return startsWith(a.begin(), a.end(), b); } bool startsWith(const std::string& a, const std::string& b) @@ -1623,7 +1623,7 @@ bool startsWith(const std::string& a, const std::string& b) bool istartsWith(const std::string& a, const char* b) { - return istartsWith(a.begin(), a.end(), b, b+strlen(b)); + return istartsWith(a.begin(), a.end(), b); } bool endsWith(const std::string& a, const char* b) diff --git a/src/util.h b/src/util.h index e655d4f9..4d2ceb43 100644 --- a/src/util.h +++ b/src/util.h @@ -559,7 +559,12 @@ bool streq template bool streq(InputIterator first, InputIterator last, const char* b) { - return streq(first, last, b, b+strlen(b)); + for(; first != last && *b != '\0'; ++first, ++b) { + if(*first != *b) { + return false; + } + } + return first == last && *b == '\0'; } struct CaseCmp { @@ -601,7 +606,13 @@ bool strieq template bool strieq(InputIterator first, InputIterator last, const char* b) { - return strieq(first, last, b, b+strlen(b)); + CaseCmp cmp; + for(; first != last && *b != '\0'; ++first, ++b) { + if(!cmp(*first, *b)) { + return false; + } + } + return first == last && *b == '\0'; } template @@ -620,7 +631,12 @@ bool startsWith template bool startsWith(InputIterator first, InputIterator last, const char* b) { - return startsWith(first, last, b, b+strlen(b)); + for(; first != last && *b != '\0'; ++first, ++b) { + if(*first != *b) { + return false; + } + } + return *b == '\0'; } bool startsWith(const std::string& a, const char* b); @@ -642,7 +658,13 @@ bool istartsWith template bool istartsWith(InputIterator first, InputIterator last, const char* b) { - return istartsWith(first, last, b, b+strlen(b)); + CaseCmp cmp; + for(; first != last && *b != '\0'; ++first, ++b) { + if(!cmp(*first, *b)) { + return false; + } + } + return *b == '\0'; } bool istartsWith(const std::string& a, const char* b); diff --git a/test/UtilTest.cc b/test/UtilTest.cc index b9a566d2..eb7297aa 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -686,18 +686,23 @@ void UtilTest::testStreq() s1 = "foo"; s2 = "foo"; CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str())); s2 = "fooo"; CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str())); s2 = "fo"; CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str())); s2 = ""; CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str())); s1 = ""; CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str())); } void UtilTest::testStrieq() @@ -706,22 +711,28 @@ void UtilTest::testStrieq() s1 = "foo"; s2 = "foo"; CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str())); s1 = "FoO"; s2 = "fOo"; CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str())); s2 = "fooo"; CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str())); s2 = "fo"; CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str())); s2 = ""; CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str())); s1 = ""; CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end())); + CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str())); } void UtilTest::testStrifind() @@ -756,36 +767,43 @@ void UtilTest::testStartsWith() { part = "abc"; CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str())); target = "abcdefg"; part = "abx"; CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str())); target = "abcdefg"; part = "bcd"; CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str())); target = ""; part = "a"; CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str())); target = ""; part = ""; CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str())); target = "a"; part = ""; CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str())); target = "a"; part = "a"; CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str())); } void UtilTest::testIstartsWith() { @@ -796,11 +814,13 @@ void UtilTest::testIstartsWith() { part = "aBc"; CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), part.c_str())); target = "abcdefg"; part = "abx"; CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.begin(), part.end())); + CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.c_str())); } void UtilTest::testGetContentDispositionFilename() {