unit josh_unit;
//{$define OnlyAlphaNumericChars}// a-z,A-Z,' ',0--9
{$mode ObjFPC}{$H+}
interface
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils;
const
josh_squote=chr(39);
josh_Arr_Start=Byte(32);
josh_Arr_End=Byte(127);
josh_mess_s1='Invalid character in input string at position %d in s1';
josh_mess_s2='Invalid character in input string at position %d in s2';
Var
josh_Map: array[josh_Arr_Start..josh_Arr_End] of Byte;
josh_loop:Int32;
josh_FreqArray: array[josh_Arr_Start..josh_Arr_End] of Int32;
function IsAnAnagramJosh(const s1, s2: AnsiString; IgnoreSpaces: Boolean; ExceptionOnError: Boolean = False): boolean; inline;
implementation
function IsAnAnagramJosh(const s1, s2: AnsiString; IgnoreSpaces: Boolean; ExceptionOnError: Boolean = False): boolean; inline;
var
k, l1, l2: Int32;
v:byte;
begin
{$define UseInc}
l1:=Length(s1);
l2:=Length(s2);
if (l1 = 0) or (l2 = 0) then Exit(False);
If Not ExceptionOnError then
begin
for k:=1 to l1 do
begin
v:=Byte(s1[k]);
if v in [josh_Arr_Start..josh_Arr_End] then {$ifdef useinc}inc(josh_FreqArray[josh_Map[v]]){$else}josh_FreqArray[josh_Map[v]]:=josh_FreqArray[josh_Map[v]]+1{$endif}
else
begin
fillchar(josh_FreqArray,sizeof(josh_FreqArray),0);
exit(false);
end;
end;
for k:=1 to l2 do
begin
v:=Byte(s2[k]);
if v in [josh_Arr_Start..josh_Arr_End] then {$ifdef useinc}dec(josh_FreqArray[josh_Map[v]]){$else}josh_FreqArray[josh_Map[v]]:=josh_FreqArray[josh_Map[v]]-1{$endif}
else
begin
fillchar(josh_FreqArray,sizeof(josh_FreqArray),0);
exit(false);
end;
end;
end
else
begin
for k:=1 to l1 do
begin
v:=Byte(s1[k]);
if v in [josh_Arr_Start..josh_Arr_End] then {$ifdef useinc}inc(josh_FreqArray[josh_Map[v]]){$else}josh_FreqArray[josh_Map[v]]:=josh_FreqArray[josh_Map[v]]+1{$endif}
else
begin
fillchar(josh_FreqArray,sizeof(josh_FreqArray),0);
Result:=False;
raise Exception.CreateFmt(josh_mess_s1,[k]);
exit;
end;
end;
for k:=1 to l2 do
begin
v:=Byte(s2[k]);
if v in [josh_Arr_Start..josh_Arr_End] then {$ifdef useinc}dec(josh_FreqArray[josh_Map[v]]){$else}josh_FreqArray[josh_Map[v]]:=josh_FreqArray[josh_Map[v]]-1{$endif}
else
begin
fillchar(josh_FreqArray,sizeof(josh_FreqArray),0);
Result:=False;
raise Exception.CreateFmt(josh_mess_s2,[k]);
exit;
end;
end;
end;
if IgnoreSpaces then
begin
for k:=josh_Arr_Start+1 to josh_Arr_End do if josh_FreqArray[k]<>0 then
begin
fillchar(josh_FreqArray,sizeof(josh_FreqArray),0);
exit(false);
end;
josh_FreqArray[josh_Arr_Start]:=0;
end
else
begin
for k:=josh_Arr_Start to josh_Arr_End do if josh_FreqArray[k]<>0 then
begin
fillchar(josh_FreqArray,sizeof(josh_FreqArray),0);
exit(false);
end;
end;
Result:=True;
end;
Initialization
for josh_loop:=josh_Arr_Start to josh_Arr_End do josh_Map[josh_loop]:=josh_loop;
fillchar(josh_FreqArray,sizeof(josh_FreqArray),0);
{$ifdef OnlyAlphaNumericChars}
for josh_loop:=$30 to $39 do josh_Map[josh_loop]:=josh_loop;
for josh_loop:=$41 to $5A do josh_Map[josh_loop]:=josh_loop;
for josh_loop:=$61 to $7A do josh_Map[josh_loop]:=josh_loop-$20;
{$else}
for josh_loop:=$61 to $7A do josh_Map[josh_loop]:=josh_loop-$20;
{$endif}
end.