在流中查找任意字串

来源:互联网 发布:测谎仪软件 编辑:程序博客网 时间:2024/06/15 02:30
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

开发中有时需要在流中查找某个字串,这些字串有可能是ASCII表后半部分或混杂.但是网上提供的例程效果不佳而且代码冗长.

后来自己抽个时间写了一个,代码极少,效率很高.仅供各位参考:


function ScanStream(T:Tstream;S:String):integer;
var i,j:integer;
    p:Pchar;
begin
  getMem(p,T.size);//分配内存
  T.ReadBuffer(p^,t.Size );//读

  for i:=0 to T.Size -1 do begin
    for j:=1 to length(S) do
      if p[i+j]<>S[j] then break;//有一个不同即退出
    if j>length(S) then begin //依据
      result:=i+1;
      break; //完成
    end;
  end;
  FreeMem(p);
end;

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>