-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtinymatch.pas
45 lines (37 loc) · 1.03 KB
/
dtinymatch.pas
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
unit dtinymatch;
// tiny wildcard/pattern matching. Based on anonymous souce code (Rob Pike's ?).
// - rlyeh. public domain | wtrmrkrlyeh
// - pascal port by Doj
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
interface
function Match(Pattern, S: PAnsiChar): Boolean;
implementation
function Match(Pattern, S: PAnsiChar): Boolean;
begin
if Pattern^ = #0 then
Exit(S^ = #0);
if Pattern^ = '*' then
Exit(Match(Pattern + 1, S) or ((S^ <> #0) and Match(Pattern, S + 1)));
if Pattern^ = '?' then
Exit((S^ <> #0) and (S^ <> '.') and Match(Pattern + 1, S + 1));
Exit((S^ = Pattern^) and Match(Pattern + 1, S + 1));
end;
// procedure Example(Pattern, S: PAnsiChar);
// begin
// if Match(Pattern, S) then begin
// Writeln(Pattern, ' found in ', S);
// end else
// Writeln(Pattern, ' not found in ', S);
// end;
//
// begin
// Example('abc', 'abc');
// Example('abc*', 'abcd');
// Example('*bc', 'abc');
// Example('*bc*', 'abc');
// Example('*b?d*', 'abcdef');
// end.
end.