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
*----------------------------------------------------------------------
72
#include <ctype.h> /* PAK: need tolower declaration */
74
#define UCHAR (unsigned char)
76
/* Note: restore original code from the Tcl tree if you want unicode
80
StringCaseMatch(string, pattern, nocase)
81
CONST char *string; /* String. */
82
CONST char *pattern; /* Pattern, which may contain special
84
int nocase; /* 0 for case sensitive, 1 for insensitive */
86
int p /*, charLen*/; /* PAK: unused */
87
/* CONST char *pstart = pattern; */ /* PAK: unused */
94
* See if we're at the end of both the pattern and the string. If
95
* so, we succeeded. If we're at the end of the pattern but not at
96
* the end of the string, we failed.
100
return (*string == '\0');
102
if ((*string == '\0') && (p != '*')) {
107
* Check for a "*" as the next pattern character. It matches
108
* any substring. We handle this by calling ourselves
109
* recursively for each postfix of string, until either we
110
* match or we reach the end of the string.
115
* Skip all successive *'s in the pattern
117
while (*(++pattern) == '*') {}
122
ch2 = (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
125
* Optimization for matching - cruise through the string
126
* quickly if the next char in the pattern isn't a special
129
if ((p != '[') && (p != '?') && (p != '\\')) {
133
if (ch2==ch1 || ch2==tolower(ch1)) break;
144
if (StringCaseMatch(string, pattern, nocase)) {
147
if (*string == '\0') {
155
* Check for a "?" as the next pattern character. It matches
156
* any single character.
166
* Check for a "[" as the next pattern character. It is followed
167
* by a list of characters that are acceptable, or by a range
168
* (two characters separated by "-").
172
char startChar, endChar;
175
ch1 = (nocase ? tolower(UCHAR(*string)) : UCHAR(*string));
178
if ((*pattern == ']') || (*pattern == '\0')) {
182
(nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
184
if (*pattern == '-') {
186
if (*pattern == '\0') {
190
(nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
192
if (((startChar <= ch1) && (ch1 <= endChar))
193
|| ((endChar <= ch1) && (ch1 <= startChar))) {
195
* Matches ranges of form [a-z] or [z-a].
200
} else if (startChar == ch1) {
204
while (*pattern != ']') {
205
if (*pattern == '\0') {
216
* If the next pattern character is '\', just strip off the '\'
217
* so we do exact matching on the character that follows.
222
if (*pattern == '\0') {
228
* There's no special character. Just make sure that the next
229
* bytes of each string match.
235
if (tolower(ch1) != tolower(ch2)) {
238
} else if (ch1 != ch2) {