function IsAnagram(const AValue1, AValue2: String; AIgnoreSpaces, AExceptionOnError: Boolean): Boolean;
const
Primes: array[Char] of Integer = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307,
311, 313, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
241, 251, 257, 263, 269, 271, 277, 317, 331, 337, 347, 349, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
procedure Error(const AName: String; APosition: Integer); overload;
begin
Raise ERangeError.CreateFmt('IsAnagram: illegal character in ' + AName + ' at position %d', [APosition]);
end;
function Process(const AName, AValue: String): QWord; inline;
var
I: Integer;
begin
Result := 1;
for I := 1 to Length(AValue) do
Result *= Primes[AValue[I]];
if Result = 0 then
if AExceptionOnError then
for I := 1 to Length(AValue) do
if Primes[AValue[I]] = 0 then
Error(AName, I);
end;
begin
if (not AIgnoreSpaces) and (not AExceptionOnError) and (Length(AValue1) <> Length(AValue2)) then
Exit(False);
Primes[' '] := specialize IfThen<Integer>(AIgnoreSpaces, 1, 2);
Result := Process('S1', AValue1) = Process('S2', AValue2);
end;