ex1.pp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. {$ifdef fpc}
  2. {$mode objfpc}
  3. {$h+}
  4. {$endif}
  5. Program ex1;
  6. uses StrUtils;
  7. Const
  8. S : PChar = 'Some very long string with some words in it';
  9. Starts : Array[1..3] of Integer = (0,10,41);
  10. Procedure DoTest(Sub:String; Start : Integer; O : TStringSearchOptions);
  11. Var
  12. Res : String;
  13. P : PChar;
  14. begin
  15. Write('Searching for "',Sub,'" starting at pos ',Start,' : ');
  16. P:=SearchBuf(Pchar(S),Length(S),Start,0,Sub,O);
  17. if (P=Nil) then
  18. Writeln('Not found')
  19. else
  20. begin
  21. Res:=StringOfChar(' ',Length(Sub));
  22. SetLength(Res,Length(Sub));
  23. Move(P^,Res[1],Length(Sub));
  24. Writeln('Found at pos ',(P-PChar(S)),' : ',Res);
  25. end;
  26. end;
  27. Procedure DoTests(Sub : String; O : TStringSearchOptions);
  28. Var
  29. I : Integer;
  30. begin
  31. Writeln('Searching up');
  32. For I:=1 to 3 do
  33. DoTest(Sub,Starts[i],O);
  34. Include(O,soDown);
  35. Writeln('Searching down');
  36. For I:=1 to 3 do
  37. DoTest(Sub,Starts[i],O);
  38. end;
  39. Procedure DoAllTests(S : String);
  40. begin
  41. Writeln('No options');
  42. DoTests(S,[]);
  43. Writeln('Match case:');
  44. DoTests(S,[soMatchCase]);
  45. Writeln('Whole word:');
  46. DoTests(S,[soWholeWord]);
  47. Writeln('Match case, whole word:');
  48. DoTests(S,[soMatchCase,soWholeWord]);
  49. end;
  50. begin
  51. DoAllTests('very');
  52. DoAllTests('Very');
  53. DoAllTests('in');
  54. DoAllTests('In');
  55. end.