~ubuntu-branches/ubuntu/saucy/octave-miscellaneous/saucy-proposed

« back to all changes in this revision

Viewing changes to src/stringmatch.c

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot, Sébastien Villemot, Rafael Laboissiere
  • Date: 2012-04-02 13:20:23 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120402132023-t41xaso7sl5cex90
Tags: 1.1.0-1
[ Sébastien Villemot ]
* Imported Upstream version 1.1.0
* debian/patches/match-cell-array.patch: remove patch (applied upstream)
* debian/patches/waitbar-rename.patch: remove patch (applied upstream)
* debian/patches/no-flexml.patch: remove obsolete patch, flex no longer used
  (Closes: #666294)
* debian/copyright: reflect upstream changes
* debian/octave-miscellaneous.docs: remove, no more docs in the package
* debian/clean: remove obsolete file
* debian/rules: remove hack for wrong permissions in upstream tarball

[ Rafael Laboissiere ]
* debian/watch: Use the SourceForge redirector

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Extracted from tclUtil.c
2
 
 
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.
6
 
 
7
 
 
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
12
 
individual files.
13
 
 
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
22
 
they apply.
23
 
 
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.
29
 
 
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
35
 
MODIFICATIONS.
36
 
 
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. 
48
 
 
49
 
 */
50
 
 
51
 
 
52
 
/*
53
 
 *----------------------------------------------------------------------
54
 
 *
55
 
 * StringCaseMatch --
56
 
 *
57
 
 *      See if a particular string matches a particular pattern.
58
 
 *      Allows case insensitivity.
59
 
 *
60
 
 * Results:
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).
65
 
 *
66
 
 * Side effects:
67
 
 *      None.
68
 
 *
69
 
 *----------------------------------------------------------------------
70
 
 */
71
 
 
72
 
#include <ctype.h> /* PAK: need tolower declaration */
73
 
#define CONST const
74
 
#define UCHAR (unsigned char)
75
 
 
76
 
/* Note: restore original code from the Tcl tree if you want unicode
77
 
 * support. */
78
 
 
79
 
int
80
 
StringCaseMatch(string, pattern, nocase)
81
 
    CONST char *string;         /* String. */
82
 
    CONST char *pattern;        /* Pattern, which may contain special
83
 
                                 * characters. */
84
 
    int nocase;                 /* 0 for case sensitive, 1 for insensitive */
85
 
{
86
 
    int p /*, charLen*/;  /* PAK: unused */
87
 
    /* CONST char *pstart = pattern; */  /* PAK: unused */
88
 
    char ch1, ch2;
89
 
    
90
 
    while (1) {
91
 
        p = *pattern;
92
 
        
93
 
        /*
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.
97
 
         */
98
 
        
99
 
        if (p == '\0') {
100
 
            return (*string == '\0');
101
 
        }
102
 
        if ((*string == '\0') && (p != '*')) {
103
 
            return 0;
104
 
        }
105
 
 
106
 
        /*
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.
111
 
         */
112
 
        
113
 
        if (p == '*') {
114
 
            /*
115
 
             * Skip all successive *'s in the pattern
116
 
             */
117
 
            while (*(++pattern) == '*') {}
118
 
            p = *pattern;
119
 
            if (p == '\0') {
120
 
                return 1;
121
 
            }
122
 
            ch2 = (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
123
 
            while (1) {
124
 
                /*
125
 
                 * Optimization for matching - cruise through the string
126
 
                 * quickly if the next char in the pattern isn't a special
127
 
                 * character
128
 
                 */
129
 
                if ((p != '[') && (p != '?') && (p != '\\')) {
130
 
                    if (nocase) {
131
 
                        while (*string) {
132
 
                            ch1 = *string;
133
 
                            if (ch2==ch1 || ch2==tolower(ch1)) break;
134
 
                            string++;
135
 
                        }
136
 
                    } else {
137
 
                        while (*string) {
138
 
                            ch1 = *string;
139
 
                            if (ch2==ch1) break;
140
 
                            string++;
141
 
                        }
142
 
                    }
143
 
                }
144
 
                if (StringCaseMatch(string, pattern, nocase)) {
145
 
                    return 1;
146
 
                }
147
 
                if (*string == '\0') {
148
 
                    return 0;
149
 
                }
150
 
                string++;
151
 
            }
152
 
        }
153
 
 
154
 
        /*
155
 
         * Check for a "?" as the next pattern character.  It matches
156
 
         * any single character.
157
 
         */
158
 
 
159
 
        if (p == '?') {
160
 
            pattern++;
161
 
            string++;
162
 
            continue;
163
 
        }
164
 
 
165
 
        /*
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 "-").
169
 
         */
170
 
 
171
 
        if (p == '[') {
172
 
            char startChar, endChar;
173
 
 
174
 
            pattern++;
175
 
            ch1 = (nocase ? tolower(UCHAR(*string)) : UCHAR(*string));
176
 
            string++;
177
 
            while (1) {
178
 
                if ((*pattern == ']') || (*pattern == '\0')) {
179
 
                    return 0;
180
 
                }
181
 
                startChar = 
182
 
                        (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
183
 
                pattern++;
184
 
                if (*pattern == '-') {
185
 
                    pattern++;
186
 
                    if (*pattern == '\0') {
187
 
                        return 0;
188
 
                    }
189
 
                    endChar = 
190
 
                      (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
191
 
                    pattern++;
192
 
                    if (((startChar <= ch1) && (ch1 <= endChar))
193
 
                            || ((endChar <= ch1) && (ch1 <= startChar))) {
194
 
                        /*
195
 
                         * Matches ranges of form [a-z] or [z-a].
196
 
                         */
197
 
 
198
 
                        break;
199
 
                    }
200
 
                } else if (startChar == ch1) {
201
 
                    break;
202
 
                }
203
 
            }
204
 
            while (*pattern != ']') {
205
 
                if (*pattern == '\0') {
206
 
                    pattern--;
207
 
                    break;
208
 
                }
209
 
                pattern++;
210
 
            }
211
 
            pattern++;
212
 
            continue;
213
 
        }
214
 
 
215
 
        /*
216
 
         * If the next pattern character is '\', just strip off the '\'
217
 
         * so we do exact matching on the character that follows.
218
 
         */
219
 
 
220
 
        if (p == '\\') {
221
 
            pattern++;
222
 
            if (*pattern == '\0') {
223
 
                return 0;
224
 
            }
225
 
        }
226
 
 
227
 
        /*
228
 
         * There's no special character.  Just make sure that the next
229
 
         * bytes of each string match.
230
 
         */
231
 
 
232
 
        ch1 = *string++;
233
 
        ch2 = *pattern++;
234
 
        if (nocase) {
235
 
            if (tolower(ch1) != tolower(ch2)) {
236
 
                return 0;
237
 
            }
238
 
        } else if (ch1 != ch2) {
239
 
            return 0;
240
 
        }
241
 
    }
242
 
}