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

Added trimSelf(). Rewritten trim() to use trimSelf().
	* Util.cc
	* Util.h
pull/1/head
Tatsuhiro Tsujikawa 2008-05-21 07:16:54 +00:00
parent 90846bdafe
commit ad6ffd7f33
3 changed files with 28 additions and 6 deletions

View File

@ -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.

View File

@ -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);
}
}

View File

@ -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);