~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily

« back to all changes in this revision

Viewing changes to src/stringmatch.c

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot, Rafael Laboissiere, Sébastien Villemot
  • Date: 2012-10-17 13:40:55 UTC
  • mfrom: (1.2.1) (12 sid)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20121017134055-vatltexghy77fnv7
Tags: 1.2.0-1
[ Rafael Laboissiere ]
* Imported Upstream version 1.2.0
* Bump Standards-Version to 3.9.4 (no changes needed)
* Refresh for new upstream release
* Use Sébastien Villemot's @debian.org email address
* Remove obsolete DM-Upload-Allowed flag
* Add patch autoload-yes.patch
* Add copyright info for file lauchli.m (included in a Debian patch)
* Add patch partcnt-test-succeeds.patch
* Build-depends on octave-pkg-dev >= 1.0.3

[ Sébastien Villemot ]
* debian/control: fix versioned dependency on debhelper
* Add lintian override for false positive on hardening (fortify)

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