summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2010-10-10 06:58:31 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2010-10-10 06:58:31 +0000
commit0abd4a2f7b34b35d9c01b2fcecfdb241eeb4db32 (patch)
tree541e098d186675da1e971791c7acdfd4921a0ccd /src/util.h
parent8f173868dc7c262a028ec9f7926cfa51f225255f (diff)
2010-10-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Rewritten util::split() * src/util.h * test/UtilTest.cc
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h39
1 files changed, 18 insertions, 21 deletions
diff --git a/src/util.h b/src/util.h
index 5efde239..12f37f1d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -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;
}