home *** CD-ROM | disk | FTP | other *** search
- {NAME
- match_char -- match a character in a string
-
- SYNOPSIS
-
- match_char(match_str, ifirst, target_char, match_spec)
-
- str255: STRING[255]. A TYPE which must be declared before you INCLUDE
- the function.
- match_str: str255. String to search.
- ifirst: INTEGER. Index of start of search.
- target_char: BYTE. ORD of character to compare.
- match_type: (next_match, last_match, next_unmatch, last_unmatch)
- next_match- find first matching character after ifirst.
- last_match- find last matching character before ifirst.
- next_unmatch- find first non-matching character after ifirst.
- last_unmatch- find last non-matching character before ifirst.
- match_char: INTEGER. If the find succeeds, its index. If not, 0.
-
- DESCRIPTION
- The find starts with ifirst. If it meets the condition (match or
- unmatch, match_char = ifirst. If not, the index increases (next) or
- decreases (last) until either the find succeeds or it runs off the string.
- If it runs off the string, match_char = 0.
- match_char is moderately forgiving for ifirst. If the direction is
- up (next), it starts at the beginning of the string for a nonpositive
- ifirst. If the direction is down, it starts at the end of the string for
- ifirst too big.
-
- NOTE: These functions use MARK and RELEASE to handle the dynamic
- heap. As a result, any program which uses one of them can not use
- DISPOSE.
-
- L. Paper
- 11/18/84
- }
-
- TYPE
- match_type = (next_match, last_match, next_unmatch, last_unmatch);
-
- FUNCTION match_char(match_str: str255; ifirst:INTEGER; target_char: byte;
- match_spec: match_type): INTEGER;
-
- VAR
- n:INTEGER;
- next, match, more: BOOLEAN;
-
- BEGIN
-
- n := LENGTH(match_str);
-
- IF n > 0 THEN
- BEGIN
- next := (match_spec = next_match) OR (match_spec = next_unmatch);
- match := (match_spec = next_match) OR (match_spec = last_match);
-
- n := ifirst;
- IF next
- THEN
- BEGIN
- IF n < 1 THEN n := 1;
- IF n > LENGTH(match_str) THEN n := 0;
- END {next}
- ELSE {NOT next}
- BEGIN
- IF n < 1 THEN n := 0;
- IF n > LENGTH(match_str) THEN n := LENGTH(match_str);
- END; {NOT next} {For IF next}
- END; {IF n > 0}
-
- IF n > 0 THEN
- BEGIN {Avoid dangling ELSE}
- IF next
- THEN
- BEGIN
- {Use more rather than AND to avoid evaluating
- match_str[LENGTH(match_str) +1]}
- IF match
- THEN more := (ORD(match_str[n]) <> target_char)
- ELSE more := (ORD(match_str[n]) = target_char);
- WHILE more DO
- BEGIN
- n := n + 1;
- more := (n <= LENGTH(match_str));
- IF more THEN
- IF match
- THEN more := (ORD(match_str[n]) <> target_char)
- ELSE more := (ORD(match_str[n]) = target_char);
- END; {WHILE more}
- IF n > LENGTH(match_str) THEN n := 0;
- END {if next}
- ELSE {last}
- BEGIN
- IF match
- THEN WHILE (n > 0) AND (ORD(match_str[n]) <> target_char)
- DO n := n - 1
- ELSE WHILE (n > 0) AND (ORD(match_str[n]) = target_char)
- DO n := n - 1;
- IF n <= 0 THEN n := 0;
- END; {last} {For IF next}
- END; {IF n > 0}
- match_char := n;
- END; {match_char}