]> mj.ucw.cz Git - libucw.git/commitdiff
New pattern matching code -- simplified and debugged.
authorMartin Mares <mj@ucw.cz>
Tue, 2 Dec 1997 09:56:19 +0000 (09:56 +0000)
committerMartin Mares <mj@ucw.cz>
Tue, 2 Dec 1997 09:56:19 +0000 (09:56 +0000)
lib/patimatch.c
lib/patmatch.c
lib/patmatch.h [new file with mode: 0644]

index 8a0c8851f80b8798750092d31e8bff4c92cbbd7a..fa651b06dda3acfb52ba9ed6cddb3130e811f9cb 100644 (file)
 #include "lib.h"
 #include "string.h"
 
-int
-match_pattern_nocase(byte *p, byte *s)
-{
-  while (*p)
-    {
-      if (*p == '?' && *s)
-       p++, s++;
-      else if (*p == '*')
-       {
-         int z = p[1];
+#define Convert(x) Cupcase(x)
+#define MATCH_FUNC_NAME match_pattern_nocase
 
-         if (!z)
-           return 1;
-         while (s = strchr(s, z))
-           {
-             if (match_pattern_nocase(p+1, s))
-               return 1;
-             s++;
-           }
-         return 0;
-       }
-      else
-       {
-         if (*p == '\\' && p[1])
-           p++;
-         if (Cupcase(*p++) != Cupcase(*s++))
-           return 0;
-       }
-    }
-  return !*s;
-}
+#include "patmatch.h"
index c1d60932cd44544252f1777adc04a06ae3310e93..53a0710a2a46b4ae357cc1861bfa1c64fe1416bd 100644 (file)
@@ -9,34 +9,7 @@
 
 #include "lib.h"
 
-int
-match_pattern(byte *p, byte *s)
-{
-  while (*p)
-    {
-      if (*p == '?' && *s)
-       p++, s++;
-      else if (*p == '*')
-       {
-         int z = p[1];
+#define Convert(x) (x)
+#define MATCH_FUNC_NAME match_pattern
 
-         if (!z)
-           return 1;
-         while (s = strchr(s, z))
-           {
-             if (match_pattern(p+1, s))
-               return 1;
-             s++;
-           }
-         return 0;
-       }
-      else
-       {
-         if (*p == '\\' && p[1])
-           p++;
-         if (*p++ != *s++)
-           return 0;
-       }
-    }
-  return !*s;
-}
+#include "patmatch.h"
diff --git a/lib/patmatch.h b/lib/patmatch.h
new file mode 100644 (file)
index 0000000..6d51216
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ *     Sherlock Library -- Generic Shell-Like Pattern Matching (currently only '?' and '*')
+ *
+ *     (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ */
+
+int
+MATCH_FUNC_NAME(byte *p, byte *s)
+{
+  while (*p)
+    {
+      if (*p == '?' && *s)
+       p++, s++;
+      else if (*p == '*')
+       {
+         int z = p[1];
+
+         if (!z)
+           return 1;
+         if (z == '\\' && p[2])
+           z = p[2];
+         z = Convert(z);
+         while (*s)
+           {
+             while (*s && Convert(*s) != z)
+               s++;
+             if (*s && match_pattern(p+1, s))
+               return 1;
+             s++;
+           }
+         return 0;
+       }
+      else
+       {
+         if (*p == '\\' && p[1])
+           p++;
+         if (Convert(*p++) != Convert(*s++))
+           return 0;
+       }
+    }
+  return !*s;
+}