Procházet zdrojové kódy

Code cleanup. Avoid std::string temporaries.

Tatsuhiro Tsujikawa před 14 roky
rodič
revize
d305432ec0

+ 3 - 3
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);

+ 1 - 1
src/HttpHeaderProcessor.cc

@@ -116,7 +116,7 @@ SharedHandle<HttpHeader> 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.

+ 1 - 1
src/bittorrent_helper.cc

@@ -881,7 +881,7 @@ SharedHandle<TorrentAttribute> parseMagnet(const std::string& magnet)
     const String* xt = downcast<String>(*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);

+ 1 - 1
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)) {

+ 2 - 2
src/option_processing.cc

@@ -118,11 +118,11 @@ void option_processing(Option& op, std::vector<std::string>& 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);

+ 3 - 4
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());