[Scummvm-cvs-logs] SF.net SVN: scummvm: [30828] scummvm/trunk/common

buddha_ at users.sourceforge.net buddha_ at users.sourceforge.net
Fri Feb 8 05:11:20 CET 2008


Revision: 30828
          http://scummvm.svn.sourceforge.net/scummvm/?rev=30828&view=rev
Author:   buddha_
Date:     2008-02-07 20:11:20 -0800 (Thu, 07 Feb 2008)

Log Message:
-----------
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.

Modified Paths:
--------------
    scummvm/trunk/common/str.cpp
    scummvm/trunk/common/str.h
    scummvm/trunk/common/util.cpp
    scummvm/trunk/common/util.h

Modified: scummvm/trunk/common/str.cpp
===================================================================
--- scummvm/trunk/common/str.cpp	2008-02-08 01:45:46 UTC (rev 30827)
+++ scummvm/trunk/common/str.cpp	2008-02-08 04:11:20 UTC (rev 30828)
@@ -226,6 +226,10 @@
 	return strstr(c_str(), x) != NULL;
 }
 
+bool String::contains(char x) const {
+	return strchr(c_str(), x) != NULL;
+}
+
 void String::deleteLastChar() {
 	deleteChar(_len - 1);
 }

Modified: scummvm/trunk/common/str.h
===================================================================
--- scummvm/trunk/common/str.h	2008-02-08 01:45:46 UTC (rev 30827)
+++ scummvm/trunk/common/str.h	2008-02-08 04:11:20 UTC (rev 30828)
@@ -133,6 +133,7 @@
 	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; }

Modified: scummvm/trunk/common/util.cpp
===================================================================
--- scummvm/trunk/common/util.cpp	2008-02-08 01:45:46 UTC (rev 30827)
+++ scummvm/trunk/common/util.cpp	2008-02-08 04:11:20 UTC (rev 30828)
@@ -96,6 +96,33 @@
 	}
 }
 
+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
 //

Modified: scummvm/trunk/common/util.h
===================================================================
--- scummvm/trunk/common/util.h	2008-02-08 01:45:46 UTC (rev 30827)
+++ scummvm/trunk/common/util.h	2008-02-08 04:11:20 UTC (rev 30828)
@@ -75,6 +75,32 @@
 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


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list