Forráskód Böngészése

2008-05-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Added trimSelf(). Rewritten trim() to use trimSelf().
	* Util.cc
	* Util.h
Tatsuhiro Tsujikawa 17 éve
szülő
commit
ad6ffd7f33
3 módosított fájl, 28 hozzáadás és 6 törlés
  1. 6 0
      ChangeLog
  2. 15 5
      src/Util.cc
  3. 7 1
      src/Util.h

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+2008-05-21  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Added trimSelf(). Rewritten trim() to use trimSelf().
+	* Util.cc
+	* Util.h
+
 2008-05-21  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	* src/ChunkChecksum.h (getChecksum): Return const reference.

+ 15 - 5
src/Util.cc

@@ -60,14 +60,24 @@
 
 namespace aria2 {
 
+const std::string Util::DEFAULT_TRIM_CHARSET("\r\n\t ");
+
 std::string Util::trim(const std::string& src, const std::string& trimCharset)
 {
-  std::string::size_type sp = src.find_first_not_of(trimCharset);
-  std::string::size_type ep = src.find_last_not_of(trimCharset);
-  if(sp == std::string::npos || ep == std::string::npos) {
-    return A2STR::NIL;
+  std::string temp(src);
+  trimSelf(temp, trimCharset);
+  return temp;
+}
+
+void Util::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 {
-    return src.substr(sp, ep-sp+1);
+    std::string::size_type last = str.find_last_not_of(trimCharset)+1;
+    str.erase(last);
+    str.erase(0, first);
   }
 }
 

+ 7 - 1
src/Util.h

@@ -119,7 +119,13 @@ public:
   static void slice(std::deque<std::string>& result, const std::string& src,
 		    char delim, bool trim = false);
   
-  static std::string trim(const std::string& src, const std::string& trimCharset = "\r\n\t ");
+  static const std::string DEFAULT_TRIM_CHARSET;
+
+  static std::string trim(const std::string& src,
+			  const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
+
+  static void trimSelf(std::string& str,
+		       const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
 
   static bool startsWith(const std::string& target, const std::string& part);