From dc972168510407d04a2cc50cfcb389221e606787 Mon Sep 17 00:00:00 2001 From: "C.Ponapalt" Date: Thu, 11 Mar 2004 06:15:53 +0000 Subject: [PATCH] =?utf8?q?=E6=AD=A3=E8=A6=8F=E8=A1=A8=E7=8F=BE=E6=A4=9C?= =?utf8?q?=E7=B4=A2=E7=B5=A1=E3=81=BF=E3=81=AE=E4=B8=8D=E5=85=B7=E5=90=88?= =?utf8?q?=E5=AF=BE=E5=87=A6=E3=80=80BRegExp.pas=E3=81=AE=E6=9C=80?= =?utf8?q?=E6=96=B0=E7=89=88=E3=81=B8=E3=81=AE=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bottleclient/BRegExp.pas | 230 ++++++++++++++++++++++++++++++++++++----------- bottleclient/LogForm.pas | 3 +- 2 files changed, 181 insertions(+), 52 deletions(-) diff --git a/bottleclient/BRegExp.pas b/bottleclient/BRegExp.pas index 8e9ae3d..02640db 100755 --- a/bottleclient/BRegExp.pas +++ b/bottleclient/BRegExp.pas @@ -2,7 +2,7 @@ unit BRegExp; //===================================================================== // BRegExp.pas : Borland Delphi —p BREGEXP.DLL —˜—pƒ†ƒjƒbƒg -// 1998/10/11”Å osamu@big.or.jp +// 1998/10/03”Å osamu@big.or.jp // // BREGEXP.DLL ‚́Ahttp://www.hi-ho.or.jp/~babaq/ ‚É‚ÄŒöŠJ‚³‚ê‚Ä‚¢‚é // Perl5ŒÝŠ·‚̐³‹K•\Œ»ƒGƒ“ƒWƒ“ BREGEXP.DLL ‚ð Borland Delphi ‚©‚ç—˜—p @@ -24,11 +24,14 @@ unit BRegExp; // ‚¨’m‚点‰º‚³‚ê‚΁A‹C•ªŽŸ‘æ‚Å‚Í‚È‚ñ‚ç‚©‚̑Ώˆ‚ð‚·‚é‰Â”\«‚ª‚ ‚è‚Ü‚·B(^_^; // // Žg—p•û–@‚ɂ‚¢‚Ä‚Í•t‘®‚̃wƒ‹ƒvƒtƒ@ƒCƒ‹‚ð‚²——‰º‚³‚¢B -// -// [—š—ð] -// 98.10.3 ‰Šúƒo[ƒWƒ‡ƒ“ -// 98.10.11 ƒoƒOƒtƒBƒbƒNƒX (TBRegExpRec.splitp ‚ð ^PChar -> PPChar) -// +//===================================================================== +// 2001/04/14”Å osamu@big.or.jp +// –{‰Æ‚̃hƒLƒ…ƒƒ“ƒg‚̃o[ƒWƒ‡ƒ“ƒAƒbƒv‚É”º‚¢”­Šo‚µ‚Ä‚¢‚½ƒoƒO‚ðC³ +// brx ŠÖ”‚𓱓ü +// ‹ó•¶Žš‚ɑ΂·‚錟õ‚̃Gƒ‰[‰ñ”ð +// MatchPos ‚ð 1 ‚©‚琔‚¦Žn‚ß‚é‚悤‚ÉŽd—l•ÏX +// Subst Œã‚É Strings[] ‚ðŽQÆ‰Â”\‚É‚µ‚½ +// ‚±‚ê‚É”º‚¢‘å—Ê‚Ì•¶Žš—ñ‚ɑ΂·‚é’u‚«Š·‚¦“®ì‚Í’x‚­‚È‚Á‚½ //===================================================================== interface @@ -40,9 +43,12 @@ uses SysUtils; // BREGEXP.DLL ‚Æ’¼Œ‹‚µ‚½éŒ¾ //===================================================================== +const +BREGEXP_ERROR_MAX= 80; // ƒGƒ‰[ƒƒbƒZ[ƒW‚̍ő咷 + type PPChar=^PChar; -TBRegExpRec=record +TBRegExpRec=packed record outp: PChar; // ’uŠ·‚¦Œ‹‰Êæ“ªƒ|ƒCƒ“ƒ^ outendp: PChar; // ’uŠ·‚¦Œ‹‰Ê––”öƒ|ƒCƒ“ƒ^ splitctr: Integer; // split Œ‹‰ÊƒJƒEƒ“ƒ^ @@ -53,6 +59,7 @@ TBRegExpRec=record transtblp: PChar; // tr ƒe[ƒuƒ‹‚ւ̃|ƒCƒ“ƒ^ startp: PPChar; // ƒ}ƒbƒ`‚µ‚½•¶Žš—ñ‚ւ̐擪ƒ|ƒCƒ“ƒ^ endp: PPChar; // ƒ}ƒbƒ`‚µ‚½•¶Žš—ñ‚Ö‚Ì––”öƒ|ƒCƒ“ƒ^ + nparens: Integer; // match/subst ’†‚ÌŠ‡ŒÊ‚̐” end; pTBRegExpRec=^TBRegExpRec; @@ -83,30 +90,53 @@ TBRegExpMode=(brxNone, brxMatch, brxSplit); TBRegExp=class(TObject) private Mode: TBRegExpMode; + pTargetString: PChar; pBRegExp: PTBRegExpRec; + function GetMatchPos: Integer; + function GetMatchLength: Integer; function GetSplitCount: Integer; function GetSplitStrings(index: Integer): string; function GetMatchStrings(index:Integer): string; function GetMatchCount: Integer; function GetCount: Integer; function GetStrings(index: Integer): string; + function GetLastCommand: string; + procedure CheckCommand(const Command: string); public destructor Destroy; override; public function Match(const Command, TargetString: string): Boolean; - function Subst(const Command: string;var TargetString: string): Boolean; + function Subst(const Command: string; var TargetString: string): Boolean; function Split(const Command, TargetString: string; Limit: Integer): Boolean; function Trans(const Command: string;var TargetString: string): Boolean; + property LastCommand: string read GetLastCommand; + property MatchPos: Integer read GetMatchPos; + property MatchLength: Integer read GetMatchLength; property Count: Integer read GetCount; property Strings[index: Integer]: string read GetStrings; default; end; //===================================================================== +// Ž©“®“I‚ÉŽÀ‘̉»A”jŠü‚³‚ê‚郆[ƒeƒBƒŠƒeƒBƒCƒ“ƒXƒ^ƒ“ƒX +//===================================================================== + +function brx: TBRegExp; + +//===================================================================== implementation //===================================================================== +var fbrx: TBRegExp; +function brx: TBRegExp; +begin + if fbrx=nil then fbrx:=TBRegExp.Create; + Result:=fbrx; +end; + +//===================================================================== + destructor TBRegExp.Destroy; begin if pBRegExp<>nil then @@ -115,31 +145,87 @@ begin end; //===================================================================== +// ‘O‰ñ‚̃Rƒ}ƒ“ƒh•¶Žš—ñ‚ð•Ô‚· + +function TBRegExp.GetLastCommand: string; +var len: Integer; +begin + if pBRegExp=nil then begin + Result:= ''; + end else begin + len:= Integer(pBRegExp^.paraendp)-Integer(pBRegExp^.parap); + SetLength(Result, len); + Move(pBRegExp^.parap^, Result[1], len); + end; +end; + +//===================================================================== +// ‘O‰ñ‚ƈقȂéƒRƒ}ƒ“ƒh‚Å‚ ‚ê‚΃LƒƒƒbƒVƒ…‚ðƒNƒŠƒA‚·‚é“à•”Žè‘±‚« + +procedure TBRegExp.CheckCommand(const Command: string); +var p,q: PChar; +begin + if pBRegExp=nil then Exit; + p:= pBRegExp.parap - 1; + q:= PChar(@Command[1]) - 1; + repeat + Inc(p); + Inc(q); + if p^<>q^ then begin + BRegFree(pBRegExp); + pBRegExp:= nil; + Break; + end; + until p^=#0; +end; + +//===================================================================== function TBRegExp.Match(const Command, TargetString: string): Boolean; var ErrorString: string; + i: Integer; begin - SetLength(ErrorString,256); + CheckCommand(Command); + SetLength(ErrorString, BREGEXP_ERROR_MAX); Mode:=brxNone; - Result:=BMatch( - PChar(Command), - PChar(TargetString), - PChar(TargetString)+Length(TargetString), - pBRegExp, - PChar(ErrorString)); - SetLength(ErrorString,StrLen(PChar(ErrorString))); + if TargetString='' then begin // ƒGƒ‰[‰ñ”ð + i:=0; + Result:=BMatch( + PChar(Command), + PChar(@i), + PChar(@i)+1, + pBRegExp, + PChar(ErrorString)); + end else begin + Result:=BMatch( + PChar(Command), + PChar(TargetString), + PChar(TargetString)+Length(TargetString), + pBRegExp, + PChar(ErrorString)); + end; + SetLength(ErrorString, StrLen(PChar(ErrorString))); if ErrorString<>'' then raise EBRegExpError.Create(ErrorString); - Mode:=brxMatch; + if Result then Mode:= brxMatch; + pTargetString:= PChar(TargetString); end; //===================================================================== function TBRegExp.Subst(const Command: string; var TargetString: string): Boolean; +const TextBuffer: string=''; var ErrorString: string; + ep,sp: PPChar; + i: Integer; begin - SetLength(ErrorString,256); + CheckCommand(Command); + Result:=False; + if TargetString='' then Exit; + TextBuffer:= TargetString; // ( ) ‚𐳂µ‚­•Ô‚·‚½‚߂ɃeƒLƒXƒg‚ð•Û‘¶‚·‚é + UniqueString(TextBuffer); + SetLength(ErrorString, BREGEXP_ERROR_MAX); Mode:=brxNone; Result:=BSubst( PChar(Command), @@ -148,9 +234,21 @@ begin pBRegExp, PChar(ErrorString)); SetLength(ErrorString,StrLen(PChar(ErrorString))); - if ErrorString<>'' then + if ErrorString<>'' then raise EBRegExpError.Create(ErrorString); - if Result then TargetString:=pBRegExp^.outp; + + if Result then begin // ( ) ‚ÌŒ‹‰Ê‚𐳂µ‚­•Ô‚·‚½‚ß + sp:=pBRegExp^.startp; + ep:=pBRegExp^.endp; + for i:=0 to GetMatchCount-1 do begin + Inc(ep^, Integer(TextBuffer)-Integer(TargetString)); + Inc(sp^, Integer(TextBuffer)-Integer(TargetString)); + Inc(sp); + Inc(ep); + end; + TargetString:= pBRegExp^.outp; + Mode:=brxMatch; + end; end; //===================================================================== @@ -159,8 +257,11 @@ function TBRegExp.Trans(const Command: string; var TargetString: string): Boolean; var ErrorString: string; begin - SetLength(ErrorString,256); + CheckCommand(Command); Mode:=brxNone; + if TargetString='' then // ƒGƒ‰[‰ñ”ð + TargetString:= #0; + SetLength(ErrorString, BREGEXP_ERROR_MAX); Result:=BTrans( PChar(Command), PChar(TargetString), @@ -178,16 +279,29 @@ end; function TBRegExp.Split(const Command, TargetString: string; Limit: Integer): Boolean; var ErrorString: string; + t: string; begin - SetLength(ErrorString,256); + CheckCommand(Command); + SetLength(ErrorString, BREGEXP_ERROR_MAX); Mode:=brxNone; - Result:=BSplit( - PChar(Command), - PChar(TargetString), - PChar(TargetString)+Length(TargetString), - Limit, - pBRegExp, - PChar(ErrorString)); + if TargetString='' then begin // ƒGƒ‰[‰ñ”ð + t:= #0; + Result:=BSplit( + PChar(Command), + PChar(t), + PChar(t)+1, + Limit, + pBRegExp, + PChar(ErrorString)); + end else begin + Result:=BSplit( + PChar(Command), + PChar(TargetString), + PChar(TargetString)+Length(TargetString), + Limit, + pBRegExp, + PChar(ErrorString)); + end; SetLength(ErrorString,StrLen(PChar(ErrorString))); if ErrorString<>'' then raise EBRegExpError.Create(ErrorString); @@ -196,6 +310,24 @@ end; //===================================================================== +function TBRegExp.GetMatchPos: Integer; +begin + if Mode<>brxMatch then + raise EBRegExpError.Create('no match pos'); + Result:=Integer(pBRegExp.startp^)-Integer(pTargetString)+1; +end; + +//===================================================================== + +function TBRegExp.GetMatchLength: Integer; +begin + if Mode<>brxMatch then + raise EBRegExpError.Create('no match length'); + Result:=Integer(pBRegExp.endp^)-Integer(pBRegExp.startp^); +end; + +//===================================================================== + function TBRegExp.GetCount: Integer; begin Result:=0; @@ -209,19 +341,15 @@ begin end; end; +//===================================================================== + function TBRegExp.GetMatchCount: Integer; -var sp,ep: PPChar; begin - Result:=0; - sp:=pBRegExp^.startp; - ep:=pBRegExp^.endp; - while Integer(sp^)0 do begin - if Integer(sp^)>=Integer(ep^) then - raise EBRegExpError.Create('index out of range'); - Inc(sp); - Inc(ep); - Dec(index); - end; + if (index<0) or (index>=GetMatchCount) then + raise EBRegExpError.Create('index out of range'); + sp:=pBRegExp^.startp; Inc(sp, index); + ep:=pBRegExp^.endp; Inc(ep, index); SetLength(Result,Integer(ep^)-Integer(sp^)); Move(sp^^,PChar(Result)^,Integer(ep^)-Integer(sp^)); end; +//===================================================================== + function TBRegExp.GetSplitStrings(index:Integer): string; var p: PPChar; sp,ep: PChar; @@ -266,15 +393,16 @@ begin if (index<0) or (index>=GetSplitCount) then raise EBRegExpError.Create('index out of range'); p:=pBRegExp^.splitp; - Inc(p,index*2); - sp:=p^; - Inc(p); - ep:=p^; + Inc(p,index*2); sp:=p^; + Inc(p); ep:=p^; SetLength(Result,Integer(ep)-Integer(sp)); Move(sp^,PChar(Result)^,Integer(ep)-Integer(sp)); end; //===================================================================== +initialization +finalization + fbrx.Free; end. diff --git a/bottleclient/LogForm.pas b/bottleclient/LogForm.pas index f3dd8f1..67c0d9f 100755 --- a/bottleclient/LogForm.pas +++ b/bottleclient/LogForm.pas @@ -1672,7 +1672,8 @@ begin if not RegExp.Match(Condition.ScriptPattern, Bottle.Script) then Ok := false; except - on EBRegExpError do ; // do nothing + on EBRegExpError do + Ok := false; //–­‚Ȑ³‹K•\Œ»‚ðR‚é end; end else begin -- 2.11.0