Mercurial > hg > th-libs
annotate th_strglob.c @ 687:5a7254b78614
Add th_strchr().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 06 Mar 2020 14:26:45 +0200 |
parents | b87395754c8d |
children | 4ca6a3b30fe8 |
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 |
553 | 4 * (C) Copyright 2002-2020 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 |
593
1a33088e7fa9
Rename some internal preprocessor macros.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
9 BOOL TH_STRGLOB_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 { |
615 | 66 BOOL equals = TH_STRGLOB_COLLATE(*pattern) == TH_STRGLOB_COLLATE(*haystack); |
67 if (any) | |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 { |
615 | 69 if (equals) |
70 { | |
71 any = FALSE; | |
72 } | |
73 else | |
74 if (*haystack) | |
75 haystack++; | |
76 else | |
77 matched = FALSE; | |
414
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 else |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 { |
615 | 81 if (equals) |
82 { | |
83 if (*pattern) | |
84 pattern++; | |
85 if (*haystack) | |
86 haystack++; | |
87 } | |
88 else | |
89 if (tmp) | |
90 { | |
91 any = TRUE; | |
92 pattern = tmp; | |
93 } | |
94 else | |
95 matched = FALSE; | |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 } |
615 | 97 |
98 if (!*haystack && !*pattern) | |
99 end = TRUE; | |
414
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 break; |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 } |
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 return matched; |
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 |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 |
593
1a33088e7fa9
Rename some internal preprocessor macros.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
108 #undef TH_STRGLOB_FUNC |
1a33088e7fa9
Rename some internal preprocessor macros.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
109 #undef TH_STRGLOB_COLLATE |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 |