Mercurial > hg > th-libs
comparison th_strglob.c @ 606:5ec903f366b5
Rename th_strmatch.c to th_strglob.c
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 14 Jan 2020 12:53:23 +0200 |
parents | th_strmatch.c@1a33088e7fa9 |
children | b87395754c8d |
comparison
equal
deleted
inserted
replaced
600:492633200ddd | 606:5ec903f366b5 |
---|---|
1 /* | |
2 * String glob match implementation | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2002-2020 Tecnic Software productions (TNSP) | |
5 * | |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 | |
9 BOOL TH_STRGLOB_FUNC (const char *haystack, const char *pattern) | |
10 { | |
11 BOOL matched = TRUE, any = FALSE, end = FALSE; | |
12 const char *tmp = NULL; | |
13 | |
14 // Check given pattern and string | |
15 if (haystack == NULL || pattern == NULL) | |
16 return FALSE; | |
17 | |
18 // Start comparision | |
19 while (matched && !end) | |
20 switch (*pattern) | |
21 { | |
22 case '?': | |
23 // Any single character matches | |
24 if (*haystack) | |
25 { | |
26 pattern++; | |
27 haystack++; | |
28 } | |
29 else | |
30 matched = FALSE; | |
31 break; | |
32 | |
33 case '*': | |
34 pattern++; | |
35 if (!*pattern || *pattern == '?') | |
36 end = TRUE; | |
37 any = TRUE; | |
38 tmp = pattern; | |
39 break; | |
40 | |
41 case 0: | |
42 if (any) | |
43 { | |
44 if (*haystack) | |
45 haystack++; | |
46 else | |
47 end = TRUE; | |
48 } | |
49 else | |
50 if (*haystack) | |
51 { | |
52 if (tmp) | |
53 { | |
54 any = TRUE; | |
55 pattern = tmp; | |
56 } | |
57 else | |
58 matched = FALSE; | |
59 } | |
60 else | |
61 end = TRUE; | |
62 break; | |
63 | |
64 default: | |
65 if (any) | |
66 { | |
67 if (TH_STRGLOB_COLLATE(*pattern) == TH_STRGLOB_COLLATE(*haystack)) | |
68 { | |
69 any = FALSE; | |
70 } | |
71 else | |
72 if (*haystack) | |
73 haystack++; | |
74 else | |
75 matched = FALSE; | |
76 } | |
77 else | |
78 { | |
79 if (TH_STRGLOB_COLLATE(*pattern) == TH_STRGLOB_COLLATE(*haystack)) | |
80 { | |
81 if (*pattern) | |
82 pattern++; | |
83 if (*haystack) | |
84 haystack++; | |
85 } | |
86 else | |
87 if (tmp) | |
88 { | |
89 any = TRUE; | |
90 pattern = tmp; | |
91 } | |
92 else | |
93 matched = FALSE; | |
94 } | |
95 | |
96 if (!*haystack && !*pattern) | |
97 end = TRUE; | |
98 | |
99 break; | |
100 } | |
101 | |
102 return matched; | |
103 } | |
104 | |
105 | |
106 #undef TH_STRGLOB_FUNC | |
107 #undef TH_STRGLOB_COLLATE | |
108 |