unit ddjb2;

{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}

interface

function GetDjb2(Key, KeyEnd: Pointer): UInt32; inline; overload;
function GetDjb2(Key: Pointer; Len: SizeUInt): UInt32; inline; overload;

implementation

function _djb2(Key, KeyEnd: PAnsiChar; Hash: UInt32): UInt32;
begin
  while Key < KeyEnd do begin
    Hash := ((Hash shl 5) + Hash) + Ord(Key^); // hash * 33 + c
    Inc(Key);
  end;
  Exit(Hash);
end;

function GetDjb2(Key, KeyEnd: Pointer): UInt32;
begin
  Exit(_djb2(Key, KeyEnd, 5381));
end;

function GetDjb2(Key: Pointer; Len: SizeUInt): UInt32;
begin
  Exit(_djb2(Key, Key + Len, 5381));
end;

end.
