#1 mORMot 1 » function StringToIPv4 » 2019-12-16 19:29:46

bejoy
Replies: 1

Hello.
It seems that my version of StringToIPv4 conversion should be much faster than existed IPToCardinal.
Moreover, it can operate with standard strings like "192.168.1.2" and strings with extra whitespaces like "192.168.  1.  2";

function StrToIPv4( const AValue: AnsiString; var IPv4: UInt32 ): Boolean; inline;
var
  O, I, C, V: Byte;
  Data: array[0..3] of Byte absolute IPv4;
begin
  // * Inital result
  Result := False;

  // * Reset main iterator
  I := 0;
  // * Walk IPv4 octets
  for O := 3 downto 0 do
  begin
    // * Octet value
    V := 0;
    // * Octet loop
    while True do
    begin
      // * Increase character iterator
      Inc( I );
      // * Get character
      C := Byte( AValue[I] );

      // * If NULL or dot [.]
      if ( C = 0 ) or ( C = 46 ) then Break;
      // * If space [ ]
      if ( C = 32 ) then Continue;

      // * If digit
      if ( C >= 48 ) and ( C <= 57 ) then
        // * Calculate octet value
        V := V * 10 + ( C - 48 )
      else
        // * Exit function
        Exit;

    end;
    // * Write octet value
    Data[O] := V;
  end;

  // * Assign result
  Result := True;
end;

So what do you think about?
P.S. This is my code, so it can be used freely.

Board footer

Powered by FluxBB