saoren最后的GetBewteenString函数好像有点问题,不能达到目的。
我刚好也要用到,修改如下:
function GetBewteenString(const TextStr, FirstSubStr, LastSubStr: string
IgnoreCase: Boolean
List: TStrings): Integer;
var
Text: PByte;
TextLen: Integer;
FirstBuffer: array [0..MAX_CHAR - 1] of Integer;
LastBuffer: array [0..MAX_CHAR - 1] of Integer;
function FindMatchIndex(const Sub: PByte;
Buffer: array of Integer
SubLen, CurrPos: Integer): Integer;
var
I, J: Integer;
begin
Result := -1;
while CurrPos < TextLen do
begin
I := CurrPos;
J := SubLen - 1;
while (J >= 0) and
((PByteArr(Text)^ = PByteArr(Sub)^[J]) or
(IgnoreCase and (UpCase(PCharArr(Text)^) = PCharArr(Sub)^[J]))) do
begin
Dec(J);
Dec(I);
end;
if -1 = J then
begin
Result := CurrPos - SubLen + 2;
break;
end else
begin
if IgnoreCase then
Inc(CurrPos, Buffer[Byte(UpCase(PCharArr(Text)^[CurrPos]))])
else
Inc(CurrPos, Buffer[PByteArr(Text)^[CurrPos]]);
end;
end;
end;
var
FirstSub, LastSub: PByte;
I, CurrPos, LastPos, FirstPos, FirstLen, LastLen: Integer;
begin
Result := 0;
List.Clear
FirstLen := Length(FirstSubStr);
LastLen := Length(LastSubStr);
if (FirstLen = 0) or (LastLen = 0) then Exit;
TextLen := Length(TextStr);
FirstSub := @FirstSubStr[1];
LastSub := @LastSubStr[1];
Text := @TextStr[1];
if IgnoreCase then
begin
GetMem(FirstSub, FirstLen);
Move(FirstSubStr[1], FirstSub^, FirstLen);
FirstSub := PByte(StrUpper(PChar(FirstSub)));
GetMem(LastSub, LastLen);
Move(LastSubStr[1], LastSub^, LastLen);
LastSub := PByte(StrUpper(PChar(LastSub)));
end;
for I := 0 to MAX_CHAR - 1 do
begin
FirstBuffer := FirstLen;
LastBuffer := LastLen;
end;
for I := 0 to FirstLen - 2 do
FirstBuffer[PByteArr(FirstSub)^] := FirstLen - I - 1;
for I := 0 to LastLen - 2 do
LastBuffer[PByteArr(LastSub)^] := LastLen - I - 1;
List.BeginUpdate;
CurrPos := FirstLen;
try
while CurrPos < TextLen do
begin
FirstPos := FindMatchIndex(FirstSub, FirstBuffer, FirstLen, CurrPos-1);
if FirstPos <> -1 then
begin
LastPos := FindMatchIndex(LastSub, LastBuffer, LastLen, FirstPos + FirstLen);
if LastPos <> -1 then
begin
Inc(Result);
List.Add(Copy(TextStr, FirstPos + FirstLen, LastPos - FirstPos - FirstLen));
CurrPos := LastPos + LastLen;
end else
begin
if IgnoreCase then
Inc(CurrPos, FirstBuffer[Byte(UpCase(PCharArr(Text)^[CurrPos]))])
else
Inc(CurrPos, FirstBuffer[PByteArr(Text)^[CurrPos]]);
end;
end else
break
//如果没有找到FirstSub,那退出.
end;
finally
List.EndUpdate;
if IgnoreCase then
begin
FreeMem(FirstSub);
FreeMem(LastSub);
end;
end;
end
**************
以下部分变动了:
CurrPos := FirstLen;
try
while CurrPos < TextLen do
begin
FirstPos := FindMatchIndex(FirstSub, FirstBuffer, FirstLen, CurrPos-1);
if FirstPos <> -1 then
begin
LastPos := FindMatchIndex(LastSub, LastBuffer, LastLen, FirstPos + FirstLen);