Skip to content

Instantly share code, notes, and snippets.

@crcx
Created July 11, 2018 03:16
Show Gist options
  • Save crcx/f693f3d74a6a0d7733c61d7d792b784e to your computer and use it in GitHub Desktop.
Save crcx/f693f3d74a6a0d7733c61d7d792b784e to your computer and use it in GitHub Desktop.

Comparison of Address/Length Strings

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment