delphi XE7 数组操作中缺少的find(POS)功能

delphi xe7 中对数组操作做了很多扩充,比如加入了类似字符串处理的功能。

例如,数组相加

var
  A: array of integer;
  B: TBytes = [1,2,3,4]; //Initialization can be done from declaration
begin
  ...
  A:=[1,2,3]; // assignation using constant array
  A:=A+[4,5]; // addition - A will become [1,2,3,4,5]
  ...
end;

 数组插入

var
  A: array of integer;
begin
  ...
  A:=[1,2,3,4];
  Insert(5,A,2); // A will become [1,2,5,3,4]
  ...
end;

 数组删除

var
  A: array of integer;
begin
  ...
  A:=[1,2,3,4];
  Delete(A,1,2); //A will become [1,4]
  ...
end;

数组连接

 A := Concat([1,2,3],[4,5,6]); //A will become [1,2,3,4,5,6]

为什么在xe7 中要对数组做这么大的变化呢,当然首先肯定是方便数组编程,其实更深层的原因是因为ansistring 在移动平台上的缺失,

很多过去的代码,由于都是把byte 当作ansichar 处理的,到了移动平台上,这些代码都跑不起来了。而且很难改造。

那么只有使用Tbytes 里替换传统的ansistring. 因此对数组操作增加了这么多方法来解决这个传统问题。

那现在问题来了,传统的pos  功能却没加入,导致大量的是使用pos 的操作无法改造。

不知道会在xe? 里面加入?现在临时的办法就是自己做一个find(pos)函数来解决这个问题。

为了不与以后的pos 冲突,函数名就叫find, 功能是在一个数组里面查找另一个数组,并返回位置。

function Find(const sub, Buffer:TBytes): Integer;
var
  N: Integer;
begin


  N := Length(sub);
  if N>0 then
    for Result := low(Buffer) to high(Buffer)-(N-1) do
      if CompareMem(@Buffer[Result], sub, N) then
        exit;
  Result := -1;
end;

 这样就可以用这个替换原来的ansistring 的pos 操作了。

 

你可能感兴趣的:(Delphi)