aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/strsearch.x
blob: e3006ed8813272fab14dc438acf464d5b7b60d70 (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

# STRSEARCH -- Search a string for a substring.  This is the simplest and
# fastest member of the pattern matching family.  A significant increase in
# efficiency will result if this procedure is used to search for substrings
# that do not use any metacharacters.

int procedure strsearch (str, patstr)

char	str[ARB]		# string to be searched
char	patstr[ARB]		# substring to search for

int	first_char, ch
int	ip, patlen
bool	strse1()

begin
	# The null pattern matches any string.
	if (patstr[1] == EOS)
	    return (1)

	first_char = patstr[1]

	do ip = 1, ARB {
	    ch = str[ip]
	    if (ch == EOS)
		break
	    if (ch == first_char)
		if (strse1 (str[ip], patstr, patlen))
		    return (ip + patlen)
	}

	return (0)
end


# STRSE1 -- Internal routine which compares a substring of the first string
# with the pattern string.  STREQ cannot be used because it does not give a
# match unless the two strings have the same length.

bool procedure strse1 (str, patstr, patlen)

char	str[ARB]
char	patstr[ARB]
int	patlen
int	ip

begin
	do ip = 1, ARB
	    if (patstr[ip] == EOS || str[ip] != patstr[ip])
		break

	patlen = ip - 1
	return (patstr[ip] == EOS)
end