aboutsummaryrefslogtreecommitdiff
path: root/common/util.cpp
diff options
context:
space:
mode:
authorKari Salminen2008-02-08 04:11:20 +0000
committerKari Salminen2008-02-08 04:11:20 +0000
commitb6cad0f0ce9b266710383fceb69c9f4e3b0ffba2 (patch)
tree7959a6bd6614d26ec79a85b4be9316af4d210d94 /common/util.cpp
parente3852c92a77fef402bb37863a8430f76275c38bf (diff)
downloadscummvm-rg350-b6cad0f0ce9b266710383fceb69c9f4e3b0ffba2.tar.gz
scummvm-rg350-b6cad0f0ce9b266710383fceb69c9f4e3b0ffba2.tar.bz2
scummvm-rg350-b6cad0f0ce9b266710383fceb69c9f4e3b0ffba2.zip
Added a simple non-optimized StringTokenizer-class for tokenizing strings. Also added a contains(char)-function to the String-class because it was handy in implementing the StringTokenizer.
svn-id: r30828
Diffstat (limited to 'common/util.cpp')
-rw-r--r--common/util.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/common/util.cpp b/common/util.cpp
index 19f506cf6e..82a910f9a8 100644
--- a/common/util.cpp
+++ b/common/util.cpp
@@ -96,6 +96,33 @@ bool matchString(const char *str, const char *pat) {
}
}
+StringTokenizer::StringTokenizer(const String &str, const String &delimiters) : _str(str), _delimiters(delimiters) {
+ reset();
+}
+
+void StringTokenizer::reset() {
+ _tokenBegin = _tokenEnd = 0;
+}
+
+bool StringTokenizer::empty() const {
+ // Search for the next token's start (i.e. the next non-delimiter character)
+ for (uint i = _tokenEnd; i < _str.size(); i++) {
+ if (!_delimiters.contains(_str[i]))
+ return false; // Found a token so the tokenizer is not empty
+ }
+ // Didn't find any more tokens so the tokenizer is empty
+ return true;
+}
+
+String StringTokenizer::nextToken() {
+ // Seek to next token's start (i.e. jump over the delimiters before next token)
+ for (_tokenBegin = _tokenEnd; _tokenBegin < _str.size() && _delimiters.contains(_str[_tokenBegin]); _tokenBegin++);
+ // Seek to the token's end (i.e. jump over the non-delimiters)
+ for (_tokenEnd = _tokenBegin; _tokenEnd < _str.size() && !_delimiters.contains(_str[_tokenEnd]); _tokenEnd++);
+ // Return the found token
+ return String(_str.c_str() + _tokenBegin, _tokenEnd - _tokenBegin);
+}
+
//
// Print hexdump of the data passed in
//