searchbuf.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. type
  2. TEqualFunction = function (const a,b : char) : boolean;
  3. function EqualWithCase (const a,b : char) : boolean;
  4. begin
  5. result := (a = b);
  6. end;
  7. function EqualWithoutCase (const a,b : char) : boolean;
  8. begin
  9. result := (lowerCase(a) = lowerCase(b));
  10. end;
  11. function IsWholeWord (bufstart, bufend, wordstart, wordend : pchar) : boolean;
  12. begin
  13. // Check start
  14. result := ((wordstart = bufstart) or ((wordstart-1)^ in worddelimiters)) and
  15. // Check end
  16. ((wordend = bufend) or ((wordend+1)^ in worddelimiters));
  17. end;
  18. function SearchDown(buf,aStart,endchar:pchar; SearchString:string;
  19. Equals : TEqualFunction; WholeWords:boolean) : pchar;
  20. var Found : boolean;
  21. s, c : pchar;
  22. begin
  23. result := aStart;
  24. Found := false;
  25. while not Found and (result <= endchar) do
  26. begin
  27. // Search first letter
  28. while (result <= endchar) and not Equals(result^,SearchString[1]) do
  29. inc (result);
  30. // Check if following is searchstring
  31. c := result;
  32. s := @(Searchstring[1]);
  33. Found := true;
  34. while (c <= endchar) and (s^ <> #0) and Found do
  35. begin
  36. Found := Equals(c^, s^);
  37. inc (c);
  38. inc (s);
  39. end;
  40. if s^ <> #0 then
  41. Found := false;
  42. // Check if it is a word
  43. if Found and WholeWords then
  44. Found := IsWholeWord(buf,endchar,result,c-1);
  45. if not found then
  46. inc (result);
  47. end;
  48. if not Found then
  49. result := nil;
  50. end;
  51. function SearchUp(buf,aStart,endchar:pchar; SearchString:string;
  52. equals : TEqualFunction; WholeWords:boolean) : pchar;
  53. var Found : boolean;
  54. s, c, l : pchar;
  55. begin
  56. result := aStart;
  57. Found := false;
  58. l := @(SearchString[length(SearchString)]);
  59. while not Found and (result >= buf) do
  60. begin
  61. // Search last letter
  62. while (result >= buf) and not Equals(result^,l^) do
  63. dec (result);
  64. // Check if before is searchstring
  65. c := result;
  66. s := l;
  67. Found := true;
  68. while (c >= buf) and (s >= @SearchString[1]) and Found do
  69. begin
  70. Found := Equals(c^, s^);
  71. dec (c);
  72. dec (s);
  73. end;
  74. if (s >= @(SearchString[1])) then
  75. Found := false;
  76. // Check if it is a word
  77. if Found and WholeWords then
  78. Found := IsWholeWord(buf,endchar,c+1,result);
  79. if found then
  80. result := c+1
  81. else
  82. dec (result);
  83. end;
  84. if not Found then
  85. result := nil;
  86. end;
  87. //function SearchDown(buf,aStart,endchar:pchar; SearchString:string; equal : TEqualFunction; WholeWords:boolean) : pchar;
  88. function SearchBuf(Buf: PChar;BufLen: Integer;SelStart: Integer;SelLength: Integer;
  89. SearchString: String;Options: TStringSearchOptions):PChar;
  90. var
  91. equal : TEqualFunction;
  92. begin
  93. SelStart := SelStart + SelLength;
  94. if (SearchString = '') or (SelStart > BufLen) or (SelStart < 0) then
  95. result := nil
  96. else
  97. begin
  98. if soMatchCase in Options then
  99. Equal := @EqualWithCase
  100. else
  101. Equal := @EqualWithoutCase;
  102. if soDown in Options then
  103. result := SearchDown(buf,buf+SelStart,Buf+(BufLen-1), SearchString, Equal, (soWholeWord in Options))
  104. else
  105. result := SearchUp(buf,buf+SelStart,Buf+(Buflen-1), SearchString, Equal, (soWholeWord in Options));
  106. end;
  107. end;