COMPARE

( c-addr1 u1 c-addr2 u2 -- n )

Compare the string specified by c-addr1 u1 to the string specified by c-addr2 u2. The strings are compared, beginning at the given addresses, character by character, up to the length of the shorter string or until a difference is found. If the two strings are identical, n is zero. If the two strings are identical up to the length of the shorter string, n is minus-one (-1) if u1 is less than u2 and one (1) otherwise. If the two strings are not identical up to the length of the shorter string, n is minus-one (-1) if the first non-matching character in the string specified by c-addr1 u1 has a lesser numeric value than the corresponding character in the string specified by c-addr2 u2 and one (1) otherwise.

Testing:

T{ s1        s1 COMPARE ->  0  }T
T{ s1  PAD SWAP CMOVE   ->     }T    \ Copy s1 to PAD
T{ s1  PAD OVER COMPARE ->  0  }T
T{ s1     PAD 6 COMPARE ->  1  }T
T{ PAD 10    s1 COMPARE -> -1  }T
T{ s1     PAD 0 COMPARE ->  1  }T
T{ PAD  0    s1 COMPARE -> -1  }T
T{ s1        s6 COMPARE ->  1  }T
T{ s6        s1 COMPARE -> -1  }T

: "abdde" S" abdde" ;
: "abbde" S" abbde" ;
: "abcdf" S" abcdf" ;
: "abcdee" S" abcdee" ;

T{ s1 "abdde"  COMPARE -> -1 }T
T{ s1 "abbde"  COMPARE ->  1 }T
T{ s1 "abcdf"  COMPARE -> -1 }T
T{ s1 "abcdee" COMPARE ->  1 }T

: s11 S" 0abc" ;
: s12 S" 0aBc" ;

T{ s11 s12 COMPARE ->  1 }T
T{ s12 s11 COMPARE -> -1 }T

ContributeContributions

agsbavatar of agsb How many cells can be compared ? Must have any limit ?Request for clarification2021-04-25 19:52:16

AntonErtlavatar of AntonErtl

COMPARE compares characters, not cells. The maximum number of characters compared is specified by the u1 and u2 arguments.

Closed

StephenPelcavatar of StephenPelc

Note that where the standard mentions characters, it nearly always means "primitive characters" or pchars for short. With the AU=1 decision, this means bytes in all practical cases. UTF-16 systems need to use XCHAR operations. KEY and EMIT are still there to handle 8 bit operations such as Telnet. Note also that on most implementations, TCP/IP and USB operations can have packet breaks in the middle of UTF-16 characters.

StephenPelcavatar of StephenPelc

StephenPelcavatar of StephenPelc

Oops, I forgot to close the response again

Closed
Reply New Version