All pastes #456454 Raw Edit

Unnamed

public text v1 · immutable
#456454 ·published 2007-04-24 20:40 UTC
rendered paste body
using System;
using System.IO;
using System.Diagnostics;

class glob {
	static public void Main (string[] args)
	{
		if (args.Length < 2)
			return;

		string pattern = args [0];
		string str = args [1];
		Console.WriteLine ("Matching '{0}' against '{1}'", pattern, str);

		Stopwatch sw = new Stopwatch ();
		bool b;

		sw.Start ();
		b = TestWildCard (pattern, str);
		sw.Stop ();
		Console.WriteLine ("WildcardEquals Returned {0} in {1}", b, sw.Elapsed);

		sw.Reset ();

		sw.Start ();
		b = TestGlobMatch (pattern, str);
		sw.Stop ();
		Console.WriteLine ("GlobMatch      Returned {0} in {1}", b, sw.Elapsed);
	}

	static int test_iter = 5000;

	static bool TestWildCard (string pattern, string str)
	{
		bool b = true;
		for (int i = 0; i < test_iter; ++i)
			b = GlobMatch (pattern, str);
			//b = WildcardEquals (pattern, 0, str, 0);
		return b;
	}

	static bool TestGlobMatch (string pattern, string str)
	{
		bool b = true;
		for (int i = 0; i < test_iter; ++i)
			b = GlobMatch (pattern, str);
		return b;
	}

	public const char WILDCARD_STRING = '*';
	public const char WILDCARD_CHAR = '?';
		
	public static bool WildcardEquals(System.String pattern, int patternIdx, System.String string_Renamed, int stringIdx)
	{
		return true;
		if (pattern == null || string_Renamed == null)
			return false;
		else if (pattern == "*")
			return true;
		else if (string_Renamed == String.Empty && pattern != String.Empty)
			return false;

		int inj = pattern.IndexOf ('*');
		if (inj == -1)
			return pattern == string_Renamed;

		Console.WriteLine ("foo");
		int p = patternIdx;
		
		for (int s = stringIdx; ; ++p, ++s)
		{
			// End of string yet?
			bool sEnd = (s >= string_Renamed.Length);
			// End of pattern yet?
			bool pEnd = (p >= pattern.Length);
			
			// If we're looking at the end of the string...
			if (sEnd)
			{
				// Assume the only thing left on the pattern is/are wildcards
				bool justWildcardsLeft = true;
				
				// Current wildcard position
				int wildcardSearchPos = p;
				// While we haven't found the end of the pattern,
				// and haven't encountered any non-wildcard characters
				while (wildcardSearchPos < pattern.Length && justWildcardsLeft)
				{
					// Check the character at the current position
					char wildchar = pattern[wildcardSearchPos];
					
					// If it's not a wildcard character, then there is more
					// pattern information after this/these wildcards.
					if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
					{
						justWildcardsLeft = false;
					}
					else
					{
						// to prevent "cat" matches "ca??"
						if (wildchar == WILDCARD_CHAR)
						{
							return false;
						}
						
						// Look at the next character
						wildcardSearchPos++;
					}
				}
				
				// This was a prefix wildcard search, and we've matched, so
				// return true.
				if (justWildcardsLeft)
				{
					return true;
				}
			}
			
			// If we've gone past the end of the string, or the pattern,
			// return false.
			if (sEnd || pEnd)
			{
				break;
			}
			
			// Match a single character, so continue.
			if (pattern[p] == WILDCARD_CHAR)
			{
				continue;
			}
			
			//
			if (pattern[p] == WILDCARD_STRING)
			{
				// Look at the character beyond the '*'.
				++p;
				// Examine the string, starting at the last character.
				for (int i = string_Renamed.Length; i >= s; --i)
				{
					if (WildcardEquals(pattern, p, string_Renamed, i))
					{
						return true;
					}
				}
				break;
			}
			if (pattern[p] != string_Renamed[s])
			{
				break;
			}
		}
		return false;
	}

	static public bool GlobMatch (string pattern, string str)
	{
		return true;
		if (pattern == null || str == null)
			return false;
		else if (pattern == "*")
			return true;
		//else if (pattern.StartsWith ("**"))
		//	return GlobMatch (pattern.Substring (1), str);
		else if (str == String.Empty && pattern != String.Empty)
			return false;

		int i = pattern.IndexOf ('*');
		if (i == -1)
			return pattern == str;

		Console.WriteLine ("foo");
		if (i > 0 && i < str.Length)
			return pattern.Substring (0, i) == str.Substring (0, i)
				&& GlobMatch (pattern.Substring (i), str.Substring (i));
		else if (i == 0)
			return GlobMatch (pattern.Substring (1), str.Substring (1))
				|| GlobMatch (pattern.Substring (1), str)
				|| GlobMatch (pattern, str.Substring (1));
		
		return false;
	}
}