Prechádzať zdrojové kódy

2009-10-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Replaced Util::slice() with split()
	* src/AbstractCommand.cc
	* src/CookieParser.cc
	* src/File.cc
	* src/HttpHeaderProcessor.cc
	* src/HttpRequest.cc
	* src/Metalink2RequestGroup.cc
	* src/MetalinkParserController.cc
	* src/Netrc.cc
	* src/NsCookieParser.cc
	* src/ParameterizedStringParser.cc
	* src/ServerStatMan.cc
	* src/UriListParser.cc
	* src/Util.cc
	* src/Util.h
	* src/bittorrent_helper.cc
	* test/UtilTest.cc
Tatsuhiro Tsujikawa 16 rokov pred
rodič
commit
c8d34a46be

+ 20 - 0
ChangeLog

@@ -1,3 +1,23 @@
+2009-10-18  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Replaced Util::slice() with split()
+	* src/AbstractCommand.cc
+	* src/CookieParser.cc
+	* src/File.cc
+	* src/HttpHeaderProcessor.cc
+	* src/HttpRequest.cc
+	* src/Metalink2RequestGroup.cc
+	* src/MetalinkParserController.cc
+	* src/Netrc.cc
+	* src/NsCookieParser.cc
+	* src/ParameterizedStringParser.cc
+	* src/ServerStatMan.cc
+	* src/UriListParser.cc
+	* src/Util.cc
+	* src/Util.h
+	* src/bittorrent_helper.cc
+	* test/UtilTest.cc
+
 2009-10-18  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Use request->getDir() instead of "/" for embedded user/pass in

+ 2 - 2
src/AbstractCommand.cc

@@ -414,8 +414,8 @@ public:
 static bool inNoProxy(const SharedHandle<Request>& req,
 		      const std::string& noProxy)
 {
-  std::deque<std::string> entries;
-  Util::slice(entries, noProxy, ',', true);
+  std::vector<std::string> entries;
+  split(noProxy, std::back_inserter(entries), ",", true);
   if(entries.empty()) {
     return false;
   }

+ 4 - 3
src/CookieParser.cc

@@ -39,6 +39,7 @@
 #include <utility>
 #include <istream>
 #include <map>
+#include <vector>
 
 #include "Util.h"
 #include "A2STR.h"
@@ -61,8 +62,8 @@ Cookie CookieParser::parse(const std::string& cookieStr) const
 
 Cookie CookieParser::parse(const std::string& cookieStr, const std::string& defaultDomain, const std::string& defaultPath) const
 {
-  std::deque<std::string> terms;
-  Util::slice(terms, Util::trim(cookieStr), ';', true);
+  std::vector<std::string> terms;
+  split(Util::trim(cookieStr), std::back_inserter(terms), ";", true);
   if(terms.empty()) {
     return Cookie();
   }
@@ -73,7 +74,7 @@ Cookie CookieParser::parse(const std::string& cookieStr, const std::string& defa
   values[C_DOMAIN] = defaultDomain;
   values[C_PATH] = defaultPath;
   
-  for(std::deque<std::string>::iterator itr = terms.begin()+1;
+  for(std::vector<std::string>::iterator itr = terms.begin()+1;
       itr != terms.end(); ++itr) {
     std::pair<std::string, std::string> nv;
     Util::split(nv, *itr, '=');

+ 5 - 5
src/File.cc

@@ -39,7 +39,7 @@
 #include <utime.h>
 #include <unistd.h>
 
-#include <deque>
+#include <vector>
 #include <cstring>
 #include <cstdio>
 
@@ -105,8 +105,8 @@ bool File::mkdirs() {
   if(isDir()) {
     return false;
   }
-  std::deque<std::string> dirs;
-  Util::slice(dirs, name, '/');
+  std::vector<std::string> dirs;
+  split(name, std::back_inserter(dirs), "/");
   if(!dirs.size()) {
     return true;
   }
@@ -115,8 +115,8 @@ bool File::mkdirs() {
   if(Util::startsWith(name, A2STR::SLASH_C)) {
     accDir = A2STR::SLASH_C;
   }
-  for(std::deque<std::string>::const_iterator itr = dirs.begin(); itr != dirs.end();
-      ++itr, accDir += A2STR::SLASH_C) {
+  for(std::vector<std::string>::const_iterator itr = dirs.begin();
+      itr != dirs.end(); ++itr, accDir += A2STR::SLASH_C) {
     accDir += *itr;
     if(File(accDir).isDir()) {
       continue;

+ 3 - 2
src/HttpHeaderProcessor.cc

@@ -35,6 +35,7 @@
 #include "HttpHeaderProcessor.h"
 
 #include <sstream>
+#include <vector>
 
 #include "HttpHeader.h"
 #include "message.h"
@@ -124,8 +125,8 @@ SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpRequestHeader()
      delimpos < 14) {
     throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
   }
-  std::deque<std::string> firstLine;
-  Util::slice(firstLine, _buf.substr(0, delimpos), ' ', true);
+  std::vector<std::string> firstLine;
+  split(_buf.substr(0, delimpos), std::back_inserter(firstLine), " ", true);
   if(firstLine.size() != 3) {
     throw DL_ABORT_EX("Malformed HTTP request header.");    
   }

+ 3 - 2
src/HttpRequest.cc

@@ -36,6 +36,7 @@
 
 #include <cassert>
 #include <numeric>
+#include <vector>
 
 #include "Segment.h"
 #include "Range.h"
@@ -276,8 +277,8 @@ void HttpRequest::disableContentEncoding()
 
 void HttpRequest::addHeader(const std::string& headersString)
 {
-  std::deque<std::string> headers;
-  Util::slice(headers, headersString, '\n', true);
+  std::vector<std::string> headers;
+  split(headersString, std::back_inserter(headers), "\n", true);
   _headers.insert(_headers.end(), headers.begin(), headers.end());
 }
 

+ 2 - 1
src/Metalink2RequestGroup.cc

@@ -143,7 +143,8 @@ Metalink2RequestGroup::createRequestGroup
     SharedHandle<MetalinkEntry>& entry = *itr;
     if(option->defined(PREF_METALINK_LOCATION)) {
       std::deque<std::string> locations;
-      Util::slice(locations, option->get(PREF_METALINK_LOCATION), ',', true);
+      split(option->get(PREF_METALINK_LOCATION), std::back_inserter(locations),
+	    ",", true);
       entry->setLocationPreference(locations, 100);
     }
     if(option->get(PREF_METALINK_PREFERRED_PROTOCOL) != V_NONE) {

+ 3 - 2
src/MetalinkParserController.cc

@@ -35,6 +35,7 @@
 #include "MetalinkParserController.h"
 
 #include <algorithm>
+#include <vector>
 
 #include "Metalinker.h"
 #include "MetalinkEntry.h"
@@ -75,8 +76,8 @@ void MetalinkParserController::setFileNameOfEntry(const std::string& filename)
   if(_tEntry.isNull()) {
     return;
   }
-  std::deque<std::string> elements;
-  Util::slice(elements, filename, '/');
+  std::vector<std::string> elements;
+  split(filename, std::back_inserter(elements), "/");
   std::string path = Util::joinPath(elements.begin(), elements.end());
 
   if(_tEntry->file.isNull()) {

+ 1 - 1
src/Netrc.cc

@@ -93,7 +93,7 @@ void Netrc::parse(const std::string& path)
       continue;
     }
     std::vector<std::string> tokens;
-    Util::split(line, std::back_inserter(tokens), " \t", true);
+    split(line, std::back_inserter(tokens), " \t", true);
     for(std::vector<std::string>::const_iterator iter = tokens.begin();
 	iter != tokens.end(); ++iter) {
       const std::string& token = *iter;

+ 3 - 2
src/NsCookieParser.cc

@@ -35,6 +35,7 @@
 #include "NsCookieParser.h"
 
 #include <fstream>
+#include <vector>
 
 #include "Util.h"
 #include "A2STR.h"
@@ -51,8 +52,8 @@ static const std::string C_TRUE("TRUE");
 
 static Cookie parseNsCookie(const std::string& nsCookieStr)
 {
-  std::deque<std::string> vs;
-  Util::slice(vs, nsCookieStr, '\t', true);
+  std::vector<std::string> vs;
+  split(nsCookieStr, std::back_inserter(vs), "\t", true);
   if(vs.size() < 6 ) {
     return Cookie();
   }

+ 2 - 1
src/ParameterizedStringParser.cc

@@ -91,7 +91,8 @@ PStringDatumHandle ParameterizedStringParser::createSelect(const std::string& sr
     throw DL_ABORT_EX("Missing '}' in the parameterized string.");
   }
   std::deque<std::string> values;
-  Util::slice(values, src.substr(offset, rightParenIndex-offset), ',', true);
+  split(src.substr(offset, rightParenIndex-offset), std::back_inserter(values),
+	",", true);
   if(values.empty()) {
     throw DL_ABORT_EX("Empty {} is not allowed.");
   }

+ 4 - 3
src/ServerStatMan.cc

@@ -38,6 +38,7 @@
 #include <ostream>
 #include <iterator>
 #include <map>
+#include <vector>
 
 #include "ServerStat.h"
 #include "Util.h"
@@ -101,10 +102,10 @@ bool ServerStatMan::load(std::istream& in)
     if(line.empty()) {
       continue;
     }
-    std::deque<std::string> items;
-    Util::slice(items, line, ',');
+    std::vector<std::string> items;
+    split(line, std::back_inserter(items), ",");
     std::map<std::string, std::string> m;
-    for(std::deque<std::string>::const_iterator i = items.begin();
+    for(std::vector<std::string>::const_iterator i = items.begin();
 	i != items.end(); ++i) {
       std::pair<std::string, std::string> p = Util::split(*i, "=");
       Util::trimSelf(p.first);

+ 1 - 1
src/UriListParser.cc

@@ -74,7 +74,7 @@ void UriListParser::parseNext(std::deque<std::string>& uris, Option& op)
   }
   do {
     if(!Util::trim(_line).empty()) {
-      Util::slice(uris, _line, '\t', true);
+      split(_line, std::back_inserter(uris), "\t", true);
       getOptions(op);
       return;
     }

+ 0 - 25
src/Util.cc

@@ -141,31 +141,6 @@ int32_t Util::difftvsec(struct timeval tv1, struct timeval tv2) {
   return tv1.tv_sec-tv2.tv_sec;
 }
 
-void Util::slice(std::deque<std::string>& result, const std::string& src, char delim, bool doTrim) {
-  std::string::size_type p = 0;
-  while(1) {
-    std::string::size_type np = src.find(delim, p);
-    if(np == std::string::npos) {
-      std::string term = src.substr(p);
-      if(doTrim) {
-	term = trim(term);
-      }
-      if(term.size()) {
-	result.push_back(term);
-      }
-      break;
-    }
-    std::string term = src.substr(p, np-p);
-    if(doTrim) {
-      term = trim(term);
-    }
-    p = np+1;
-    if(term.size()) {
-      result.push_back(term);
-    }
-  } 
-}
-
 bool Util::startsWith(const std::string& target, const std::string& part) {
   if(target.size() < part.size()) {
     return false;

+ 35 - 37
src/Util.h

@@ -133,43 +133,6 @@ public:
    */
   static int64_t difftv(struct timeval tv1, struct timeval tv2);
   static int32_t difftvsec(struct timeval tv1, struct timeval tv2);
-  /**
-   * Take a string src which is a deliminated list and add its elements
-   * into result. result is not cleared before conversion begins.
-   */
-  static void slice(std::deque<std::string>& result, const std::string& src,
-		    char delim, bool trim = false);
-  
-  template<typename OutputIterator>
-  static OutputIterator split(const std::string& src, OutputIterator out,
-			      const std::string& delims, bool doTrim = false)
-  {
-    std::string::size_type p = 0;
-    while(1) {
-      std::string::size_type np = src.find_first_of(delims, p);
-      if(np == std::string::npos) {
-	std::string term = src.substr(p);
-	if(doTrim) {
-	  term = trim(term);
-	}
-	if(!term.empty()) {
-	  *out = term;
-	  ++out;
-	}
-	break;
-      }
-      std::string term = src.substr(p, np-p);
-      if(doTrim) {
-	term = trim(term);
-      }
-      p = np+1;
-      if(!term.empty()) {
-	*out = term;
-	++out;
-      }
-    }
-    return out;
-  }
 
   static const std::string DEFAULT_TRIM_CHARSET;
 
@@ -334,6 +297,41 @@ public:
   static std::map<size_t, std::string> createIndexPathMap(std::istream& i);
 };
 
+/**
+ * Take a string src which is a deliminated list and add its elements
+ * into result. result is stored in out.
+ */
+template<typename OutputIterator>
+static OutputIterator split(const std::string& src, OutputIterator out,
+			    const std::string& delims, bool doTrim = false)
+{
+  std::string::size_type p = 0;
+  while(1) {
+    std::string::size_type np = src.find_first_of(delims, p);
+    if(np == std::string::npos) {
+      std::string term = src.substr(p);
+      if(doTrim) {
+	term = Util::trim(term);
+      }
+      if(!term.empty()) {
+	*out = term;
+	++out;
+      }
+      break;
+    }
+    std::string term = src.substr(p, np-p);
+    if(doTrim) {
+      term = Util::trim(term);
+    }
+    p = np+1;
+    if(!term.empty()) {
+      *out = term;
+      ++out;
+    }
+  }
+  return out;
+}
+
 } // namespace aria2
 
 #endif // _D_UTIL_H_

+ 5 - 5
src/bittorrent_helper.cc

@@ -195,9 +195,9 @@ static void extractFileEntries
     }
     const BDE& nameData = infoDict[nameKey];
     if(nameData.isString()) {
-      // Slice path by '/' just in case nasty ".." is included in name
-      std::deque<std::string> pathelems;
-      Util::slice(pathelems, nameData.s(), '/');
+      // Split path by '/' just in case nasty ".." is included in name
+      std::vector<std::string> pathelems;
+      split(nameData.s(), std::back_inserter(pathelems), "/");
       name = Util::joinPath(pathelems.begin(), pathelems.end());
       torrent[NAME] = nameData.s();
     } else {
@@ -248,8 +248,8 @@ static void extractFileEntries
       strappend(path, "/", Util::joinPath(pathelem.begin(), pathelem.end()));
       // Split path with '/' again because each pathList element can
       // contain "/" inside.
-      std::deque<std::string> elements;
-      Util::slice(elements, path, '/');
+      std::vector<std::string> elements;
+      split(path, std::back_inserter(elements), "/");
       path = Util::joinPath(elements.begin(), elements.end());
 
       std::deque<std::string> uris;

+ 7 - 8
test/UtilTest.cc

@@ -20,7 +20,7 @@ class UtilTest:public CppUnit::TestFixture {
   CPPUNIT_TEST_SUITE(UtilTest);
   CPPUNIT_TEST(testTrim);
   CPPUNIT_TEST(testSplit);
-  CPPUNIT_TEST(testSlice);
+  CPPUNIT_TEST(testSplit_many);
   CPPUNIT_TEST(testEndsWith);
   CPPUNIT_TEST(testReplace);
   CPPUNIT_TEST(testStartsWith);
@@ -63,7 +63,7 @@ public:
 
   void testTrim();
   void testSplit();
-  void testSlice();
+  void testSplit_many();
   void testEndsWith();
   void testReplace();
   void testStartsWith();
@@ -140,12 +140,10 @@ void UtilTest::testSplit() {
   CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
 }
 
-void UtilTest::testSlice() {
+void UtilTest::testSplit_many() {
   std::deque<std::string> v1;
-  Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true);
-  CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
-  v1.clear();
-  Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
+  split("name1=value1; name2=value2; name3=value3", std::back_inserter(v1),
+	";", true);
   CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
   std::deque<std::string>::iterator itr = v1.begin();
   CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
@@ -154,7 +152,8 @@ void UtilTest::testSlice() {
 
   v1.clear();
 
-  Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false);
+  split("name1=value1; name2=value2; name3=value3", std::back_inserter(v1),
+	";", false);
   CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
   itr = v1.begin();
   CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);