aboutsummaryrefslogtreecommitdiff
path: root/common
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
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')
-rw-r--r--common/str.cpp4
-rw-r--r--common/str.h1
-rw-r--r--common/util.cpp27
-rw-r--r--common/util.h26
4 files changed, 58 insertions, 0 deletions
diff --git a/common/str.cpp b/common/str.cpp
index 9d40fe8902..9e94d240d7 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -226,6 +226,10 @@ bool String::contains(const char *x) const {
return strstr(c_str(), x) != NULL;
}
+bool String::contains(char x) const {
+ return strchr(c_str(), x) != NULL;
+}
+
void String::deleteLastChar() {
deleteChar(_len - 1);
}
diff --git a/common/str.h b/common/str.h
index d42320f2d9..fc652d517a 100644
--- a/common/str.h
+++ b/common/str.h
@@ -133,6 +133,7 @@ public:
bool hasPrefix(const char *x) const;
bool contains(const char *x) const;
+ bool contains(char x) const;
inline const char *c_str() const { return _str; }
inline uint size() const { return _len; }
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
//
diff --git a/common/util.h b/common/util.h
index 8e5fd8d1ed..e7a71ff42c 100644
--- a/common/util.h
+++ b/common/util.h
@@ -75,6 +75,32 @@ namespace Common {
bool matchString(const char *str, const char *pat);
/**
+ * A simple non-optimized string tokenizer.
+ *
+ * Example of use:
+ * StringTokenizer("Now, this is a test!", " ,!") gives tokens "Now", "this", "is", "a" and "test" using nextToken().
+ */
+class StringTokenizer {
+public:
+ /**
+ * Creates a StringTokenizer.
+ * @param str The string to be tokenized.
+ * @param delimiters String containing all the delimiter characters (i.e. the characters to be ignored).
+ * @note Uses space, horizontal tab, carriage return, newline, form feed and vertical tab as delimiters by default.
+ */
+ StringTokenizer(const String &str, const String &delimiters = " \t\r\n\f\v");
+ void reset(); //!< Resets the tokenizer to its initial state
+ bool empty() const; //!< Returns true if there are no more tokens left in the string, false otherwise
+ String nextToken(); //!< Returns the next token from the string (Or an empty string if there are no more tokens)
+
+private:
+ const String _str; //!< The string to be tokenized
+ const String _delimiters; //!< String containing all the delimiter characters
+ uint _tokenBegin; //!< Latest found token's begin (Valid after a call to nextToken(), zero otherwise)
+ uint _tokenEnd; //!< Latest found token's end (Valid after a call to nextToken(), zero otherwise)
+};
+
+/**
* Print a hexdump of the data passed in. The number of bytes per line is
* customizable.
* @param data the data to be dumped