CompareChar: invalid result
Repost from https://forum.lazarus.freepascal.org/index.php/topic,68984.0.html
CompareChar
https://www.freepascal.org/docs-html/rtl/system/comparechar.html
-1
if buf1 and buf2 contain different characters in the first len positions, and the first such character is smaller in buf1 than the character at the same position in buf2.
0
if the first len characters in buf1 and buf2 are equal.
1
if buf1 and buf2 contain different characters in the first len positions, and the first such character is larger in buf1 than the character at the same position in buf2.`
This means the possible result values are -1, 0, and 1; while in practice, it behaves like CompareByte and returns the difference between the character values in the ASCII table.
var
s, d: string;
begin
s := 'testaxxx';
d := 'testxxxx';
writeln(CompareChar(s[1], d[1], length(s)));
end.
It prints -23, the difference between Ord('a')
and Ord('x')
.
Actually implementation of CompareChar is to use CompareByte:
function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
begin
CompareChar:=CompareByte(buf1,buf2,len);
end;
Edited by Fibonacci