summaryrefslogtreecommitdiff
path: root/fpcdocs/strutex/ex1.pp
blob: 357e6cd3683a69122ffcb7500caaa2c324814c53 (plain)
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
{$ifdef fpc}
{$mode objfpc}
{$h+}
{$endif}
Program ex1;

uses StrUtils;

Const
  S : PChar = 'Some very long string with some words in it';
  Starts : Array[1..3] of Integer = (0,10,41);

Procedure DoTest(Sub:String; Start : Integer; O : TStringSearchOptions);

Var
  Res : String;
  P : PChar;

begin
  Write('Searching for "',Sub,'" starting at pos ',Start,' : ');
  P:=SearchBuf(Pchar(S),Length(S),Start,0,Sub,O);
  if (P=Nil) then
      Writeln('Not found')
  else
    begin
    Res:=StringOfChar(' ',Length(Sub));
    SetLength(Res,Length(Sub));
    Move(P^,Res[1],Length(Sub));
    Writeln('Found at pos ',(P-PChar(S)),' : ',Res);
    end;
end;

Procedure DoTests(Sub : String; O : TStringSearchOptions);

Var
  I : Integer;

begin
  Writeln('Searching up');
  For I:=1 to 3 do
    DoTest(Sub,Starts[i],O);
  Include(O,soDown);
  Writeln('Searching down');
  For I:=1 to 3 do
    DoTest(Sub,Starts[i],O);
end;

Procedure DoAllTests(S : String);

begin
  Writeln('No options');
  DoTests(S,[]);
  Writeln('Match case:');
  DoTests(S,[soMatchCase]);
  Writeln('Whole word:');
  DoTests(S,[soWholeWord]);
  Writeln('Match case, whole word:');
  DoTests(S,[soMatchCase,soWholeWord]);
end;

begin
  DoAllTests('very');
  DoAllTests('Very');
  DoAllTests('in');
  DoAllTests('In');
end.