mirror of https://github.com/aria2/aria2
Avoid strlen in util::streq, util::strieq, util::startsWith and util::istartsWith
parent
b340c25164
commit
3b1834e633
|
@ -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)
|
||||
|
|
30
src/util.h
30
src/util.h
|
@ -559,7 +559,12 @@ bool streq
|
|||
template<typename InputIterator>
|
||||
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<typename InputIterator>
|
||||
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<typename InputIterator1, typename InputIterator2>
|
||||
|
@ -620,7 +631,12 @@ bool startsWith
|
|||
template<typename InputIterator>
|
||||
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<typename InputIterator>
|
||||
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);
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue