Forum > Documentation (Maintaining -)

Docs: CompareChar, invalid result

(1/2) > >>

Fibonacci:
https://www.freepascal.org/docs-html/rtl/system/comparechar.html

--- Quote ---The function returns one of the following values:

-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.
--- End quote ---

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.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;begin  CompareChar:=CompareByte(buf1,buf2,len);end;

Bart:
File a bugreport in fpc's documentation bugtracker.

Bart

Fibonacci:
Thanks, wasnt aware there is a doc bugtracker :)

https://gitlab.com/freepascal.org/fpc/documentation/-/issues/39402

ASerge:
The same bug in documentation of the CompareWord, CompareDWord, but not in CompareChar0, CompareByte.

Thaddy:
Note that the distance is very valuable. In many cases you need that distance, not just -1,0,1.

Navigation

[0] Message Index

[#] Next page

Go to full version