src/lib/wildcmp.c 1.6
1.6 2009/11/20 08:08:26 kent
Adding sqlMatchLike
Index: src/lib/wildcmp.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/wildcmp.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -b -B -U 4 -r1.5 -r1.6
--- src/lib/wildcmp.c 6 May 2003 07:33:44 -0000 1.5
+++ src/lib/wildcmp.c 20 Nov 2009 08:08:26 -0000 1.6
@@ -6,9 +6,9 @@
#include "common.h"
static char const rcsid[] = "$Id$";
-static int subMatch(char *str, char *wild)
+static int subMatch(char *str, char *wild, char single, char multi)
/* Returns number of characters that match between str and wild up
* to the next wildcard in wild (or up to end of string.). */
{
int len = 0;
@@ -17,33 +17,41 @@
{
if(toupper(*str++) != toupper(*wild++) )
return(0);
++len;
- switch(*wild)
- {
- case 0:
- case '?':
- case '*':
- return(len);
+ char c = *wild;
+ if (c == 0 || c == single || c == multi)
+ return len;
}
+}
+
+boolean anyWild(char *string)
+/* Return TRUE if any wild card characters in string. */
+{
+char c;
+while ((c = *string++) != 0)
+ {
+ if (c == '?' || c == '*')
+ return TRUE;
}
+return FALSE;
}
-boolean wildMatch(char *wildCard, char *string)
+static boolean globMatch(char *wildCard, char *string, char single, char multi)
/* does a case sensitive wild card match with a string.
* * matches any string or no character.
* ? matches any single character.
* anything else etc must match the character exactly. */
{
boolean matchStar = 0;
int starMatchSize;
+char c;
for(;;)
{
NEXT_WILD:
- switch(*wildCard)
- {
- case 0: /* end of wildcard */
+ c = *wildCard;
+ if (c == 0)
{
if(matchStar)
{
while(*string++)
@@ -54,19 +62,19 @@
return FALSE;
else
return TRUE;
}
- case '*':
+ else if (c == multi)
+ {
matchStar = TRUE;
- break;
- case '?': /* anything will do */
+ }
+ else if (c == single)
{
if(*string == 0)
return FALSE; /* out of string, no match for ? */
++string;
- break;
}
- default:
+ else
{
if(matchStar)
{
for(;;)
@@ -75,9 +83,9 @@
return FALSE;
/* note matchStar is re-used here for substring
* after star match length */
- if((starMatchSize = subMatch(string,wildCard)) != 0)
+ if((starMatchSize = subMatch(string,wildCard,single,multi)) != 0)
{
string += starMatchSize;
wildCard += starMatchSize;
matchStar = FALSE;
@@ -90,10 +98,20 @@
/* default: they must be equal or no match */
if(toupper(*string) != toupper(*wildCard))
return FALSE;
++string;
- break;
- }
}
++wildCard;
}
}
+
+boolean wildMatch(char *wildCard, char *string)
+/* Match using * and ? wildcards. */
+{
+return globMatch(wildCard, string, '?', '*');
+}
+
+boolean sqlMatchLike(char *wildCard, char *string)
+/* Match using % and _ wildcards. */
+{
+return globMatch(wildCard, string, '_', '%');
+}