本文介绍了在Delphi中是否有* SysUtils.Format *的逆函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人为Delphi编写了UnFormat例程?



我想象的是SysUtils的反向。格式,看起来像这样



UnFormat('一个数字%n和另一个%n',[float1,float2]);



所以你可以使用格式字符串将一个字符串解压缩成一系列变量。



我看过SysUtils中的格式例程,但是我从来没有使用程序集,因此对我来说没有意义。

解决方案

scanf在C中,我已经为此做了一个Delphi样式:

  function ScanFormat(const Input,Format: string; Args:指针数组):Integer; 
var
InputOffset:Integer;
FormatOffset:Integer;
InputChar:Char;
FormatChar:Char;

函数_GetInputChar:Char;
begin
if InputOffset begin
结果:= Input [InputOffset];
Inc(InputOffset);
end
else
结果:=#0;
结束

函数_PeekFormatChar:Char;
begin
如果FormatOffset< =长度(格式)然后
结果:=格式[FormatOffset]
else
结果:=#0;
结束

函数_GetFormatChar:Char;
begin
结果:= _PeekFormatChar;
如果Result<> #0 then
Inc(FormatOffset);
结束

函数_ScanInputString(const Arg:Pointer = nil):string;
var
EndChar:Char;
begin
结果:='';
EndChar:= _PeekFormatChar;
InputChar:= _GetInputChar;
while(InputChar>'')
和(InputChar&EndCar)执行
begin
结果:= Result + InputChar;
InputChar:= _GetInputChar;
结束

如果InputChar<> #0 then
Dec(InputOffset);

如果分配(Arg)然后
PString(Arg)^:= Result;
结束

函数_ScanInputInteger(const Arg:Pointer):Boolean;
var
值:string;
begin
值:= _ScanInputString;
结果:= TryStrToInt(Value,{out} PInteger(Arg)^);
结束

procedure _Raise;
begin
raise EConvertError.CreateFmt('Unknown ScanFormat character:%s!',[FormatChar]);
结束

begin
结果:= 0;
InputOffset:= 1;
FormatOffset:= 1;
FormatChar:= _GetFormatChar;
,而FormatChar<> #0 do
begin
如果FormatChar<> '%'then
begin
InputChar:= _GetInputChar;
if(InputChar =#0)
或(FormatChar<> InputChar)然后
退出;
end
else
begin
FormatChar:= _GetFormatChar;
案例FormatChar为
'%':
如果_GetInputChar<> '%'然后
退出;
's':
begin
_ScanInputString(Args [Result]);
Inc(Result);
结束
'd','u':
begin
如果不是_ScanInputInteger(Args [Result])然后
退出;

Inc(Result);
结束
else
_Raise;
结束
结束

FormatChar:= _GetFormatChar;
结束
结束


Has anyone written an 'UnFormat' routine for Delphi?

What I'm imagining is the inverse of SysUtils.Format and looks something like this

UnFormat('a number %n and another %n',[float1, float2]);

So you could unpack a string into a series of variables using format strings.

I've looked at the 'Format' routine in SysUtils, but I've never used assembly so it is meaningless to me.

解决方案

This is called scanf in C, I've made a Delphi look-a-like for this :

function ScanFormat(const Input, Format: string; Args: array of Pointer): Integer;
var
  InputOffset: Integer;
  FormatOffset: Integer;
  InputChar: Char;
  FormatChar: Char;

  function _GetInputChar: Char;
  begin
    if InputOffset <= Length(Input) then
    begin
      Result := Input[InputOffset];
      Inc(InputOffset);
    end
    else
      Result := #0;
  end;

  function _PeekFormatChar: Char;
  begin
    if FormatOffset <= Length(Format) then
      Result := Format[FormatOffset]
    else
      Result := #0;
  end;

  function _GetFormatChar: Char;
  begin
    Result := _PeekFormatChar;
    if Result <> #0 then
      Inc(FormatOffset);
  end;

  function _ScanInputString(const Arg: Pointer = nil): string;
  var
    EndChar: Char;
  begin
    Result := '';
    EndChar := _PeekFormatChar;
    InputChar := _GetInputChar;
    while (InputChar > ' ')
      and (InputChar <> EndChar) do
    begin
      Result := Result + InputChar;
      InputChar := _GetInputChar;
    end;

    if InputChar <> #0 then
      Dec(InputOffset);

    if Assigned(Arg) then
      PString(Arg)^ := Result;
  end;

  function _ScanInputInteger(const Arg: Pointer): Boolean;
  var
    Value: string;
  begin
    Value := _ScanInputString;
    Result := TryStrToInt(Value, {out} PInteger(Arg)^);
  end;

  procedure _Raise;
  begin
    raise EConvertError.CreateFmt('Unknown ScanFormat character : "%s"!', [FormatChar]);
  end;

begin
  Result := 0;
  InputOffset := 1;
  FormatOffset := 1;
  FormatChar := _GetFormatChar;
  while FormatChar <> #0 do
  begin
    if FormatChar <> '%' then
    begin
      InputChar := _GetInputChar;
      if (InputChar = #0)
      or (FormatChar <> InputChar) then
        Exit;
    end
    else
    begin
      FormatChar := _GetFormatChar;
      case FormatChar of
        '%':
          if _GetInputChar <> '%' then
            Exit;
        's':
          begin
            _ScanInputString(Args[Result]);
            Inc(Result);
          end;
        'd', 'u':
          begin
            if not _ScanInputInteger(Args[Result]) then
              Exit;

            Inc(Result);
          end;
      else
        _Raise;
      end;
    end;

    FormatChar := _GetFormatChar;
  end;
end;

这篇关于在Delphi中是否有* SysUtils.Format *的逆函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:39