home *** CD-ROM | disk | FTP | other *** search
- unit strtest;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls,
- StrUtils;
-
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- ParseFirstTokenBtn: TButton;
- Memo1: TMemo;
- ParseAllTokensBtn: TButton;
- HighlightTokensBtn: TButton;
- FindTokenIndexesBtn: TButton;
- HighlightTokensInMemoBtn: TButton;
- procedure ParseFirstTokenBtnClick(Sender: TObject);
- procedure ParseAllTokensBtnClick(Sender: TObject);
- procedure HighlightTokensBtnClick(Sender: TObject);
- procedure FindTokenIndexesBtnClick(Sender: TObject);
- procedure HighlightTokensInMemoBtnClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure Delay(Num: longint);
- function GetLineSelStartIndex( LineNum: LongInt ) : LongInt;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
- procedure TForm1.Delay(Num: longint);
- var
- tc :longint;
- begin
- tc :=GetTickCount;
- repeat
- Application.ProcessMessages;
- until ((GetTickCount-tc) >= Num);
- end;
-
- procedure TForm1.ParseFirstTokenBtnClick(Sender: TObject);
- var
- s, first, rest : string;
- tokstart, tokend : integer;
- begin
- s := Edit1.Text;
- first := ''; rest := '';
- tokstart := 0; tokend := 0;
- firstTokenAt( s,tokstart,tokend,first,rest);
- Memo1.Clear;
- Memo1.Lines.Add( Format('tokstart=%d, tokend = %d, first=%s, rest=%s',
- [tokstart,tokend, first, rest]) );
- end;
-
- procedure TForm1.ParseAllTokensBtnClick(Sender: TObject);
- var
- first, rest : string;
- tokstart, tokend : integer;
- begin
- rest := Edit1.Text;
- first := '';
- tokstart := 1;
- tokend := 0;
- Memo1.Clear;
- while (tokstart <> 0) do
- begin
- firstTokenAt( rest, tokstart, tokend, first, rest);
- Memo1.Lines.Add( Format('tokstart=%d, tokend = %d, first=%s, rest=%s',
- [tokstart,tokend,first,rest]) );
- end;
-
- end;
-
- procedure TForm1.HighlightTokensBtnClick(Sender: TObject);
- var
- ti : tokenindexes;
- i, numfound : integer;
- s : string;
- begin
- s := Edit1.Text;
- TokensFoundAt( s, numfound, ti );
- Memo1.Clear;
- for i := 1 to numfound do
- begin
- Memo1.Lines.Add( Format('ti[%d].tstart=%d, .tend=%d',
- [i,ti[i].tstart,ti[i].tend]) );
- Edit1.SetFocus;
- Edit1.SelStart := ti[i].tstart;
- Edit1.SelLength := ti[i].tend;
- Delay(500);
- end;
- end;
-
- procedure TForm1.FindTokenIndexesBtnClick(Sender: TObject);
- var
- ti : tokenindexes;
- i, numfound : integer;
- s : string;
- begin
- s := Edit1.Text;
- TokensFoundAt( s, numfound, ti );
- Memo1.Clear;
- Memo1.Lines.Add( Format('s = %s, numfound=%d',
- [s, numfound]) );
- for i := 1 to numfound do
- Memo1.Lines.Add( Format('ti[%d].tstart=%d, .tend=%d',
- [i,ti[i].tstart,ti[i].tend]) );
-
-
- end;
-
- function TForm1.GetLineSelStartIndex( LineNum: LongInt ) : LongInt;
- { return Selection (char num from start of RichEdit) index of 1st character in
- the Line specified by LineNum }
- begin
- result := SendMessage(Memo1.Handle, EM_LINEINDEX, LineNum,0);
- end;
-
- procedure TForm1.HighlightTokensInMemoBtnClick(Sender: TObject);
- // highlight tokens in 2nd line of Memo
- var
- ti : tokenindexes;
- i, numfound : integer;
- StartIndex : LongInt;
- s : string;
- begin
- s := Memo1.Lines[1];
- TokensFoundAt( s, numfound, ti );
- Memo1.SetFocus;
- StartIndex := GetLineSelStartIndex( 1 );
- for i := 1 to numfound do
- begin
- Memo1.SelStart := StartIndex + ti[i].tstart;
- Memo1.SelLength := ti[i].tend;
- Delay(500);
- end;
- end;
-
- end.
-