Ver código fonte

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

	Use util::strip() instead of util::trim()
	* src/ExpatXmlRpcRequestProcessor.cc
	* src/FeatureConfig.cc
	* src/HttpHeader.cc
	* src/MetalinkParserStateV3Impl.cc
	* src/OptionHandlerImpl.h
	* src/ServerStatMan.cc
	* src/UriListParser.cc
	* src/Xml2XmlRpcRequestProcessor.cc
	* src/bittorrent_helper.cc
	* src/cookie_helper.cc
	* src/messageDigest.cc
	* src/util.cc
	* src/util.h
	* test/FeatureConfigTest.cc
	* test/HttpRequestTest.cc
	* test/UriListParserTest.cc
	* test/UtilTest.cc
Tatsuhiro Tsujikawa 15 anos atrás
pai
commit
1505671e7b

+ 21 - 0
ChangeLog

@@ -1,3 +1,24 @@
+2010-10-10  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Use util::strip() instead of util::trim()
+	* src/ExpatXmlRpcRequestProcessor.cc
+	* src/FeatureConfig.cc
+	* src/HttpHeader.cc
+	* src/MetalinkParserStateV3Impl.cc
+	* src/OptionHandlerImpl.h
+	* src/ServerStatMan.cc
+	* src/UriListParser.cc
+	* src/Xml2XmlRpcRequestProcessor.cc
+	* src/bittorrent_helper.cc
+	* src/cookie_helper.cc
+	* src/messageDigest.cc
+	* src/util.cc
+	* src/util.h
+	* test/FeatureConfigTest.cc
+	* test/HttpRequestTest.cc
+	* test/UriListParserTest.cc
+	* test/UtilTest.cc
+
 2010-10-10  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Added util::strip()

+ 2 - 2
src/ExpatXmlRpcRequestProcessor.cc

@@ -69,7 +69,7 @@ static void mlStartElement(void* userData, const char* name, const char** attrs)
       if(*p == 0) {
         break;
       }
-      std::string value = util::trim(*p++);
+      std::string value = util::strip(*p++);
       attrmap[name] = value;
     }
   }
@@ -84,7 +84,7 @@ static void mlEndElement(void* userData, const char* name)
   SessionData* sd = reinterpret_cast<SessionData*>(userData);
   std::string characters;
   if(sd->stm_->needsCharactersBuffering()) {
-    characters = util::trim(sd->charactersStack_.top());
+    characters = util::strip(sd->charactersStack_.top());
     sd->charactersStack_.pop();
   }
   sd->stm_->endElement(name, characters);

+ 1 - 1
src/FeatureConfig.cc

@@ -158,7 +158,7 @@ std::string FeatureConfig::featureSummary() const
       s += ", ";
     }
   }
-  return util::trim(s, ", ");
+  return util::strip(s, ", ");
 }
 
 } // namespace aria2

+ 1 - 1
src/HttpHeader.cc

@@ -214,7 +214,7 @@ void HttpHeader::fill(std::istream& in)
 {
   std::string line;
   while(std::getline(in, line)) {
-    line = util::trim(line);
+    line = util::strip(line);
     if(line.empty()) {
       continue;
     }

+ 14 - 14
src/MetalinkParserStateV3Impl.cc

@@ -113,7 +113,7 @@ void FilesMetalinkParserState::beginElement
     stm->setFileState();
     std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, NAME);
     if(itr != attrs.end()) {
-      std::string name = util::trim((*itr).value);
+      std::string name = util::strip((*itr).value);
       if(name.empty() || util::detectDirTraversal(name)) {
         return;
       }
@@ -196,7 +196,7 @@ void VersionMetalinkParserState::endElement
  const std::string& nsUri,
  const std::string& characters)
 {
-  stm->setVersionOfEntry(util::trim(characters));
+  stm->setVersionOfEntry(util::strip(characters));
 }
 
 void LanguageMetalinkParserState::endElement
@@ -206,7 +206,7 @@ void LanguageMetalinkParserState::endElement
  const std::string& nsUri,
  const std::string& characters)
 {
-  stm->setLanguageOfEntry(util::trim(characters));
+  stm->setLanguageOfEntry(util::strip(characters));
 }
 
 void OSMetalinkParserState::endElement
@@ -216,7 +216,7 @@ void OSMetalinkParserState::endElement
  const std::string& nsUri,
  const std::string& characters)
 {
-  stm->setOSOfEntry(util::trim(characters));
+  stm->setOSOfEntry(util::strip(characters));
 }
 
 void VerificationMetalinkParserState::beginElement
@@ -236,7 +236,7 @@ void VerificationMetalinkParserState::beginElement
     if(itr == attrs.end()) {
       return;
     } else {
-      std::string type = util::trim((*itr).value);
+      std::string type = util::strip((*itr).value);
       stm->newChecksumTransaction();
       stm->setTypeOfChecksum(type);
     }
@@ -258,7 +258,7 @@ void VerificationMetalinkParserState::beginElement
         if(itr == attrs.end()) {
           return;
         } else {
-          type = util::trim((*itr).value);
+          type = util::strip((*itr).value);
         }
       }
       stm->newChunkChecksumTransaction();
@@ -276,10 +276,10 @@ void VerificationMetalinkParserState::beginElement
         return;
       } else {
         stm->newSignatureTransaction();
-        stm->setTypeOfSignature(util::trim((*itr).value));
+        stm->setTypeOfSignature(util::strip((*itr).value));
         std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, FILE);
         if(itr != attrs.end()) {
-          std::string file = util::trim((*itr).value);
+          std::string file = util::strip((*itr).value);
           if(!util::detectDirTraversal(file)) {
             stm->setFileOfSignature(file);
           }
@@ -297,7 +297,7 @@ void HashMetalinkParserState::endElement
  const std::string& nsUri,
  const std::string& characters)
 {
-  stm->setHashOfChecksum(util::trim(characters));
+  stm->setHashOfChecksum(util::strip(characters));
   stm->commitChecksumTransaction();
 }
 
@@ -342,7 +342,7 @@ void PieceHashMetalinkParserState::endElement
  const std::string& nsUri,
  const std::string& characters)
 {
-  stm->setMessageDigestOfChunkChecksum(util::trim(characters));
+  stm->setMessageDigestOfChunkChecksum(util::strip(characters));
   stm->addHashOfChunkChecksum();
 }
 
@@ -353,7 +353,7 @@ void SignatureMetalinkParserState::endElement
  const std::string& nsUri,
  const std::string& characters)
 {
-  stm->setBodyOfSignature(util::trim(characters));
+  stm->setBodyOfSignature(util::strip(characters));
   stm->commitSignatureTransaction();
 }
 
@@ -374,14 +374,14 @@ void ResourcesMetalinkParserState::beginElement
       if(itr == attrs.end()) {
         return;
       } else {
-        type = util::trim((*itr).value);
+        type = util::strip((*itr).value);
       }
     }
     std::string location;
     {
       std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, LOCATION);
       if(itr != attrs.end()) {
-        location = util::trim((*itr).value);
+        location = util::strip((*itr).value);
       }
     }
     int preference;
@@ -430,7 +430,7 @@ void URLMetalinkParserState::endElement
  const std::string& nsUri,
  const std::string& characters)
 {
-  stm->setURLOfResource(util::trim(characters));
+  stm->setURLOfResource(util::strip(characters));
   stm->commitResourceTransaction();
 }
 

+ 1 - 1
src/OptionHandlerImpl.h

@@ -497,7 +497,7 @@ public:
     std::stringstream s;
     std::copy(validParamValues_.begin(), validParamValues_.end(),
               std::ostream_iterator<std::string>(s, ","));
-    return util::trim(s.str(), ", ");
+    return util::strip(s.str(), ", ");
   }
 };
 

+ 3 - 3
src/ServerStatMan.cc

@@ -98,7 +98,7 @@ bool ServerStatMan::load(std::istream& in)
 
   std::string line;
   while(getline(in, line)) {
-    util::trimSelf(line);
+    line = util::strip(line);
     if(line.empty()) {
       continue;
     }
@@ -108,8 +108,8 @@ bool ServerStatMan::load(std::istream& in)
     for(std::vector<std::string>::const_iterator i = items.begin(),
           eoi = items.end(); i != eoi; ++i) {
       std::pair<std::string, std::string> p = util::split(*i, "=");
-      util::trimSelf(p.first);
-      util::trimSelf(p.second);
+      p.first = util::strip(p.first);
+      p.second = util::strip(p.second);
       m[p.first] = p.second;
     }
     if(m[S_HOST].empty() || m[S_PROTOCOL].empty()) {

+ 1 - 1
src/UriListParser.cc

@@ -76,7 +76,7 @@ void UriListParser::parseNext(std::vector<std::string>& uris, Option& op)
     return;
   }
   do {
-    if(!util::startsWith(line_, A2STR::SHARP_C) && !util::trim(line_).empty()) {
+    if(!util::startsWith(line_, A2STR::SHARP_C) && !util::strip(line_).empty()){
       util::split(line_, std::back_inserter(uris), "\t", true);
       getOptions(op);
       return;

+ 2 - 2
src/Xml2XmlRpcRequestProcessor.cc

@@ -70,7 +70,7 @@ static void mlStartElement(void* userData, const xmlChar* name,
       if(*p == 0) {
         break;
       }
-      std::string value = util::trim(reinterpret_cast<const char*>(*p));
+      std::string value = util::strip(reinterpret_cast<const char*>(*p));
       ++p;
       attrmap[name] = value;
     }
@@ -86,7 +86,7 @@ static void mlEndElement(void* userData, const xmlChar* name)
   SessionData* sd = reinterpret_cast<SessionData*>(userData);
   std::string characters;
   if(sd->stm_->needsCharactersBuffering()) {
-    characters = util::trim(sd->charactersStack_.top());
+    characters = util::strip(sd->charactersStack_.top());
     sd->charactersStack_.pop();
   }
   sd->stm_->endElement(reinterpret_cast<const char*>(name), characters);

+ 3 - 3
src/bittorrent_helper.cc

@@ -325,7 +325,7 @@ static void extractAnnounce
             eoi2 = tier->end(); uriIter != eoi2; ++uriIter) {
         const String* uri = asString(*uriIter);
         if(uri) {
-          ntier.push_back(util::trim(uri->s()));
+          ntier.push_back(util::strip(uri->s()));
         }
       }
       if(!ntier.empty()) {
@@ -336,7 +336,7 @@ static void extractAnnounce
     const String* announce = asString(rootDict->get(C_ANNOUNCE));
     if(announce) {
       std::vector<std::string> tier;
-      tier.push_back(util::trim(announce->s()));
+      tier.push_back(util::strip(announce->s()));
       torrent->announceList.push_back(tier);
     }
   }
@@ -357,7 +357,7 @@ static void extractNodes
       if(!hostname) {
         continue;
       }
-      if(util::trim(hostname->s()).empty()) {
+      if(util::strip(hostname->s()).empty()) {
         continue;
       }
       const Integer* port = asInteger(addrPairList->get(1));

+ 4 - 4
src/cookie_helper.cc

@@ -204,11 +204,11 @@ bool parse
   if(eq == nvEnd) {
     return false;
   }
-  std::string cookieName = util::trim(std::string(cookieStr.begin(), eq));
+  std::string cookieName = util::stripIter(cookieStr.begin(), eq);
   if(cookieName.empty()) {
     return false;
   }
-  std::string cookieValue = util::trim(std::string(eq+1, nvEnd));
+  std::string cookieValue = util::stripIter(eq+1, nvEnd);
   time_t expiryTime = 0;
   bool foundExpires = false;
   bool persistent = false;
@@ -225,10 +225,10 @@ bool parse
     for(; j != end && *j != ';'; ++j);
     std::string::const_iterator eq = i;
     for(; eq != j && *eq != '='; ++eq);
-    std::string attrName = util::toLower(util::trim(std::string(i, eq)));
+    std::string attrName = util::toLower(util::stripIter(i, eq));
     std::string attrValue;
     if(eq != j) {
-      attrValue = util::trim(std::string(eq+1, j));
+      attrValue = util::stripIter(eq+1, j);
     }
     i = j;
     if(j != end) {

+ 1 - 1
src/messageDigest.cc

@@ -144,7 +144,7 @@ std::string MessageDigestContext::getSupportedAlgoString()
     algos += (*itr).first;
     algos += ", ";
   }
-  return util::trim(algos, ", ");
+  return util::strip(algos, ", ");
 }
 
 bool MessageDigestContext::isStronger

+ 14 - 32
src/util.cc

@@ -92,26 +92,7 @@ namespace aria2 {
 
 namespace util {
 
-const std::string DEFAULT_TRIM_CHARSET("\r\n\t ");
-
-std::string trim(const std::string& src, const std::string& trimCharset)
-{
-  std::string temp(src);
-  trimSelf(temp, trimCharset);
-  return temp;
-}
-
-void trimSelf(std::string& str, const std::string& trimCharset)
-{
-  std::string::size_type first = str.find_first_not_of(trimCharset);
-  if(first == std::string::npos) {
-    str.clear();
-  } else {
-    std::string::size_type last = str.find_last_not_of(trimCharset)+1;
-    str.erase(last);
-    str.erase(0, first);
-  }
-}
+const std::string DEFAULT_STRIP_CHARSET("\r\n\t ");
 
 std::string strip(const std::string& str, const std::string& chars)
 {
@@ -124,11 +105,11 @@ void split(std::pair<std::string, std::string>& hp, const std::string& src, char
   hp.second = A2STR::NIL;
   std::string::size_type p = src.find(delim);
   if(p == std::string::npos) {
-    hp.first = trim(src);
+    hp.first = strip(src);
     hp.second = A2STR::NIL;
   } else {
-    hp.first = trim(src.substr(0, p));
-    hp.second = trim(src.substr(p+1));
+    hp.first = strip(src.substr(0, p));
+    hp.second = strip(src.substr(p+1));
   }
 }
 
@@ -139,11 +120,11 @@ std::pair<std::string, std::string> split(const std::string& src, const std::str
   hp.second = A2STR::NIL;
   std::string::size_type p = src.find_first_of(delims);
   if(p == std::string::npos) {
-    hp.first = trim(src);
+    hp.first = strip(src);
     hp.second = A2STR::NIL;
   } else {
-    hp.first = trim(src.substr(0, p));
-    hp.second = trim(src.substr(p+1));
+    hp.first = strip(src.substr(0, p));
+    hp.second = strip(src.substr(p+1));
   }
   return hp;
 }
@@ -555,7 +536,8 @@ uint32_t parseUInt(const std::string& s, int base)
 
 bool parseUIntNoThrow(uint32_t& result, const std::string& s, int base)
 {
-  std::string trimed = trim(s);
+  // TODO what happens when we don't trim s?
+  std::string trimed = strip(s);
   if(trimed.empty()) {
     return false;
   }
@@ -577,7 +559,7 @@ bool parseUIntNoThrow(uint32_t& result, const std::string& s, int base)
 
 int64_t parseLLInt(const std::string& s, int32_t base)
 {
-  std::string trimed = trim(s);
+  std::string trimed = strip(s);
   if(trimed.empty()) {
     throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
                                    "empty string").str());
@@ -597,7 +579,7 @@ int64_t parseLLInt(const std::string& s, int32_t base)
 
 bool parseLLIntNoThrow(int64_t& result, const std::string& s, int base)
 {
-  std::string trimed = trim(s);
+  std::string trimed = strip(s);
   if(trimed.empty()) {
     return false;
   }
@@ -615,7 +597,7 @@ bool parseLLIntNoThrow(int64_t& result, const std::string& s, int base)
 
 uint64_t parseULLInt(const std::string& s, int base)
 {
-  std::string trimed = trim(s);
+  std::string trimed = strip(s);
   if(trimed.empty()) {
     throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
                                    "empty string").str());
@@ -800,7 +782,7 @@ static void parseParam(OutputIterator out, const std::string& header)
         param = std::string(paramFirst, paramLast);
       }
     }
-    trimSelf(param);
+    param = strip(param);
     *out++ = param;
     if(paramLast == eoi) {
       break;
@@ -910,7 +892,7 @@ std::string getContentDispositionFilename(const std::string& header)
       }
       static const std::string TRIMMED("\r\n\t '\"");
       value = percentDecode(std::string(value.begin(), filenameLast));
-      trimSelf(value, TRIMMED);
+      value = strip(value, TRIMMED);
       value.erase(std::remove(value.begin(), value.end(), '\\'), value.end());
       if(!detectDirTraversal(value) &&
          value.find(A2STR::SLASH_C) == std::string::npos) {

+ 8 - 14
src/util.h

@@ -126,18 +126,12 @@ std::string itos(int64_t value, bool comma = false);
 int64_t difftv(struct timeval tv1, struct timeval tv2);
 int32_t difftvsec(struct timeval tv1, struct timeval tv2);
 
-extern const std::string DEFAULT_TRIM_CHARSET;
-
-std::string trim(const std::string& src,
-                 const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
-
-void trimSelf(std::string& str,
-              const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
+extern const std::string DEFAULT_STRIP_CHARSET;
 
 template<typename InputIterator>
 std::string stripIter
 (InputIterator first, InputIterator last,
- const std::string& chars = DEFAULT_TRIM_CHARSET)
+ const std::string& chars = DEFAULT_STRIP_CHARSET)
 {
   if(std::distance(first, last) == 0) {
     return A2STR::NIL;
@@ -154,7 +148,7 @@ std::string stripIter
 }
 
 std::string strip
-(const std::string& str, const std::string& chars = DEFAULT_TRIM_CHARSET);
+(const std::string& str, const std::string& chars = DEFAULT_STRIP_CHARSET);
 
 bool startsWith(const std::string& target, const std::string& part);
 
@@ -340,7 +334,7 @@ std::map<size_t, std::string> createIndexPathMap(std::istream& i);
  */
 template<typename OutputIterator>
 OutputIterator split(const std::string& src, OutputIterator out,
-                     const std::string& delims, bool doTrim = false,
+                     const std::string& delims, bool doStrip = false,
                      bool allowEmpty = false)
 {
   std::string::size_type p = 0;
@@ -348,8 +342,8 @@ OutputIterator split(const std::string& src, OutputIterator out,
     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(doStrip) {
+        term = util::strip(term);
       }
       if(allowEmpty || !term.empty()) {
         *out = term;
@@ -358,8 +352,8 @@ OutputIterator split(const std::string& src, OutputIterator out,
       break;
     }
     std::string term = src.substr(p, np-p);
-    if(doTrim) {
-      term = util::trim(term);
+    if(doStrip) {
+      term = util::strip(term);
     }
     p = np+1;
     if(allowEmpty || !term.empty()) {

+ 1 - 2
test/FeatureConfigTest.cc

@@ -91,8 +91,7 @@ void FeatureConfigTest::testFeatureSummary() {
   const std::string delim(", ");
   std::for_each(vbegin(features), vend(features),
                 StringAppend(featuresString, delim));
-  // USE util::trimSelf(featureString);
-  featuresString = util::trim(featuresString, delim);
+  featuresString = util::strip(featuresString, delim);
   
   CPPUNIT_ASSERT_EQUAL(featuresString,
                        FeatureConfig::getInstance()->featureSummary());

+ 1 - 1
test/HttpRequestTest.cc

@@ -524,7 +524,7 @@ void HttpRequestTest::testCreateRequest_head()
   std::stringstream result(httpRequest.createRequest());
   std::string line;
   CPPUNIT_ASSERT(getline(result, line));
-  util::trimSelf(line);
+  line = util::strip(line);
   CPPUNIT_ASSERT_EQUAL(std::string("HEAD /aria2-1.0.0.tar.bz2 HTTP/1.1"), line);
 }
 

+ 1 - 1
test/UriListParserTest.cc

@@ -36,7 +36,7 @@ std::string UriListParserTest::list2String(const std::vector<std::string>& src)
 {
   std::ostringstream strm;
   std::copy(src.begin(), src.end(), std::ostream_iterator<std::string>(strm, " "));
-  return util::trim(strm.str());
+  return util::strip(strm.str());
 }
 
 void UriListParserTest::testHasNext()

+ 0 - 21
test/UtilTest.cc

@@ -19,7 +19,6 @@ namespace aria2 {
 class UtilTest:public CppUnit::TestFixture {
 
   CPPUNIT_TEST_SUITE(UtilTest);
-  CPPUNIT_TEST(testTrim);
   CPPUNIT_TEST(testStrip);
   CPPUNIT_TEST(testSplit);
   CPPUNIT_TEST(testSplit_many);
@@ -74,7 +73,6 @@ public:
   void setUp() {
   }
 
-  void testTrim();
   void testStrip();
   void testSplit();
   void testSplit_many();
@@ -127,25 +125,6 @@ public:
 
 CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
 
-void UtilTest::testTrim() {
-  std::string str1 = "aria2";
-  CPPUNIT_ASSERT_EQUAL(str1, util::trim("aria2"));
-  CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2"));
-  CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2 "));
-  CPPUNIT_ASSERT_EQUAL(str1, util::trim("  aria2  "));
-  std::string str2 = "aria2 debut";
-  CPPUNIT_ASSERT_EQUAL(str2, util::trim("aria2 debut"));
-  CPPUNIT_ASSERT_EQUAL(str2, util::trim(" aria2 debut "));
-  std::string str3 = "";
-  CPPUNIT_ASSERT_EQUAL(str3, util::trim(""));
-  CPPUNIT_ASSERT_EQUAL(str3, util::trim(" "));
-  CPPUNIT_ASSERT_EQUAL(str3, util::trim("  "));
-  std::string str4 = "A";
-  CPPUNIT_ASSERT_EQUAL(str4, util::trim("A"));
-  CPPUNIT_ASSERT_EQUAL(str4, util::trim(" A "));
-  CPPUNIT_ASSERT_EQUAL(str4, util::trim("  A  "));
-}
-
 void UtilTest::testStrip()
 {
   std::string str1 = "aria2";