Removed util::endsWith(a, b). Added util::iendsWith()

pull/2/head
Tatsuhiro Tsujikawa 2011-11-05 00:25:24 +09:00
parent 601ec0f44a
commit 79876af88f
13 changed files with 68 additions and 42 deletions

View File

@ -215,7 +215,7 @@ AuthConfigFactory::BasicCred::BasicCred
user_(user), password_(password), user_(user), password_(password),
host_(host), port_(port), path_(path), activated_(activated) host_(host), port_(port), path_(path), activated_(activated)
{ {
if(!util::endsWith(path_, "/")) { if(path_.empty() || path_[path_.size()-1] != '/') {
path_ += "/"; path_ += "/";
} }
} }

View File

@ -49,7 +49,8 @@ bool tailMatch
(InputIterator first, InputIterator last, const std::string& target) (InputIterator first, InputIterator last, const std::string& target)
{ {
for(; first != last; ++first) { for(; first != last; ++first) {
if(util::endsWith(target, *first)) { if(util::endsWith(target.begin(), target.end(),
(*first).begin(), (*first).end())) {
return true; return true;
} }
} }

View File

@ -71,9 +71,13 @@ bool DHTMessageTrackerEntry::match(const std::string& transactionID, const std::
if(targetNode_->getIPAddress() == ipaddr) { if(targetNode_->getIPAddress() == ipaddr) {
return true; return true;
} }
if(util::endsWith(targetNode_->getIPAddress(), ipaddr)) { if(util::endsWith(targetNode_->getIPAddress().begin(),
targetNode_->getIPAddress().end(),
ipaddr.begin(), ipaddr.end())) {
return targetNode_->getIPAddress() == "::ffff:"+ipaddr; return targetNode_->getIPAddress() == "::ffff:"+ipaddr;
} else if(util::endsWith(ipaddr, targetNode_->getIPAddress())) { } else if(util::endsWith(ipaddr.begin(), ipaddr.end(),
targetNode_->getIPAddress().begin(),
targetNode_->getIPAddress().end())) {
return ipaddr == "::ffff:"+targetNode_->getIPAddress(); return ipaddr == "::ffff:"+targetNode_->getIPAddress();
} }
return false; return false;

View File

@ -380,8 +380,11 @@ void DownloadCommand::installStreamFilter
} }
streamFilter->installDelegate(streamFilter_); streamFilter->installDelegate(streamFilter_);
streamFilter_ = streamFilter; streamFilter_ = streamFilter;
const std::string& name = streamFilter_->getName();
sinkFilterOnly_ = sinkFilterOnly_ =
util::endsWith(streamFilter_->getName(), SinkStreamFilter::NAME); util::endsWith(name.begin(), name.end(),
SinkStreamFilter::NAME.begin(),
SinkStreamFilter::NAME.end());
} }
} // namespace aria2 } // namespace aria2

View File

@ -87,12 +87,15 @@ bool HttpDownloadCommand::prepareForNextSegment() {
getDownloadEngine()->addCommand(command); getDownloadEngine()->addCommand(command);
return true; return true;
} else { } else {
const std::string& streamFilterName = getStreamFilter()->getName();
if(getRequest()->isPipeliningEnabled() || if(getRequest()->isPipeliningEnabled() ||
(getRequest()->isKeepAliveEnabled() && (getRequest()->isKeepAliveEnabled() &&
( (
// Make sure that all filters are finished to pool socket // Make sure that all filters are finished to pool socket
(!util::endsWith(getStreamFilter()->getName(), (!util::endsWith(streamFilterName.begin(),
SinkStreamFilter::NAME) && streamFilterName.end(),
SinkStreamFilter::NAME.begin(),
SinkStreamFilter::NAME.end()) &&
getStreamFilter()->finished()) || getStreamFilter()->finished()) ||
getRequestEndOffset() == getRequestEndOffset() ==
getFileEntry()->gtoloff(getSegments().front()->getPositionToWrite()) getFileEntry()->gtoloff(getSegments().front()->getPositionToWrite())

View File

@ -185,7 +185,9 @@ void HttpServer::feedResponse(const std::string& status,
} }
if(!headers.empty()) { if(!headers.empty()) {
header += headers; header += headers;
if(!util::endsWith(headers, "\r\n")) { if(headers.size() < 2 ||
(headers[headers.size()-2] != '\r' &&
headers[headers.size()-1] != '\n')) {
header += "\r\n"; header += "\r\n";
} }
} }

View File

@ -94,8 +94,11 @@ void HttpSkipResponseCommand::installStreamFilter
} }
streamFilter->installDelegate(streamFilter_); streamFilter->installDelegate(streamFilter_);
streamFilter_ = streamFilter; streamFilter_ = streamFilter;
const std::string& name = streamFilter_->getName();
sinkFilterOnly_ = sinkFilterOnly_ =
util::endsWith(streamFilter_->getName(), SinkStreamFilter::NAME); util::endsWith(name.begin(), name.end(),
SinkStreamFilter::NAME.begin(),
SinkStreamFilter::NAME.end());
} }
bool HttpSkipResponseCommand::executeInternal() bool HttpSkipResponseCommand::executeInternal()

View File

@ -520,7 +520,9 @@ HttpProxyUserOptionHandler::HttpProxyUserOptionHandler
void HttpProxyUserOptionHandler::parseArg void HttpProxyUserOptionHandler::parseArg
(Option& option, const std::string& optarg) (Option& option, const std::string& optarg)
{ {
if(util::endsWith(pref_->k, "-user")) { const char A2_USER[] = "-user";
if(util::endsWith(pref_->k.begin(), pref_->k.end(),
A2_USER, vend(A2_USER)-1)) {
const Pref* proxyPref = option::k2p(pref_->k.substr(0, pref_->k.size()-5)); const Pref* proxyPref = option::k2p(pref_->k.substr(0, pref_->k.size()-5));
const std::string& olduri = option.get(proxyPref); const std::string& olduri = option.get(proxyPref);
if(!olduri.empty()) { if(!olduri.empty()) {
@ -562,7 +564,9 @@ HttpProxyPasswdOptionHandler::HttpProxyPasswdOptionHandler
void HttpProxyPasswdOptionHandler::parseArg void HttpProxyPasswdOptionHandler::parseArg
(Option& option, const std::string& optarg) (Option& option, const std::string& optarg)
{ {
if(util::endsWith(pref_->k, "-passwd")) { const char A2_PASSWD[] = "-passwd";
if(util::endsWith(pref_->k.begin(), pref_->k.end(),
A2_PASSWD, vend(A2_PASSWD)-1)) {
const Pref* proxyPref = option::k2p(pref_->k.substr(0, pref_->k.size()-7)); const Pref* proxyPref = option::k2p(pref_->k.substr(0, pref_->k.size()-7));
const std::string& olduri = option.get(proxyPref); const std::string& olduri = option.get(proxyPref);
if(!olduri.empty()) { if(!olduri.empty()) {

View File

@ -182,7 +182,7 @@ OutputIterator createUri
const std::string& filePath) const std::string& filePath)
{ {
for(; first != last; ++first) { for(; first != last; ++first) {
if(util::endsWith(*first, "/")) { if(!(*first).empty() && (*first)[(*first).size()-1] == '/') {
*out++ = (*first)+filePath; *out++ = (*first)+filePath;
} else { } else {
*out++ = (*first)+"/"+filePath; *out++ = (*first)+"/"+filePath;
@ -310,7 +310,7 @@ void extractFileEntries
std::vector<std::string> uris; std::vector<std::string> uris;
for(std::vector<std::string>::const_iterator i = urlList.begin(), for(std::vector<std::string>::const_iterator i = urlList.begin(),
eoi = urlList.end(); i != eoi; ++i) { eoi = urlList.end(); i != eoi; ++i) {
if(util::endsWith(*i, A2STR::SLASH_C)) { if(!(*i).empty() && (*i)[(*i).size()-1] == '/') {
uris.push_back((*i)+util::percentEncode(utf8Name)); uris.push_back((*i)+util::percentEncode(utf8Name));
} else { } else {
uris.push_back(*i); uris.push_back(*i);

View File

@ -368,7 +368,8 @@ std::string canonicalizeHost(const std::string& host)
bool domainMatch(const std::string& requestHost, const std::string& domain) bool domainMatch(const std::string& requestHost, const std::string& domain)
{ {
return requestHost == domain || return requestHost == domain ||
(util::endsWith(requestHost, domain) && (util::endsWith(requestHost.begin(), requestHost.end(),
domain.begin(), domain.end()) &&
requestHost[requestHost.size()-domain.size()-1] == '.' && requestHost[requestHost.size()-domain.size()-1] == '.' &&
!util::isNumericHost(requestHost)); !util::isNumericHost(requestHost));
} }

View File

@ -230,20 +230,6 @@ int32_t difftvsec(struct timeval tv1, struct timeval tv2) {
return tv1.tv_sec-tv2.tv_sec; return tv1.tv_sec-tv2.tv_sec;
} }
bool endsWith(const std::string& target, const std::string& part) {
if(target.size() < part.size()) {
return false;
}
if(part.empty()) {
return true;
}
if(target.rfind(part) == target.size()-part.size()) {
return true;
} else {
return false;
}
}
std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr) { std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr) {
if(target.empty() || oldstr.empty()) { if(target.empty() || oldstr.empty()) {
return target; return target;
@ -1516,7 +1502,9 @@ void executeHook
memset(&pi, 0, sizeof (pi)); memset(&pi, 0, sizeof (pi));
bool batch = util::endsWith(util::toLower(command), ".bat"); const char A2_BAT[] = ".bat";
bool batch = util::iendsWith(command.begin(), command.end(),
A2_BAT, vend(A2_BAT)-1);
std::string cmdline; std::string cmdline;
std::string cmdexe; std::string cmdexe;
if(batch) { if(batch) {
@ -1618,7 +1606,8 @@ bool noProxyDomainMatch
const std::string& domain) const std::string& domain)
{ {
if(!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) { if(!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) {
return util::endsWith(hostname, domain); return util::endsWith(hostname.begin(), hostname.end(),
domain.begin(), domain.end());
} else { } else {
return hostname == domain; return hostname == domain;
} }

View File

@ -205,10 +205,6 @@ InputIterator lstripIter
std::string strip std::string strip
(const std::string& str, const std::string& chars = DEFAULT_STRIP_CHARSET); (const std::string& str, const std::string& chars = DEFAULT_STRIP_CHARSET);
bool startsWith(const std::string& target, const std::string& part);
bool endsWith(const std::string& target, const std::string& part);
std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr); std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr);
std::string percentEncode(const unsigned char* target, size_t len); std::string percentEncode(const unsigned char* target, size_t len);
@ -714,6 +710,20 @@ bool endsWith
return std::equal(first2, last2, last1-(last2-first2)); return std::equal(first2, last2, last1-(last2-first2));
} }
template<typename InputIterator1, typename InputIterator2>
bool iendsWith
(InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2)
{
if(last1-first1 < last2-first2) {
return false;
}
first1 = last1-(last2-first2);
return strieq(first1, last1, first2, last2);
}
void generateRandomData(unsigned char* data, size_t length); void generateRandomData(unsigned char* data, size_t length);
// Saves data to file whose name is filename. If overwrite is true, // Saves data to file whose name is filename. If overwrite is true,

View File

@ -33,6 +33,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testStreq); CPPUNIT_TEST(testStreq);
CPPUNIT_TEST(testStrieq); CPPUNIT_TEST(testStrieq);
CPPUNIT_TEST(testEndsWith); CPPUNIT_TEST(testEndsWith);
CPPUNIT_TEST(testIendsWith);
CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testReplace);
CPPUNIT_TEST(testStartsWith); CPPUNIT_TEST(testStartsWith);
// may be moved to other helper class in the future. // may be moved to other helper class in the future.
@ -100,6 +101,7 @@ public:
void testStreq(); void testStreq();
void testStrieq(); void testStrieq();
void testEndsWith(); void testEndsWith();
void testIendsWith();
void testReplace(); void testReplace();
void testStartsWith(); void testStartsWith();
// may be moved to other helper class in the future. // may be moved to other helper class in the future.
@ -635,53 +637,57 @@ void UtilTest::testSplitIterM() {
void UtilTest::testEndsWith() { void UtilTest::testEndsWith() {
std::string target = "abcdefg"; std::string target = "abcdefg";
std::string part = "fg"; std::string part = "fg";
CPPUNIT_ASSERT(util::endsWith(target, part));
CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
target = "abdefg"; target = "abdefg";
part = "g"; part = "g";
CPPUNIT_ASSERT(util::endsWith(target, part));
CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
target = "abdefg"; target = "abdefg";
part = "eg"; part = "eg";
CPPUNIT_ASSERT(!util::endsWith(target, part));
CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
target = "g"; target = "g";
part = "eg"; part = "eg";
CPPUNIT_ASSERT(!util::endsWith(target, part));
CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
target = "g"; target = "g";
part = "g"; part = "g";
CPPUNIT_ASSERT(util::endsWith(target, part));
CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
target = "g"; target = "g";
part = ""; part = "";
CPPUNIT_ASSERT(util::endsWith(target, part));
CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
target = ""; target = "";
part = ""; part = "";
CPPUNIT_ASSERT(util::endsWith(target, part));
CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
target = ""; target = "";
part = "g"; part = "g";
CPPUNIT_ASSERT(!util::endsWith(target, part));
CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(), CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
part.begin(), part.end())); part.begin(), part.end()));
} }
void UtilTest::testIendsWith() {
std::string target = "abcdefg";
std::string part = "Fg";
CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
part.begin(), part.end()));
target = "abdefg";
part = "ef";
CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
part.begin(), part.end()));
}
void UtilTest::testStreq() void UtilTest::testStreq()
{ {
std::string s1, s2; std::string s1, s2;