You are not logged in.
Pages: 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.
Offline
Thanks for the input!
I am not sure that it will be much faster than the existing IPToCardinal().
And our version seems safer, since it makes more checks than yours.
For instance, it will ensure that the values are really bytes: yours will parse with no problem an invalid '300.300.300.300' text and return (I guess) something rounded like 44.44.44.44 as resulting IP.
Some error checks are also lacking: I guess '1.2' will be found out as correct, and read out of the input string memory.
In fact, IPToCardinal() is not used much in the framework (only when some IPs are registered to TIPBan), so we don't need replace it.
Online
Pages: 1