This implements a word to compare address/length strings (or other data) in RETRO.
cmp-bytes
fetches a byte from each memory location and compares them.
:cmp-bytes (ss-ssf
dup-pair &fetch bi@ eq? ;
next
advances each address by 1.
:next (ss-SS
&n:inc bi@ ;
compare
ties the above to compare two ranges of the same length.
:compare (ssn-f
TRUE swap
[ &cmp-bytes dip and &next dip ]
times nip nip ;
The final word adds a bit more. It first checks that both strings are the same length, then passes the ranges to compare
for the actual comparison.
:compare-strings (snsn-f
'abcd 'acbbd reorder eq?
[ compare ] [ drop drop-pair FALSE ]
choose ;
And a test:
'hello #5 'hello #5 strcmp