Skip to content

Commit b640518

Browse files
committed
rewrite fuzzu search, #5509
1 parent 5ae63a6 commit b640518

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

app/proc_str.pas

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,26 @@ function SMaskFilenameSlashes(const fn: string): string;
9898
result:= StringReplace(result, '\', '|', [rfReplaceAll]);
9999
end;
100100

101-
function SFindFuzzyPositions(SText, SFind: UnicodeString): TATIntArray;
101+
function SFindFuzzyPositions(const SText, SFind: UnicodeString): TATIntArray;
102+
//
103+
function IsCharSep(const ch: WideChar): boolean;
104+
begin
105+
Result:= Pos(ch, ' _.,:;/\-+()[]{}=|')>0;
106+
end;
107+
//
108+
function IsCharUpperLetter(const ch: WideChar): boolean;
109+
begin
110+
Result:= (ch>='A') and (ch<='Z');
111+
end;
112+
//
102113
var
103-
i, N: integer;
114+
STextUpper: UnicodeString;
115+
i, N, N1, N2: integer;
104116
begin
105117
Result:= nil;
118+
STextUpper:= UnicodeUpperCase(SText);
106119

107-
SText:= UnicodeLowerCase(SText);
108-
SFind:= UnicodeLowerCase(SFind);
109-
120+
{
110121
//if simple match is found, don't calculate complex fuzzy matches
111122
N:= Pos(SFind, SText);
112123
if N>0 then
@@ -117,15 +128,25 @@ function SFindFuzzyPositions(SText, SFind: UnicodeString): TATIntArray;
117128
Result[i]:= Result[i-1]+1;
118129
exit;
119130
end;
131+
}
120132

121133
//calculate complex matches
122-
N:= 0;
123134
SetLength(Result, Length(SFind));
135+
N2:= 0;
124136
for i:= 1 to Length(SFind) do
125137
begin
126-
N:= PosEx(SFind[i], SText, N+1);
127-
if N=0 then Exit(nil);
128-
Result[i-1]:= N;
138+
N1:= N2;
139+
repeat
140+
N:= N2;
141+
N2:= PosEx(UpCase(SFind[i]), STextUpper, N+1);
142+
if N2=0 then Exit(nil);
143+
144+
if N2=N1+1 then Break;
145+
if IsCharUpperLetter(SText[N2]) then Break;
146+
if (N2>1) and IsCharSep(SText[N2-1]) then Break;
147+
until false;
148+
149+
Result[i-1]:= N2;
129150
end;
130151
end;
131152

0 commit comments

Comments
 (0)