changeset 414:77fd4f0327dc

Split th_str{case}match() into another file to reduce code duplication.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 09 Mar 2016 13:03:39 +0200
parents bb7120c11f8e
children 4b1b2e9d073f
files th_string.c th_strmatch.c
diffstat 2 files changed, 114 insertions(+), 207 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Wed Mar 09 13:03:05 2016 +0200
+++ b/th_string.c	Wed Mar 09 13:03:39 2016 +0200
@@ -931,217 +931,16 @@
  * wildcards ? and *. "?" matches any character and "*" matches
  * any number of characters.
  */
-BOOL th_strmatch(const char *haystack, const char *pattern)
-{
-    BOOL matched = TRUE, any = FALSE, end = FALSE;
-    const char *tmp = NULL;
-
-    // Check given pattern and string
-    if (haystack == NULL || pattern == NULL)
-        return FALSE;
-
-    // Start comparision
-    do {
-        matched = FALSE;
-        switch (*pattern)
-        {
-        case '?':
-            // Any single character matches
-            if (*haystack)
-            {
-                matched = TRUE;
-                pattern++;
-                haystack++;
-            }
-            break;
-
-        case '*':
-            matched = TRUE;
-            pattern++;
-            if (!*pattern)
-                end = TRUE;
-            any = TRUE;
-            tmp = pattern;
-            break;
-
-        case 0:
-            if (any)
-            {
-                if (*haystack)
-                    haystack++;
-                else
-                    end = TRUE;
-            }
-            else
-            {
-                if (*haystack)
-                {
-                    if (tmp)
-                    {
-                        any = TRUE;
-                        pattern = tmp;
-                    }
-                    else
-                        matched = FALSE;
-                }
-                else
-                    end = TRUE;
-            }
-            break;
-
-        default:
-            if (any)
-            {
-                if (*pattern == *haystack)
-                {
-                    any = FALSE;
-                    matched = TRUE;
-                }
-                else
-                {
-                    if (*haystack)
-                    {
-                        matched = TRUE;
-                        haystack++;
-                    }
-                }
-            }
-            else
-            {
-                if (*pattern == *haystack)
-                {
-                    matched = TRUE;
-                    if (*pattern)
-                        pattern++;
-                    if (*haystack)
-                        haystack++;
-                }
-                else
-                {
-                    if (tmp)
-                    {
-                        matched = TRUE;
-                        any = TRUE;
-                        pattern = tmp;
-                    }
-                }
-            }
-
-            if (!*haystack && !*pattern)
-                end = TRUE;
-            break;
-
-        }        // switch
-    } while (matched && !end);
-
-    return matched;
-}
+#define TH_STRMATCH_FUNC th_strmatch
+#define TH_STRMATCH_COLLATE(px) (px)
+#include "th_strmatch.c"
 
 
 /* Compare a string to a pattern. Case-INSENSITIVE version.
  */
-BOOL th_strcasematch(const char *haystack, const char *pattern)
-{
-    BOOL matched = TRUE, any = FALSE, end = FALSE;
-    const char *tmp = NULL;
-
-    // Check given pattern and string
-    if (haystack == NULL || pattern == NULL)
-        return FALSE;
-
-    // Start comparision
-    do {
-        switch (*pattern) {
-        case '?':
-            // Any single character matches
-            if (*haystack)
-            {
-                pattern++;
-                haystack++;
-            }
-            else
-                matched = FALSE;
-            break;
-
-        case '*':
-            pattern++;
-            if (!*pattern || *pattern == '?')
-                end = TRUE;
-            any = TRUE;
-            tmp = pattern;
-            break;
-
-        case 0:
-            if (any)
-            {
-                if (*haystack)
-                    haystack++;
-                else
-                    end = TRUE;
-            }
-            else
-            {
-                if (*haystack)
-                {
-                    if (tmp)
-                    {
-                        any = TRUE;
-                        pattern = tmp;
-                    }
-                    else
-                        matched = FALSE;
-                }
-                else
-                    end = TRUE;
-            }
-            break;
-
-        default:
-            if (any)
-            {
-                if (th_tolower(*pattern) == th_tolower(*haystack))
-                {
-                    any = FALSE;
-                }
-                else
-                {
-                    if (*haystack)
-                        haystack++;
-                    else
-                        matched = FALSE;
-                }
-            }
-            else
-            {
-                if (th_tolower(*pattern) == th_tolower(*haystack))
-                {
-                    if (*pattern)
-                        pattern++;
-                    if (*haystack)
-                        haystack++;
-                }
-                else
-                {
-                    if (tmp)
-                    {
-                        any = TRUE;
-                        pattern = tmp;
-                    }
-                    else
-                        matched = FALSE;
-                }
-            }
-
-            if (!*haystack && !*pattern)
-                end = TRUE;
-
-            break;
-
-        }        // switch
-    } while (matched && !end);
-
-    return matched;
-}
+#define TH_STRMATCH_FUNC th_strcasematch
+#define TH_STRMATCH_COLLATE(px) th_tolower(px)
+#include "th_strmatch.c"
 
 
 int th_get_hex_triplet(const char *str)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/th_strmatch.c	Wed Mar 09 13:03:39 2016 +0200
@@ -0,0 +1,108 @@
+/*
+ * String glob match implementation
+ * Programmed and designed by Matti 'ccr' Hamalainen
+ * (C) Copyright 2002-2016 Tecnic Software productions (TNSP)
+ *
+ * Please read file 'COPYING' for information on license and distribution.
+ */
+
+BOOL TH_STRMATCH_FUNC (const char *haystack, const char *pattern)
+{
+    BOOL matched = TRUE, any = FALSE, end = FALSE;
+    const char *tmp = NULL;
+
+    // Check given pattern and string
+    if (haystack == NULL || pattern == NULL)
+        return FALSE;
+
+    // Start comparision
+    while (matched && !end)
+    switch (*pattern)
+    {
+    case '?':
+        // Any single character matches
+        if (*haystack)
+        {
+            pattern++;
+            haystack++;
+        }
+        else
+            matched = FALSE;
+        break;
+
+    case '*':
+        pattern++;
+        if (!*pattern || *pattern == '?')
+            end = TRUE;
+        any = TRUE;
+        tmp = pattern;
+        break;
+
+    case 0:
+        if (any)
+        {
+            if (*haystack)
+                haystack++;
+            else
+                end = TRUE;
+        }
+        else
+        if (*haystack)
+        {
+            if (tmp)
+            {
+                any = TRUE;
+                pattern = tmp;
+            }
+            else
+                matched = FALSE;
+        }
+        else
+            end = TRUE;
+        break;
+
+    default:
+        if (any)
+        {
+            if (TH_STRMATCH_COLLATE(*pattern) == TH_STRMATCH_COLLATE(*haystack))
+            {
+                any = FALSE;
+            }
+            else
+            if (*haystack)
+                haystack++;
+            else
+                matched = FALSE;
+        }
+        else
+        {
+            if (TH_STRMATCH_COLLATE(*pattern) == TH_STRMATCH_COLLATE(*haystack))
+            {
+                if (*pattern)
+                    pattern++;
+                if (*haystack)
+                    haystack++;
+            }
+            else
+            if (tmp)
+            {
+                any = TRUE;
+                pattern = tmp;
+            }
+            else
+                matched = FALSE;
+        }
+
+        if (!*haystack && !*pattern)
+            end = TRUE;
+
+        break;
+    }
+
+    return matched;
+}
+
+
+#undef TH_STRMATCH_FUNC
+#undef TH_STRMATCH_COLLATE
+