From d305432ec0015cfc9479331531a78f29c398bd8c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 5 Nov 2011 01:26:09 +0900 Subject: [PATCH] Code cleanup. Avoid std::string temporaries. --- src/FtpConnection.cc | 6 +++--- src/HttpHeaderProcessor.cc | 2 +- src/bittorrent_helper.cc | 2 +- src/cookie_helper.cc | 2 +- src/option_processing.cc | 4 ++-- src/util.cc | 7 +++---- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index dcc38e39..d6cd94f0 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -395,7 +395,7 @@ bool FtpConnection::bulkReceiveResponse std::string::size_type length; if((length = findEndOfResponse(status, strbuf_)) != std::string::npos) { response.first = status; - response.second = strbuf_.substr(0, length); + response.second.assign(strbuf_.begin(), strbuf_.begin()+length); A2_LOG_INFO(fmt(MSG_RECEIVE_RESPONSE, cuid_, response.second.c_str())); @@ -516,7 +516,7 @@ unsigned int FtpConnection::receivePasvResponse unsigned int h1, h2, h3, h4, p1, p2; std::string::size_type p = response.second.find("("); if(p >= 4) { - sscanf(response.second.substr(response.second.find("(")).c_str(), + sscanf(response.second.c_str()+p, "(%u,%u,%u,%u,%u,%u).", &h1, &h2, &h3, &h4, &p1, &p2); // ip address @@ -549,7 +549,7 @@ unsigned int FtpConnection::receivePwdResponse(std::string& pwd) if((first = response.second.find("\"")) != std::string::npos && (last = response.second.find("\"", ++first)) != std::string::npos) { - pwd = response.second.substr(first, last-first); + pwd.assign(response.second.begin()+first, response.second.begin()+last); } else { throw DL_ABORT_EX2(EX_INVALID_RESPONSE, error_code::FTP_PROTOCOL_ERROR); diff --git a/src/HttpHeaderProcessor.cc b/src/HttpHeaderProcessor.cc index d196f6be..b86e58c3 100644 --- a/src/HttpHeaderProcessor.cc +++ b/src/HttpHeaderProcessor.cc @@ -116,7 +116,7 @@ SharedHandle HttpHeaderProcessor::getHttpResponseHeader() throw DL_RETRY_EX("Status code could not be parsed as integer."); } HttpHeaderHandle httpHeader(new HttpHeader()); - httpHeader->setVersion(buf_.substr(0, 8)); + httpHeader->setVersion(buf_.begin(), buf_.begin()+8); httpHeader->setStatusCode(statusCode); // TODO 1st line(HTTP/1.1 200...) is also send to HttpHeader, but it should // not. diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index e1d47d64..0346d96b 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -881,7 +881,7 @@ SharedHandle parseMagnet(const std::string& magnet) const String* xt = downcast(*xtiter); if(util::startsWith(xt->s().begin(), xt->s().end(), A2_URN_BTIH, vend(A2_URN_BTIH)-1)) { - std::string xtarg = xt->s().substr(9); + std::string xtarg(xt->s().begin()+9, xt->s().end()); size_t size = xtarg.size(); if(size == 32) { std::string rawhash = base32::decode(xtarg); diff --git a/src/cookie_helper.cc b/src/cookie_helper.cc index 653d2d9b..bbdd6bb7 100644 --- a/src/cookie_helper.cc +++ b/src/cookie_helper.cc @@ -139,7 +139,7 @@ bool parseDate "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; if((*i).size() >= 3) { - std::string head = (*i).substr(0, 3); + std::string head((*i).begin(), (*i).begin()+3); util::lowercase(head); std::string* mptr = std::find(vbegin(MONTH), vend(MONTH), head); if(mptr != vend(MONTH)) { diff --git a/src/option_processing.cc b/src/option_processing.cc index d016173e..573b95ae 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -118,11 +118,11 @@ void option_processing(Option& op, std::vector& uris, const char A2_HH[] = "--"; if(util::startsWith(keyword.begin(), keyword.end(), A2_HH, vend(A2_HH)-1)) { - keyword = keyword.substr(2); + keyword.erase(keyword.begin(), keyword.begin()+2); } std::string::size_type eqpos = keyword.find("="); if(eqpos != std::string::npos) { - keyword = keyword.substr(0, eqpos); + keyword.erase(keyword.begin()+eqpos, keyword.end()); } } showUsage(keyword, oparser); diff --git a/src/util.cc b/src/util.cc index 42834767..cf255939 100644 --- a/src/util.cc +++ b/src/util.cc @@ -238,13 +238,12 @@ std::string replace(const std::string& target, const std::string& oldstr, const std::string::size_type p = 0; std::string::size_type np = target.find(oldstr); while(np != std::string::npos) { - result += target.substr(p, np-p); + result.append(target.begin()+p, target.begin()+np); result += newstr; p = np+oldstr.size(); np = target.find(oldstr, p); } - result += target.substr(p); - + result.append(target.begin()+p, target.end()); return result; } @@ -966,7 +965,7 @@ int64_t getRealSize(const std::string& sizeWithUnit) } else if(sizeWithUnit[p] == 'M') { mult = 1024*1024; } - size = sizeWithUnit.substr(0, p); + size.assign(sizeWithUnit.begin(), sizeWithUnit.begin()+p); } int64_t v = parseLLInt(size.begin(), size.end());