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

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Fri Sep 5 22:07:35 CEST 2008


Revision: 34364
          http://scummvm.svn.sourceforge.net/scummvm/?rev=34364&view=rev
Author:   fingolfin
Date:     2008-09-05 20:07:34 +0000 (Fri, 05 Sep 2008)

Log Message:
-----------
Moved matchString from util.* to str.*; added new String::matchString method; fixed matchString doxygen comment (it confused pattern & string); added unit tests for matchString

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

Modified: scummvm/trunk/common/str.cpp
===================================================================
--- scummvm/trunk/common/str.cpp	2008-09-05 19:03:30 UTC (rev 34363)
+++ scummvm/trunk/common/str.cpp	2008-09-05 20:07:34 UTC (rev 34364)
@@ -311,6 +311,14 @@
 	return strchr(c_str(), x) != NULL;
 }
 
+bool String::matchString(const char *pat) const {
+	return Common::matchString(c_str(), pat);
+}
+
+bool String::matchString(const String &pat) const {
+	return Common::matchString(c_str(), pat.c_str());
+}
+
 void String::deleteLastChar() {
 	deleteChar(_size - 1);
 }
@@ -587,4 +595,40 @@
 	return result;
 }
 
+bool matchString(const char *str, const char *pat) {
+	assert(str);
+	assert(pat);
+
+	const char *p = 0;
+	const char *q = 0;
+
+	for (;;) {
+		switch (*pat) {
+		case '*':
+			p = ++pat;
+			q = str;
+			break;
+
+		default:
+			if (*pat != *str) {
+				if (p) {
+					pat = p;
+					str = ++q;
+					if (!*str)
+						return !*pat;
+					break;
+				}
+				else
+					return false;
+			}
+			// fallthrough
+		case '?':
+			if (!*str)
+				return !*pat;
+			pat++;
+			str++;
+		}
+	}
+}
+
 }	// End of namespace Common

Modified: scummvm/trunk/common/str.h
===================================================================
--- scummvm/trunk/common/str.h	2008-09-05 19:03:30 UTC (rev 34363)
+++ scummvm/trunk/common/str.h	2008-09-05 20:07:34 UTC (rev 34364)
@@ -149,6 +149,30 @@
 	bool contains(const char *x) const;
 	bool contains(char x) const;
 
+	/**
+	 * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
+	 * Taken from exult/files/listfiles.cc
+	 *
+	 * Token meaning:
+	 *		"*": any character, any amount of times.
+	 *		"?": any character, only once.
+	 *
+	 * Example strings/patterns:
+	 *		String: monkey.s01	 Pattern: monkey.s??	=> true
+	 *		String: monkey.s101	 Pattern: monkey.s??	=> false
+	 *		String: monkey.s99	 Pattern: monkey.s?1	=> false
+	 *		String: monkey.s101	 Pattern: monkey.s*		=> true
+	 *		String: monkey.s99	 Pattern: monkey.s*1	=> false
+	 *
+	 * @param str Text to be matched against the given pattern.
+	 * @param pat Glob pattern.
+	 *
+	 * @return true if str matches the pattern, false otherwise.
+	 */
+	bool matchString(const char *pat) const;
+	bool matchString(const String &pat) const;
+
+
 	inline const char *c_str() const		{ return _str; }
 	inline uint size() const				{ return _size; }
 
@@ -172,11 +196,19 @@
 	/** Set character c at position p. */
 	void insertChar(char c, uint32 p);
 
+	/** Clears the string, making it empty. */
 	void clear();
 
+	/** Convert all characters in the string to lowercase. */
 	void toLowercase();
+
+	/** Convert all characters in the string to uppercase. */
 	void toUppercase();
 	
+	/**
+	 * Removes trailing and leading whitespaces. Uses isspace() to decide
+	 * what is whitespace and what not.
+	 */
 	void trim();
 
 	uint hash() const;
@@ -257,6 +289,27 @@
 Common::String normalizePath(const Common::String &path, const char sep);
 
 
+/**
+ * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
+ * Taken from exult/files/listfiles.cc
+ *
+ * Token meaning:
+ *		"*": any character, any amount of times.
+ *		"?": any character, only once.
+ *
+ * Example strings/patterns:
+ *		String: monkey.s01	 Pattern: monkey.s??	=> true
+ *		String: monkey.s101	 Pattern: monkey.s??	=> false
+ *		String: monkey.s99	 Pattern: monkey.s?1	=> false
+ *		String: monkey.s101	 Pattern: monkey.s*		=> true
+ *		String: monkey.s99	 Pattern: monkey.s*1	=> false
+ *
+ * @param str Text to be matched against the given pattern.
+ * @param pat Glob pattern.
+ *
+ * @return true if str matches the pattern, false otherwise.
+ */
+bool matchString(const char *str, const char *pat);
 
 
 class StringList : public Array<String> {

Modified: scummvm/trunk/common/util.cpp
===================================================================
--- scummvm/trunk/common/util.cpp	2008-09-05 19:03:30 UTC (rev 34363)
+++ scummvm/trunk/common/util.cpp	2008-09-05 20:07:34 UTC (rev 34364)
@@ -63,39 +63,6 @@
 
 namespace Common {
 
-bool matchString(const char *str, const char *pat) {
-	const char *p = 0;
-	const char *q = 0;
-
-	for (;;) {
-		switch (*pat) {
-		case '*':
-			p = ++pat;
-			q = str;
-			break;
-
-		default:
-			if (*pat != *str) {
-				if (p) {
-					pat = p;
-					str = ++q;
-					if (!*str)
-						return !*pat;
-					break;
-				}
-				else
-					return false;
-			}
-			// fallthrough
-		case '?':
-			if (!*str)
-				return !*pat;
-			pat++;
-			str++;
-		}
-	}
-}
-
 StringTokenizer::StringTokenizer(const String &str, const String &delimiters) : _str(str), _delimiters(delimiters) {
 	reset();
 }

Modified: scummvm/trunk/common/util.h
===================================================================
--- scummvm/trunk/common/util.h	2008-09-05 19:03:30 UTC (rev 34363)
+++ scummvm/trunk/common/util.h	2008-09-05 20:07:34 UTC (rev 34364)
@@ -53,28 +53,6 @@
 namespace Common {
 
 /**
- * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
- * Taken from exult/files/listfiles.cc
- *
- * Token meaning:
- *		"*": any character, any amount of times.
- *		"?": any character, only once.
- *
- * Example strings/patterns:
- *		String: monkey.s??	 Pattern: monkey.s01	=> true
- *		String: monkey.s??	 Pattern: monkey.s101	=> false
- *		String: monkey.s?1	 Pattern: monkey.s99	=> false
- *		String: monkey.s*	 Pattern: monkey.s101	=> true
- *		String: monkey.s*1	 Pattern: monkey.s99	=> false
- *
- * @param str Text to be matched against the given pattern.
- * @param pat Glob pattern.
- *
- * @return true if str matches the pattern, false otherwise.
- */
-bool matchString(const char *str, const char *pat);
-
-/**
  * A simple non-optimized string tokenizer.
  *
  * Example of use:

Modified: scummvm/trunk/test/common/str.h
===================================================================
--- scummvm/trunk/test/common/str.h	2008-09-05 19:03:30 UTC (rev 34363)
+++ scummvm/trunk/test/common/str.h	2008-09-05 20:07:34 UTC (rev 34364)
@@ -189,4 +189,22 @@
 		TS_ASSERT(Common::normalizePath("foo//./bar//", '/') == "foo/bar");
 		TS_ASSERT(Common::normalizePath("foo//.bar//", '/') == "foo/.bar");
 	}
+
+	void test_matchString(void) {
+		TS_ASSERT( Common::matchString("monkey.s01",  "monkey.s??"));
+		TS_ASSERT( Common::matchString("monkey.s99",  "monkey.s??"));
+		TS_ASSERT(!Common::matchString("monkey.s101", "monkey.s??"));
+
+		TS_ASSERT( Common::matchString("monkey.s01",  "monkey.s?1"));
+		TS_ASSERT(!Common::matchString("monkey.s99",  "monkey.s?1"));
+		TS_ASSERT(!Common::matchString("monkey.s101", "monkey.s?1"));
+
+		TS_ASSERT( Common::matchString("monkey.s01",  "monkey.s*"));
+		TS_ASSERT( Common::matchString("monkey.s99",  "monkey.s*"));
+		TS_ASSERT( Common::matchString("monkey.s101", "monkey.s*"));
+
+		TS_ASSERT( Common::matchString("monkey.s01",  "monkey.s*1"));
+		TS_ASSERT(!Common::matchString("monkey.s99",  "monkey.s*1"));
+		TS_ASSERT( Common::matchString("monkey.s101", "monkey.s*1"));
+	}
 };


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