From d5f2adb382bce679c7b8962a6c0a7197fa7b1fe2 Mon Sep 17 00:00:00 2001 From: skalogryz Date: Thu, 24 Mar 2016 12:56:23 +0000 Subject: [PATCH] richmemo: default search engine git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4587 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/richmemo/wsrichmemo.pas | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/components/richmemo/wsrichmemo.pas b/components/richmemo/wsrichmemo.pas index 093d4c690..59003f4e7 100644 --- a/components/richmemo/wsrichmemo.pas +++ b/components/richmemo/wsrichmemo.pas @@ -294,7 +294,67 @@ end; class function TWSCustomRichMemo.Search(const AWinControl: TWinControl; const ANiddle: string; const SearchOpts: TIntSearchOpt): Integer; +var + Hay : UnicodeString; + Niddle : UnicodeString; + Hay8 : String; + isUnicode : Boolean; + i, se, ln : Integer; + begin + if not Assigned(AWinControl) or not (AWinControl is TCustomRichMemo) then begin + Result:=-1; + Exit; + end; + if not GetTextLen(AWinControl, ln) then begin + Result:=-1; + Exit; + end; + + isUnicode:=false; + if not GetSubText(AWinControl, 0, ln, true, isUnicode, Hay8, Hay) then begin + Result:=-1; + Exit; + end; + if not isUnicode then begin + Hay:=UTF8Decode(Hay8); + Hay8:=''; + end; + + if not (soMatchCase in SearchOpts.Options) then begin + Niddle:=WideLowerCase(UTF8Decode(ANiddle)); + Hay:=WideLowercase(Hay); + end else + Niddle:=UTF8Decode(ANiddle); + + i:=SearchOpts.start; + if i<=0 then i:=1 else inc(i); + + if soBackward in SearchOpts.Options then begin + dec(i); // to allow repeatative search + + se:=0; + if (SearchOpts.len>0) then se:=i-SearchOpts.len; + if se<=0 then se:=1; + + for i:=i downto se do + //todo: unicode comparison is "a bit more" complex, than just memory comparison + if CompareMem(@Niddle[1], @Hay[i], length(Niddle)*2) then begin + Result:=i-1; // offset one character! since it should be cursor position + Exit; + end; + end else begin + se:=length(Hay)-length(Niddle); + if (SearchOpts.len>0) and (se-i>SearchOpts.len) then + se:=i+SearchOpts.len; + + for i:=i to se do + //todo: unicode comparison is "a bit more" complex, than just memory comparison + if CompareMem(@Niddle[1], @Hay[i], length(Niddle)*2) then begin + Result:=i-1; // offset one character! since it should be cursor position + Exit; + end; + end; Result:=-1; end;