1
/* Extracted from tclUtil.c
3
Copyright (c) 1987-1993 The Regents of the University of California.
4
Copyright (c) 1994-1998 Sun Microsystems, Inc.
5
Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.
8
This software is copyrighted by the Regents of the University of
9
California, Sun Microsystems, Inc., Scriptics Corporation, ActiveState
10
Corporation and other parties. The following terms apply to all files
11
associated with the software unless explicitly disclaimed in
14
The authors hereby grant permission to use, copy, modify, distribute,
15
and license this software and its documentation for any purpose, provided
16
that existing copyright notices are retained in all copies and that this
17
notice is included verbatim in any distributions. No written agreement,
18
license, or royalty fee is required for any of the authorized uses.
19
Modifications to this software may be copyrighted by their authors
20
and need not follow the licensing terms described here, provided that
21
the new terms are clearly indicated on the first page of each file where
24
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
25
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
26
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
27
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
28
POSSIBILITY OF SUCH DAMAGE.
30
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
32
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
33
IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
34
NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
37
GOVERNMENT USE: If you are acquiring this software on behalf of the
38
U.S. government, the Government shall have only "Restricted Rights"
39
in the software and related documentation as defined in the Federal
40
Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
41
are acquiring the software on behalf of the Department of Defense, the
42
software shall be classified as "Commercial Computer Software" and the
43
Government shall have only "Restricted Rights" as defined in Clause
44
252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
45
authors grant the U.S. Government and others acting in its behalf
46
permission to use and distribute the software in accordance with the
47
terms specified in this license.
53
*----------------------------------------------------------------------
57
* See if a particular string matches a particular pattern.
58
* Allows case insensitivity.
61
* The return value is 1 if string matches pattern, and
62
* 0 otherwise. The matching operation permits the following
63
* special characters in the pattern: *?\[] (see the manual
64
* entry for details on what these mean).
69
*----------------------------------------------------------------------
73
#define UCHAR (unsigned char)
75
/* Note: restore original code from the Tcl tree if you want unicode
79
StringCaseMatch(string, pattern, nocase)
80
CONST char *string; /* String. */
81
CONST char *pattern; /* Pattern, which may contain special
83
int nocase; /* 0 for case sensitive, 1 for insensitive */
86
CONST char *pstart = pattern;
93
* See if we're at the end of both the pattern and the string. If
94
* so, we succeeded. If we're at the end of the pattern but not at
95
* the end of the string, we failed.
99
return (*string == '\0');
101
if ((*string == '\0') && (p != '*')) {
106
* Check for a "*" as the next pattern character. It matches
107
* any substring. We handle this by calling ourselves
108
* recursively for each postfix of string, until either we
109
* match or we reach the end of the string.
114
* Skip all successive *'s in the pattern
116
while (*(++pattern) == '*') {}
121
ch2 = (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
124
* Optimization for matching - cruise through the string
125
* quickly if the next char in the pattern isn't a special
128
if ((p != '[') && (p != '?') && (p != '\\')) {
132
if (ch2==ch1 || ch2==tolower(ch1)) break;
143
if (StringCaseMatch(string, pattern, nocase)) {
146
if (*string == '\0') {
154
* Check for a "?" as the next pattern character. It matches
155
* any single character.
165
* Check for a "[" as the next pattern character. It is followed
166
* by a list of characters that are acceptable, or by a range
167
* (two characters separated by "-").
171
char startChar, endChar;
174
ch1 = (nocase ? tolower(UCHAR(*string)) : UCHAR(*string));
177
if ((*pattern == ']') || (*pattern == '\0')) {
181
(nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
183
if (*pattern == '-') {
185
if (*pattern == '\0') {
189
(nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
191
if (((startChar <= ch1) && (ch1 <= endChar))
192
|| ((endChar <= ch1) && (ch1 <= startChar))) {
194
* Matches ranges of form [a-z] or [z-a].
199
} else if (startChar == ch1) {
203
while (*pattern != ']') {
204
if (*pattern == '\0') {
215
* If the next pattern character is '\', just strip off the '\'
216
* so we do exact matching on the character that follows.
221
if (*pattern == '\0') {
227
* There's no special character. Just make sure that the next
228
* bytes of each string match.
234
if (tolower(ch1) != tolower(ch2)) {
237
} else if (ch1 != ch2) {