diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-10-10 06:58:31 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-10-10 06:58:31 +0000 |
| commit | 0abd4a2f7b34b35d9c01b2fcecfdb241eeb4db32 (patch) | |
| tree | 541e098d186675da1e971791c7acdfd4921a0ccd /src | |
| parent | 8f173868dc7c262a028ec9f7926cfa51f225255f (diff) | |
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Rewritten util::split()
* src/util.h
* test/UtilTest.cc
Diffstat (limited to 'src')
| -rw-r--r-- | src/util.h | 39 |
1 files changed, 18 insertions, 21 deletions
@@ -338,30 +338,27 @@ OutputIterator split(const std::string& src, OutputIterator out, const std::string& delims, bool doStrip = false, bool allowEmpty = false) { - std::string::size_type p = 0; - while(1) { - std::string::size_type np = src.find_first_of(delims, p); - if(np == std::string::npos) { - std::string term = src.substr(p); - if(doStrip) { - term = util::strip(term); - } - if(allowEmpty || !term.empty()) { - *out = term; - ++out; - } - break; + std::string::const_iterator first = src.begin(); + std::string::const_iterator last = src.end(); + for(std::string::const_iterator i = first; i != last;) { + std::string::const_iterator j = i; + for(; j != last && + std::find(delims.begin(), delims.end(), *j) == delims.end(); ++j); + std::string t = doStrip?util::stripIter(i, j):std::string(i, j); + if(allowEmpty || !t.empty()) { + *out++ = t; } - std::string term = src.substr(p, np-p); - if(doStrip) { - term = util::strip(term); - } - p = np+1; - if(allowEmpty || !term.empty()) { - *out = term; - ++out; + i = j; + if(j != last) { + ++i; } } + if(allowEmpty && + (src.empty() || + std::find(delims.begin(), delims.end(), + src[src.size()-1]) != delims.end())) { + *out++ = A2STR::NIL; + } return out; } |
