A Journey 2 Eternity

Wild-Card Comparison

Posted on: May 13, 2009

Sometimes we have needed to compare a Wild-Card string in a given string. Here is a handy function for doing this:

int WildCmp(const TCHAR* wild, const TCHAR* string)
{
	const TCHAR *cp = NULL, *mp = NULL;

	while ((*string) && (*wild != ‘*’)) {
		if ((*wild != *string) && (*wild != ‘?’)) {
			return 0;
		}
		wild++;
		string++;
	}

	while (*string) {
		if (*wild == ‘*’) {
			if (!*++wild) {
				return 1;
			}

			mp = wild;
			cp = string+1;
		} else if ((*wild == *string) || (*wild == ‘?’)) {
			wild++;
			string++;
		} else {
			wild = mp;
			string = cp++;
		}
	}

	while (*wild == ‘*’) {
		wild++;
	}

	return !*wild;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Categories

May 2009
M T W T F S S
« Apr   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

Blog Stats

  • 27,176 hits
%d bloggers like this: