Menu

COMPARE this!

thebeez

COMPARE is one of the oldest libraries in 4tH - which isn't too surprising when you realize it is the equivalent of the equals word where strings are concerned. But it wasn't until I received a complaint from a user that it wasn't ANS-compliant that I became aware I had neglected it for too long.

In short, I don't have much use for an ANS compliant COMPARE. My COMPARE is case-insensitive and I like it just fine, since most of the problems I need to solve require exactly that behavior. But it doesn't stop there. It returns a negative number when it's smaller and a positive number when it's larger. But that's not what ANS-Forth requires. It requires -1 when the string is smaller, 0 when it matches and 1 when it's larger.

True, most of the time it doesn't boil down to that. You use 0< to sort it and 0= to see if it matches. Which works both ways. Except that my implementation is a bit shorter and faster. But if one really requires an ANS-compliant implementation he deserves to get it. This new implementation will fullfill all of your requirements. If you expect the legacy implementation, you're gonna get it - it's the default. If you want the legacy case-sentitive implementation - same thing.

However, if you want a COMUS like ICOMPARE, just use the pragma USEICOMPARE. If you need a case sensitive COMPARE as well, just include the darn thing twice. If you require a true ANS-Forth compliant version, use the pragma ANSCOMPARE and you'll get it. If you really want to test this implementation to the limit, be my guest and you'll see the compiler will throw in the towel, rather than to allow it to misbehave.

In the meanwhile you can tune COMPARE to whatever identity you want, "classic 4tH", "ANS" or "COMUS" - by issuing just a pragma. Confusing, yes. Impossible, no.