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