diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-10-09 16:22:56 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-10-09 16:22:56 +0000 |
| commit | dbea00fa9d8d4b9ca32b7bbea6ecc4e6348a5c12 (patch) | |
| tree | 29cc8d4baf5de7c9f459de790135bd6aff1e7fbd /src | |
| parent | f816434d06c6bc66b2421d29c8d2ffecd04868ba (diff) | |
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added util::strip()
* src/util.cc
* src/util.h
* test/UtilTest.cc
Diffstat (limited to 'src')
| -rw-r--r-- | src/util.cc | 5 | ||||
| -rw-r--r-- | src/util.h | 22 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc index d0e3d9a8..3846e4f9 100644 --- a/src/util.cc +++ b/src/util.cc @@ -113,6 +113,11 @@ void trimSelf(std::string& str, const std::string& trimCharset) } } +std::string strip(const std::string& str, const std::string& chars) +{ + return stripIter(str.begin(), str.end(), chars); +} + void split(std::pair<std::string, std::string>& hp, const std::string& src, char delim) { hp.first = A2STR::NIL; @@ -134,6 +134,28 @@ std::string trim(const std::string& src, void trimSelf(std::string& str, const std::string& trimCharset = DEFAULT_TRIM_CHARSET); +template<typename InputIterator> +std::string stripIter +(InputIterator first, InputIterator last, + const std::string& chars = DEFAULT_TRIM_CHARSET) +{ + if(std::distance(first, last) == 0) { + return A2STR::NIL; + } + for(; first != last && + std::find(chars.begin(), chars.end(), *first) != chars.end(); ++first); + if(first == last) { + return A2STR::NIL; + } + InputIterator left = last-1; + for(; left != first && + std::find(chars.begin(), chars.end(), *left) != chars.end(); --left); + return std::string(first, left+1); +} + +std::string strip +(const std::string& str, const std::string& chars = DEFAULT_TRIM_CHARSET); + bool startsWith(const std::string& target, const std::string& part); bool endsWith(const std::string& target, const std::string& part); |
