diff options
author | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2011-11-11 17:00:30 +0400 |
---|---|---|
committer | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2011-11-11 17:00:30 +0400 |
commit | 888c47d9211a921d769419b9b62f23c161cf6930 (patch) | |
tree | 00d917c14e1c1195508cb080377b9b958d146e12 /mert/Util.cpp | |
parent | c2121695c2a7be7538745759f2f86ec280baa54d (diff) |
Fix splitting strings from a string.
Diffstat (limited to 'mert/Util.cpp')
-rw-r--r-- | mert/Util.cpp | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/mert/Util.cpp b/mert/Util.cpp index ffc8a4c7e..739542378 100644 --- a/mert/Util.cpp +++ b/mert/Util.cpp @@ -18,6 +18,15 @@ Timer g_timer; int verbose=0; +namespace { + +bool FindDelimiter(const std::string &str, const std::string &delim, size_t *pos) +{ + *pos = str.find(delim); + return *pos != std::string::npos ? true : false; +} +} // namespace + int verboselevel() { return verbose; @@ -29,21 +38,22 @@ int setverboselevel(int v) return verbose; } -int getNextPound(std::string &theString, std::string &substring, const std::string delimiter) +size_t getNextPound(std::string &theString, std::string &substring, const std::string delimiter) { - unsigned int pos = 0; + size_t pos = 0; //skip all occurrences of delimiter - while ( pos == 0 ) { - if ((pos = theString.find(delimiter)) != std::string::npos) { + while (pos == 0) { + // if ((pos = theString.find(delimiter)) != std::string::npos) { + if (FindDelimiter(theString, delimiter, &pos)) { substring.assign(theString, 0, pos); - theString.erase(0,pos + delimiter.size()); + theString.erase(0, pos + delimiter.size()); } else { substring.assign(theString); theString.assign(""); } } - return (pos); + return pos; }; void split(const std::string &s, char delim, std::vector<std::string> &elems) { @@ -54,6 +64,16 @@ void split(const std::string &s, char delim, std::vector<std::string> &elems) { } } +void Tokenize(const char *str, const char delim, + std::vector<std::string> *res) { + while (1) { + const char *begin = str; + while (*str != delim && *str) str++; + res->push_back(std::string(begin, str)); + if (*str++ == 0) break; + } +} + inputfilestream::inputfilestream(const std::string &filePath) : std::istream(0), m_streambuf(0) |