如何建立一个const数组? [英] How do I build an array of const?

查看:222
本文介绍了如何建立一个const数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现解释器,而我的解释器将支持的功能之一就是Delphi的 Format 。实际上,我正在使用 SysUtils.Format 来实现我的功能。但是,我无法为函数建立第二个参数,即TVarRec的数组

I am implementing an interpreter, and one of the functions my interpreter will support is like Delphi's Format. In fact, I'm implementing my function using SysUtils.Format. However, I'm having trouble building the second parameter to the function, the array of TVarRec.

假设我具有以下代码。现在,我只是假设解释的代码需要访问哪个Delphi变量( iVar1 iVar2 ),但是我仍然不知道如何将它们放入 Format 所需的结构( arFormatArgs )。

Suppose I have the following code. For now, I just assume which Delphi variables the interpreted code will need access to (iVar1 and iVar2), but I still don't know how to put them into the structure that Format requires (arFormatArgs).

type TFormatArgs = array of TVarRec;

procedure RunInterpretedFormatFunction;
var
  iMyAge: integer;
  iMyIQ: integer;
  sCode: string;
  sText: string;
begin
  iMyAge := 5;
  iMyIQ := -5;
  sCode := 'Format(''My age is %d and my IQ is %d'', [iMyAge, iMyIQ])';
  sText := FormatThis(sCode, iMyAge, iMyIQ);
end;

function FormatThis(sFormatCode: string; iVar1: integer; iVar2: integer): string;
var
  sFormatString: string;
  arFormatArgs: TFormatArgs;
begin
  sFormatString := GetFormatString(sFormatCode); // I can implement this function
  arFormatArgs := ConstructFormatArgs(iVar1, iVar2); // NEED HELP HERE!
  result := SysUtils.Format(sFormatString, arFormatArgs);
end;

我如何在 ConstructFormatArgs 函数中实现德尔福(不是汇编程序)?

How can I implement my ConstructFormatArgs function in Delphi (not Assembly)?

推荐答案

const数组使您可以自由添加字符串,整数,浮点数等等将它们格式化为字符串。

The array of const gives you the freedom to add strings, integers, floats and so on and having these formatted into a string. And there is no limit to how many items you can add.

Delphi处理此问题的方式是,const数组实际上是TVarRec的数组。

The way Delphi deals with this issue is that the array of const really is a array of TVarRec's.

TVarRec是以下类型的记录:

A TVarRec is a record of the following type:

TVarRec = record
  case Byte of
    vtInteger:    (VInteger: Integer; VType: Byte);
    vtBoolean:    (VBoolean: Boolean);
    vtChar:       (VChar: Char);
    vtExtended:   (VExtended: PExtended);
    vtString:     (VString: PShortString);
    vtPointer:    (VPointer: Pointer);
    vtPChar:      (VPChar: PChar);
    vtObject:     (VObject: TObject);
    vtClass:      (VClass: TClass);
    vtWideChar:   (VWideChar: WideChar);
    vtPWideChar:  (VPWideChar: PWideChar);
    vtAnsiString: (VAnsiString: Pointer);
    vtCurrency:   (VCurrency: PCurrency);
    vtVariant:    (VVariant: PVariant);

TVarRec 内的值的类型由 VType 值确定。

The type of the value inside the TVarRec is determined by the VType value.

这使您可以灵活地将所需的任何一种添加到数组中const,就像在Format()函数中一样:

This gives you the flexibility to add either type you wish to the array of const, like in the Format() function:

Format('%s是一个字符串,%d是一个整数',['string',10]);

Format( '%s is a string, %d is an integer', ['string',10] );

在您自己的过程中使用const数组没什么大不了的。看一下这个例子:

Using the array of const in your own procedure is no big deal. Take a look at this example:

 procedure VarArraySample( AVarArray : array of const );
  var
    i : integer;
  begin
    for i := 0 to High(AVarArray) do
      do_something;
  end;

函数High()返回数组的最后一个索引。

The function High() returns the last index of the array.

您还可以转换TVarRec的内容。此示例摘自Delphi在线帮助,并进行了一些改进。函数将TVarRec转换为字符串:

You can also convert the contents of the TVarRec. This example is taken from the Delphi on-line help and revamped a bit. The function converts a TVarRec to a string:

function VarRecToStr( AVarRec : TVarRec ) : string;
  const
    Bool : array[Boolean] of string = ('False', 'True');
  begin
    case AVarRec.VType of
      vtInteger:    Result := IntToStr(AVarRec.VInteger);
      vtBoolean:    Result := Bool[AVarRec.VBoolean];
      vtChar:       Result := AVarRec.VChar;
      vtExtended:   Result := FloatToStr(AVarRec.VExtended^);
      vtString:     Result := AVarRec.VString^;
      vtPChar:      Result := AVarRec.VPChar;
      vtObject:     Result := AVarRec.VObject.ClassName;
      vtClass:      Result := AVarRec.VClass.ClassName;
      vtAnsiString: Result := string(AVarRec.VAnsiString);
      vtCurrency:   Result := CurrToStr(AVarRec.VCurrency^);
      vtVariant:    Result := string(AVarRec.VVariant^);
    else
      result := '';
    end;
  end;

您可以将上面的两个函数组合为一个函数,该函数将const数组中的所有元素转换为一个字符串:

You can combine the two functions above to one function that converts all elements in the array of const into one string:

function VarArrayToStr( AVarArray : array of const ) : string;
  var
    i : integer;
  begin
    result := '';
    for i := 0 to High(AVarArray) do
      result := result + VarRecToStr( AVarArray[i] );
  end;

您现在可以创建自己的Format()函数。
Format()函数扫描%,并将%something替换为const数组中的值,具体取决于格式说明符和精度说明符。

you will now be able to create your own Format() function. The Format() function scans for %'s and replaces the %something with the value in the array of const, depending on the format specifiers and precision specifiers.

这篇关于如何建立一个const数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆