字符串通信协议解析函数
unit HUtil32;
//提供⼤量的辅助函数
{$WARNINGS OFF}
interface
uses
Classes, SysUtils, WinTypes, WinProcs, Graphics, Messages, Dialogs;
type
Str4096 = array [0..4096] of AnsiChar;
Str256 = array [0..256] of AnsiChar;
//名称、值对照表
TyNameTable = record
Name: string;
varl: Longint;
end;
TLRect = record
Left, Top, Right, Bottom: Longint;
end;
// 取 SearchAfter, ArrestBefore之间的字符串
function ArrestStringEx (Source, SearchAfter, ArrestBefore: AnsiString; var ArrestStr: AnsiString): AnsiString; // 获取指定字符串中以指定分割符结尾的⼦串
function GetValidStr3 (Str: AnsiString; var Dest: AnsiString; const Divider: array of AnsiChar): AnsiString; implementation
function ArrestStringEx (Source, SearchAfter, ArrestBefore: AnsiString; var ArrestStr: AnsiString): AnsiString; var
SrcLen: integer;
GoodData: Boolean;
i, n: integer;
begin
ArrestStr := ''; {result string}
if Source = '' then begin
Result := '';
exit;
end;
try
SrcLen := Length (Source);
GoodData := FALSE;
if SrcLen >= 2 then
if Source[1] = SearchAfter then begin
Source := Copy (Source, 2, SrcLen-1);
SrcLen := Length (Source);
GoodData := TRUE;
end else begin
n := Pos (SearchAfter, Source);
if n > 0 then begin
Source := Copy (Source, n+1, SrcLen-(n));
SrcLen := Length(Source);
GoodData := TRUE;
end;
end;
字符串函数详解if GoodData then begin
n := Pos (ArrestBefore, Source);
if n > 0 then begin
ArrestStr := Copy (Source, 1, n-1);
Result := Copy (Source, n+1, SrcLen-n);
end else begin
Result := SearchAfter + Source;
end;
end else begin
for i:=1 to SrcLen do begin
if Source[i] = SearchAfter then begin
Result := Copy (Source, i, SrcLen-i+1);
break;
end;
end;
end;
except
ArrestStr := '';
Result := '';
end;
end;
//获取指定字符串中以指定分割符结尾的⼦串
function GetValidStr3 (Str: AnsiString; var Dest: AnsiString; const Divider: array of AnsiChar): AnsiString; const
BUF_SIZE = 20480; //$7FFF;
var
Buf: array[0..BUF_SIZE] of AnsiChar;
BufCount, Count, SrcLen, I, ArrCount: Longint;
Ch: AnsiChar;
label
CATCH_DIV;
begin
try
SrcLen := Length(Str);
BufCount := 0;
Count := 1;
if SrcLen >= BUF_SIZE-1 then begin
Result := '';
Dest := '';
exit;
end;
if Str = '' then begin
Dest := '';
Result := Str;
exit;
end;
ArrCount := sizeof(Divider) div sizeof(AnsiChar);
while TRUE do begin
if Count <= SrcLen then begin
Ch := Str[Count];
for I:=0 to ArrCount- 1 do
if Ch = Divider[I] then
goto CATCH_DIV;
end;
if (Count > SrcLen) then begin
CATCH_DIV:
if (BufCount > 0) then begin
if BufCount < BUF_SIZE-1 then begin
Buf[BufCount] := #0;
Dest := string (Buf);
Result := Copy (Str, Count+1, SrcLen-Count);
end;
break;
end else begin
if (Count > SrcLen) then begin
Dest := '';
Result := Copy (Str, Count+2, SrcLen-1);
break;
end;
end;
end else begin
if BufCount < BUF_SIZE-1 then begin
Buf[BufCount] := Ch;
Inc (BufCount);
end;
end;
Inc (Count); end;
except
Dest := '';
Result := ''; end;
end;
end.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论