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)
|
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)
|
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)
|
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)
|
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>
|
template<typename InputIterator>
|
||||||
bool streq(InputIterator first, InputIterator last, const char* b)
|
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 {
|
struct CaseCmp {
|
||||||
|
@ -601,7 +606,13 @@ bool strieq
|
||||||
template<typename InputIterator>
|
template<typename InputIterator>
|
||||||
bool strieq(InputIterator first, InputIterator last, const char* b)
|
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>
|
template<typename InputIterator1, typename InputIterator2>
|
||||||
|
@ -620,7 +631,12 @@ bool startsWith
|
||||||
template<typename InputIterator>
|
template<typename InputIterator>
|
||||||
bool startsWith(InputIterator first, InputIterator last, const char* b)
|
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);
|
bool startsWith(const std::string& a, const char* b);
|
||||||
|
@ -642,7 +658,13 @@ bool istartsWith
|
||||||
template<typename InputIterator>
|
template<typename InputIterator>
|
||||||
bool istartsWith(InputIterator first, InputIterator last, const char* b)
|
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);
|
bool istartsWith(const std::string& a, const char* b);
|
||||||
|
|
|
@ -686,18 +686,23 @@ void UtilTest::testStreq()
|
||||||
s1 = "foo";
|
s1 = "foo";
|
||||||
s2 = "foo";
|
s2 = "foo";
|
||||||
CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
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";
|
s2 = "fooo";
|
||||||
CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
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";
|
s2 = "fo";
|
||||||
CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
|
||||||
|
|
||||||
s2 = "";
|
s2 = "";
|
||||||
CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
|
||||||
|
|
||||||
s1 = "";
|
s1 = "";
|
||||||
CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
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()
|
void UtilTest::testStrieq()
|
||||||
|
@ -706,22 +711,28 @@ void UtilTest::testStrieq()
|
||||||
s1 = "foo";
|
s1 = "foo";
|
||||||
s2 = "foo";
|
s2 = "foo";
|
||||||
CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
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";
|
s1 = "FoO";
|
||||||
s2 = "fOo";
|
s2 = "fOo";
|
||||||
CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
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";
|
s2 = "fooo";
|
||||||
CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
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";
|
s2 = "fo";
|
||||||
CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
|
||||||
|
|
||||||
s2 = "";
|
s2 = "";
|
||||||
CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
|
||||||
|
|
||||||
s1 = "";
|
s1 = "";
|
||||||
CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
|
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()
|
void UtilTest::testStrifind()
|
||||||
|
@ -756,36 +767,43 @@ void UtilTest::testStartsWith() {
|
||||||
part = "abc";
|
part = "abc";
|
||||||
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
|
||||||
|
|
||||||
target = "abcdefg";
|
target = "abcdefg";
|
||||||
part = "abx";
|
part = "abx";
|
||||||
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
|
||||||
|
|
||||||
target = "abcdefg";
|
target = "abcdefg";
|
||||||
part = "bcd";
|
part = "bcd";
|
||||||
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
|
||||||
|
|
||||||
target = "";
|
target = "";
|
||||||
part = "a";
|
part = "a";
|
||||||
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
|
||||||
|
|
||||||
target = "";
|
target = "";
|
||||||
part = "";
|
part = "";
|
||||||
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
|
||||||
|
|
||||||
target = "a";
|
target = "a";
|
||||||
part = "";
|
part = "";
|
||||||
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
|
||||||
|
|
||||||
target = "a";
|
target = "a";
|
||||||
part = "a";
|
part = "a";
|
||||||
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtilTest::testIstartsWith() {
|
void UtilTest::testIstartsWith() {
|
||||||
|
@ -796,11 +814,13 @@ void UtilTest::testIstartsWith() {
|
||||||
part = "aBc";
|
part = "aBc";
|
||||||
CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), part.c_str()));
|
||||||
|
|
||||||
target = "abcdefg";
|
target = "abcdefg";
|
||||||
part = "abx";
|
part = "abx";
|
||||||
CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
|
CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
|
||||||
part.begin(), part.end()));
|
part.begin(), part.end()));
|
||||||
|
CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtilTest::testGetContentDispositionFilename() {
|
void UtilTest::testGetContentDispositionFilename() {
|
||||||
|
|
Loading…
Reference in New Issue