-1

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;
user1009073
  • 3,160
  • 7
  • 40
  • 82
  • There is not enough here to reproduce the issue. – Arnaud Bouchez Nov 17 '14 at 00:33
  • Found my answer....Somehow a string was being used for input, not an AnsiString. The -65 might be an indicator when this situation occurs... – user1009073 Nov 17 '14 at 03:17
  • 1
    The input was likely done with `UnicodeString` and then converted to `AnsiString`, so the data likely "appeared" similar to the human eye but is actually quite different in memory, which `CompareStr()` would notice. It is not enough to look at the string values in the debugger, you have to look at the raw bytes. A difference of 65 likely means you are dealing with ANSI characters outside of the ASCII range. – Remy Lebeau Nov 17 '14 at 03:57
  • possible duplicate of [If statement for checking strings](http://stackoverflow.com/questions/24389508/if-statement-for-checking-strings) – Disillusioned Nov 17 '14 at 07:57
  • Your strings are different - simple as that. Remember `CompareStr` requires case to be the same as well. Use `CompareText` for case insensitive comparison. If you're still struggling, my answer to the same problem shows how you can figure out where the difference is: http://stackoverflow.com/a/24390357/224704 – Disillusioned Nov 17 '14 at 08:02

2 Answers2

2

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 the idVal pointer, there is the possibility that idVal is actually referring to a Wide/Unicode string. This would mean Key1 is trying to treat a non AnsiString as if it were one.

Based on OP's comment:

Found my answer....Somehow a string was being used for input, not an AnsiString. The -65 might be an indicator when this situation occurs...

That is exactly the problem.

Community
  • 1
  • 1
Disillusioned
  • 14,635
  • 3
  • 43
  • 77
1

Both variables are AnsiStrings, both are the same value, and there are no spaces.

You would appear to be mistaken, CompareStr says otherwise. The two strings are not equal. It is always best in a situation like this to doubt yourself rather than suspecting the library function to be incorrect.

Step 1 here will be to add some debugging code. Output the two strings when interpreted as binary. Write the ordinal value of each character to aa debug log. This will reveal the difference.

CompareStr returns -65 if that helps any.

The implementation of CompareStr compares character by character and looks at the difference between the ordinal values. So long as the difference is always zero then the algorithm can move on to next next value. But when a non-zero value is found, the strings are different and that non-zero value returned. So, the first differing characters have ordinal values that differ by 65.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490