I have a comparison operator for a Tree iteration. It uses AnsiString variables. My issue is that when the values appear to be equal, I am not getting an equal indicator (aka not getting 0 from System.AnsiStrings.CompareStr). I have looked at my variables via the debbugger and stepped through my code. Both variables are AnsiStrings, both are the same value, and there are no spaces. CompareStr returns -65 if that helps any.
What can I be overlooking? Here is my code.
function CompareNodes(idVal: pointer; ANode: TALStringKeyAVLBinaryTreeNode): Integer;
var
Key1, Key2: AnsiString;
begin
Key1 := PAnsiString(idVal)^;
Key2 := ANode.ID;
Result := System.AnsiStrings.CompareStr(Key1, Key2);
end;
It is interesting to note that 65 is the difference between
A
and#0
.Since the line
Key1 := PAnsiString(idVal)^;
performs a unchecked type-cast of of theidVal
pointer, there is the possibility thatidVal
is actually referring to a Wide/Unicode string. This would meanKey1
is trying to treat a non AnsiString as if it were one.Based on OP's comment:
That is exactly the problem.