How do I build an array of const?

前端 未结 3 1599
庸人自扰
庸人自扰 2020-12-18 01:43

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

3条回答
  •  无人及你
    2020-12-18 02:25

    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.

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

    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);
    

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

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

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

    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;
    

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

    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;
    

    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;
    

    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.

提交回复
热议问题