zoukankan      html  css  js  c++  java
  • 字符串通信协议解析函数

    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.

  • 相关阅读:
    es6新增特性,数组的操作方法
    导航钩子有哪几种,如何将数据传入下一个点击的路由页面
    父组件如何获取子组件数据,子组件如何获取父组件数据,父子组件如何传值
    watch和computed的区别
    vue页面中定时器的使用
    table表格,顶端对齐
    计算机组成原理8-FPGA、ASIC、TPU、虚拟机
    vue中使用socket连接后台
    计算机组成原理7-CISC和RISC、GPU
    计算机组成原理6-流水线、多发射和超标量、SIMD、异常
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/4889016.html
Copyright © 2011-2022 走看看