From ad6ffd7f33796889d80ba4aeed433e9b206ef96a Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 21 May 2008 07:16:54 +0000 Subject: [PATCH] 2008-05-21 Tatsuhiro Tsujikawa Added trimSelf(). Rewritten trim() to use trimSelf(). * Util.cc * Util.h --- ChangeLog | 6 ++++++ src/Util.cc | 20 +++++++++++++++----- src/Util.h | 8 +++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1536749c..ed817e59 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-05-21 Tatsuhiro Tsujikawa + + Added trimSelf(). Rewritten trim() to use trimSelf(). + * Util.cc + * Util.h + 2008-05-21 Tatsuhiro Tsujikawa * src/ChunkChecksum.h (getChecksum): Return const reference. diff --git a/src/Util.cc b/src/Util.cc index 937986f2..a8a947ab 100644 --- a/src/Util.cc +++ b/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); } } diff --git a/src/Util.h b/src/Util.h index 25ba6abe..353ec1ae 100644 --- a/src/Util.h +++ b/src/Util.h @@ -119,7 +119,13 @@ public: static void slice(std::deque& 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);