Mercurial > hg > th-libs
annotate th_strglob.c @ 789:d61d3eb29053 default tip
Bump copyright.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 08 Mar 2024 15:26:24 +0200 |
parents | 600a3c08747f |
children |
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 |
726 | 4 * (C) Copyright 2002-2022 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 |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
9 bool TH_STRGLOB_FUNC (const char *haystack, const char *pattern, const bool handle_escapes) |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
10 #ifndef TH_STRGLOB_IMPL |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
11 ; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
12 #else |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 { |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
14 bool matched = true, end = false, any = false, escaped = false; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
15 const char *save = NULL; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 // 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
|
18 if (haystack == NULL || pattern == NULL) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
19 return false; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 // Start comparision |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
22 do |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 { |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
24 const bool esc_ok = !handle_escapes || !escaped; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
26 if (!*pattern) |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 { |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
28 // End of pattern |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
29 if (any) |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
30 { |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
31 if (*haystack) |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
32 haystack++; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
33 else |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
34 end = true; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
35 } |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
36 else |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 if (*haystack) |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
38 { |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
39 if (save) |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
40 { |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
41 any = true; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
42 pattern = save; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
43 } |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
44 else |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
45 matched = false; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
46 } |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 else |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
48 end = true; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 } |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 else |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
51 if (*pattern == '?' && esc_ok) |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 { |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
53 // Any single character matches |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
54 if (*haystack) |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 { |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
56 pattern++; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
57 haystack++; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 } |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 else |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
60 matched = false; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 } |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 else |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
63 if (*pattern == '*' && esc_ok) |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
64 { |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
65 // None or more any characters match |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
66 pattern++; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
67 if (!*pattern || *pattern == '?') |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
68 end = true; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
70 any = true; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
71 save = pattern; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
72 escaped = false; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
73 } |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
74 else |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
75 if (*pattern == '\\' && handle_escapes && !escaped) |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
76 { |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
77 pattern++; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
78 escaped = true; |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
79 } |
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
80 else |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 { |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
82 bool equals = TH_STRGLOB_COLLATE(*pattern) == TH_STRGLOB_COLLATE(*haystack); |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
83 escaped = false; |
615 | 84 if (any) |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 { |
615 | 86 if (equals) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
87 any = false; |
615 | 88 else |
89 if (*haystack) | |
90 haystack++; | |
91 else | |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
92 matched = false; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 } |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 else |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 { |
615 | 96 if (equals) |
97 { | |
98 if (*pattern) | |
99 pattern++; | |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
100 |
615 | 101 if (*haystack) |
102 haystack++; | |
103 } | |
104 else | |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
105 if (save) |
615 | 106 { |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
107 any = true; |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
108 pattern = save; |
615 | 109 } |
110 else | |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
111 matched = false; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 } |
615 | 113 |
114 if (!*haystack && !*pattern) | |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
115 end = true; |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 } |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
117 } while (matched && !end); |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 return matched; |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 } |
768
600a3c08747f
Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
121 #endif |
414
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 |
593
1a33088e7fa9
Rename some internal preprocessor macros.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
124 #undef TH_STRGLOB_FUNC |
1a33088e7fa9
Rename some internal preprocessor macros.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
125 #undef TH_STRGLOB_COLLATE |