forked from TurboPack/SynEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynEditSearch.pas
303 lines (267 loc) · 8.25 KB
/
SynEditSearch.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: SynEditSearch.pas, released 2000-04-07.
The Original Code is based on the mwEditSearch.pas file from the mwEdit
component suite by Martin Waldenburg and other developers.
Portions created by Martin Waldenburg are Copyright 1999 Martin Waldenburg.
Unicode translation by Maël Hörz.
All Rights Reserved.
Contributors to the SynEdit project are listed in the Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: SynEditSearch.pas,v 1.12.2.6 2009/09/29 00:16:46 maelh Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Known Issues:
-------------------------------------------------------------------------------}
unit SynEditSearch;
{$I SynEdit.inc}
interface
uses
SynEditTypes,
SynEditMiscClasses,
SynUnicode,
Classes;
type
TSynEditSearch = class(TSynEditSearchCustom)
private
Run: PWideChar;
Origin: PWideChar;
TheEnd: PWideChar;
Pat, CasedPat: string;
fCount: Integer;
fTextLen: Integer;
Look_At: Integer;
PatLen, PatLenSucc: Integer;
Shift: array[WideChar] of Integer;
fCaseSensitive: Boolean;
fWhole: Boolean;
fResults: TList;
fShiftInitialized: Boolean;
FTextToSearch: string;
function GetFinished: Boolean;
procedure InitShiftTable;
procedure SetCaseSensitive(const Value: Boolean);
protected
function TestWholeWord: Boolean;
procedure SetPattern(const Value: string); override;
function GetPattern: string; override;
function GetLength(Index: Integer): Integer; override;
function GetResult(Index: Integer): Integer; override;
function GetResultCount: Integer; override;
procedure SetOptions(const Value: TSynSearchOptions); override;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
function FindAll(const NewText: string): Integer; override;
function Replace(const aOccurrence, aReplacement: string): string; override;
function FindFirst(const NewText: string): Integer;
procedure FixResults(First, Delta: Integer);
function Next: Integer;
property Count: Integer read fCount write fCount;
property Finished: Boolean read GetFinished;
property Pattern read CasedPat;
property CaseSensitive: Boolean read fCaseSensitive write SetCaseSensitive;
property Whole: Boolean read fWhole write fWhole;
end;
implementation
uses
Windows,
SysUtils;
constructor TSynEditSearch.Create(aOwner: TComponent);
begin
inherited;
fResults := TList.Create;
end;
function TSynEditSearch.GetFinished: Boolean;
begin
Result := (Run >= TheEnd) or (PatLen >= fTextLen);
end;
function TSynEditSearch.GetResult(Index: Integer): Integer;
begin
Result := 0;
if (Index >= 0) and (Index < fResults.Count) then
Result := Integer(fResults[Index]);
end;
function TSynEditSearch.GetResultCount: Integer;
begin
Result := fResults.Count;
end;
procedure TSynEditSearch.FixResults(First, Delta: Integer);
var
i: Integer;
begin
if (Delta <> 0) and (fResults.Count > 0) then begin
i := Pred(fResults.Count);
while i >= 0 do begin
if Integer(fResults[i]) <= First then break;
fResults[i] := pointer(Integer(fResults[i]) - Delta);
Dec(i);
end;
end;
end;
procedure TSynEditSearch.InitShiftTable;
var
C: WideChar;
I: Integer;
begin
PatLen := Length(Pat);
if Patlen = 0 then raise Exception.Create('Pattern is empty');
PatLenSucc := PatLen + 1;
Look_At := 1;
for C := Low(WideChar) to High(WideChar) do Shift[C] := PatLenSucc;
for I := 1 to PatLen do Shift[Pat[I]] := PatLenSucc - I;
while Look_at < PatLen do
begin
if Pat[PatLen] = Pat[PatLen - Look_at] then break;
inc(Look_at);
end;
fShiftInitialized := True;
end;
// TODO: would be more intelligent to use IsWordBreakChar for SynEdit
function IsWordBreakChar(C: WideChar): Boolean;
begin
case C of
#0..#32, '.', ',', ';', ':', '"', '''', '´', '`', '°', '^', '!', '?', '&',
'$', '@', '§', '%', '#', '~', '[', ']', '(', ')', '{', '}', '<', '>',
'-', '=', '+', '*', '/', '\', '|':
Result := True;
else
Result := False;
end;
end;
function TSynEditSearch.TestWholeWord: Boolean;
var
Test: PWideChar;
begin
Test := Run - PatLen;
Result := ((Test < Origin) or IsWordBreakChar(Test[0])) and
((Run >= TheEnd) or IsWordBreakChar(Run[1]));
end;
function TSynEditSearch.Next: Integer;
var
I: Integer;
J: PWideChar;
begin
Result := 0;
inc(Run, PatLen);
while Run < TheEnd do
begin
if Pat[Patlen] <> Run^ then
inc(Run, Shift[(Run + 1)^])
else
begin
J := Run - PatLen + 1;
I := 1;
while Pat[I] = J^ do
begin
if I = PatLen then
begin
if fWhole and not TestWholeWord then break;
inc(fCount);
Result := Run - Origin - Patlen + 2;
exit;
end;
inc(I);
inc(J);
end;
Inc(Run, Look_At);
if Run >= TheEnd then
break;
Inc(Run, Shift[Run^] - 1);
end;
end;
end;
destructor TSynEditSearch.Destroy;
begin
fResults.Free;
inherited Destroy;
end;
procedure TSynEditSearch.SetPattern(const Value: string);
begin
if Pat <> Value then
begin
CasedPat := Value;
if CaseSensitive then
Pat := CasedPat
else
Pat := SysUtils.AnsiLowerCase(CasedPat);
fShiftInitialized := False;
end;
fCount := 0;
end;
procedure TSynEditSearch.SetCaseSensitive(const Value: Boolean);
begin
if fCaseSensitive <> Value then
begin
fCaseSensitive := Value;
if fCaseSensitive then
Pat := CasedPat
else
Pat := SysUtils.AnsiLowerCase(CasedPat);
fShiftInitialized := False;
end;
end;
function TSynEditSearch.FindAll(const NewText: string): Integer;
var
Found: Integer;
begin
// never shrink Capacity
fResults.Count := 0;
Found := FindFirst(NewText);
while Found > 0 do
begin
fResults.Add(Pointer(Found));
Found := Next;
end;
Result := fResults.Count;
end;
function TSynEditSearch.Replace(const aOccurrence, aReplacement: string): string;
begin
Result := aReplacement;
end;
function TSynEditSearch.FindFirst(const NewText: string): Integer;
begin
if not fShiftInitialized then
InitShiftTable;
Result := 0;
fTextLen := Length(NewText);
if fTextLen >= PatLen then
begin
if CaseSensitive then
FTextToSearch := NewText
else
FTextToSearch := SysUtils.AnsiLowerCase(NewText);
Origin := PWideChar(FTextToSearch);
TheEnd := Origin + fTextLen;
Run := (Origin - 1);
Result := Next;
end;
end;
function TSynEditSearch.GetLength(Index: Integer): Integer;
begin
Result := PatLen;
end;
function TSynEditSearch.GetPattern: string;
begin
Result := CasedPat;
end;
procedure TSynEditSearch.SetOptions(const Value: TSynSearchOptions);
begin
CaseSensitive := ssoMatchCase in Value;
Whole := ssoWholeWord in Value;
end;
end.