delphi - Delphi中是否有*SysUtils.Format*的反函数

标签 delphi function scanf

有人为 Delphi 编写过“UnFormat”例程吗?

我想象的是SysUtils.Format,看起来像这样

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

因此,您可以使用格式字符串将字符串解压缩为一系列变量。

我看过 SysUtils 中的“Format”例程,但我从未使用过汇编,所以它对我来说毫无意义。

最佳答案

这在 C 中称为 scanf,我为此制作了一个类似的 Delphi :

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 - Delphi中是否有*SysUtils.Format*的反函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72672/

相关文章:

multithreading - 如何使用 Indy 建立双向 TCP 连接?

delphi - 我需要在 Delphi 中完成记录数组吗?

delphi - IdSSLIOHandlerSocketOpenSSL 是否支持使用算法 SHA2-2048 的 SSL?

javascript - 是否可以在页面加载后更改 css 样式表?

javascript - TypeScript:初始化嵌套对象的更简单方法?

将字符串转换为十六进制

c# - 寻找与 scanf 等效的 C#

delphi - DeviceIoControl - GetLastError : ERROR_NOACCESS - 998

c - sscanf 不兼容的指针类型

javascript - 如何在 Javascript 中向 .submit() 添加回调函数?