有点奇怪,我在Google中查找"Delphi 字符串 Hash"居然找不到相关的Hash函数!正当我不知如何是好是,忽然想起开源的JEDI项目,这个大而全的项目虽然我极少使用,但我知道里面什么都有的。一看果然……
下面是我从JEDI里面提取出来的Hash函数:
function StrHash(const SoureStr: string): Cardinal;
const
cLongBits = 32;
cOneEight = 4;
cThreeFourths = 24;
cHighBits = $F0000000;
var
I: Integer;
P: PChar;
Temp: Cardinal;
begin
Result := 0;
P := PChar(SoureStr);
I := Length(SoureStr);
while I > 0 do
begin
Result := (Result shl cOneEight) + Ord(P^);
Temp := Result and cHighBits;
if Temp <> 0 then
Result := (Result xor (Temp shr cThreeFourths)) and (not cHighBits);
Dec(I);
Inc(P);
end;
end;
end.