File: match.c

package info (click to toggle)
nn 6.5.1-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,472 kB
  • ctags: 4,518
  • sloc: ansic: 35,126; sh: 1,489; makefile: 214; awk: 138
file content (150 lines) | stat: -rw-r--r-- 2,915 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *
 *	String/Subject matching routines.
 */

#include "config.h"
#include "regexp.h"

export int case_fold_search = 1;

#define MAXFOLD	256	/* max length of any string */

/*
 *	Systems which have a tolower(c) function which is defined for
 *	all characters, but no _tolower(c) macro which works for
 *	isupper(c) only, may define HAVE_GENERIC_TOLOWER -- it
 *	may give a slight speed-up, but is not mandatory.
 */

#ifndef HAVE_GENERIC_TOLOWER
#ifndef _tolower
#define _tolower(c) tolower(c)
#endif
#endif

void
fold_string(mask)	/* convert mask to lower-case */
register char *mask;
{
    register char c;

    for ( ; (c = *mask); mask++) {
#ifdef _tolower
	if (!isascii(c) || !isupper(c)) continue;
	*mask = _tolower(c);
#else
	*mask = tolower(c);
#endif
    }
}

int
streq_fold(mask, str)		/* mask is prefix of str - FOLD */
register char *mask, *str;
{
    register char c = 0, d;

    while ((d = *mask++)) {
	if ((c = *str++) == NUL) return 0;
	if (c == d) continue;
#ifdef _tolower
	if (!isascii(c) || !isupper(c) || _tolower(c) != (unsigned)d) return 0;
#else
	if (tolower(c) != d) return 0;
#endif
    }
    return c == NUL ? 1 : 2;
}

int
strmatch_fold(mask, str)	/* mask occurs anywhere in str - FOLD */
char *mask;
register char *str;
{
    register char c, m1 = *mask++;

    for (;;) {
	while ((c = *str++)) {	/* find first occ. of mask[0] in str. */
	    if (c == m1) break;
#ifdef _tolower
	    if (!isascii(c) || !isupper(c)) continue;
	    if (_tolower(c) == (unsigned)m1) break;
#else
	    if (tolower(c) == m1) break;
#endif
	}
	if (c == NUL) return 0;
	if (streq_fold(mask, str)) return 1;
    }
}

int
strmatch(mask, str)		/* mask occurs anywhere in str - CASE */
char *mask;
register char *str;
{
    register char *q, *m;
    register char m1 = *mask;

    for (; *str; str++) {
	if (*str != m1) continue;

	q = str; m = mask;
	do
	    if (*++m == NUL) return 1;
	while (*++q == *m);
    }
    return 0;
}

int
strmatch_cf(mask, str)		/* fold if case_fold_search is set */
char *mask;
char *str;
{
    if (case_fold_search)
	return strmatch_fold(mask, str);

    return strmatch(mask, str);
}

/*
 *	case insensitive regexp matching
 */

int regexec_fold(prog, string)
register regexp *prog;
char  *string;
{
    char buf[256];
    register char c, *bp, *str, *maxb;

    bp = buf, maxb = &buf[255];
    str = string;
    while (bp < maxb && (c = *str++) != NUL)
#ifdef _tolower
	*bp++ = (!isascii(c) || !isupper(c)) ? c : _tolower(c);
#else
	*bp++ = tolower(c);
#endif
    *bp = NUL;

    if (!regexec(prog, buf)) return 0;

    prog->startp[0] = string + (prog->startp[0] - buf);
    prog->endp[0] = string + (prog->endp[0] - buf);
    return 1;
}

int regexec_cf(prog, string)
register regexp *prog;
char  *string;
{
    if (case_fold_search)
	return regexec_fold(prog, string);

    return regexec(prog, string);
}