8.17 Comparing strings. 4tH 4tH Forth compiler
जाहिरात
जाहिरात
CHAPTER 8. 4TH ARRAYS
8.17
Comparing strings
85
If you ever sorted strings you know how indispensable comparing strings is. As we mentioned before, there are very few words in Forth that act on strings. Here is a word that can compare two strings. It is located in the library member compare.4th.
[needs lib/compare.4th]
32 string one s" Hans Bezemer" one place
32 string two s" HANS BEZEMER" two place
\ compare two chars
\ define string one
\ initialize string one
\ define string two
\ initialize string two one count two count compare \ compare two strings if
." Strings differ" \ message: strings ok else
." Strings are the same" then cr
\ message: strings not ok
\ send CR
Simply pass two strings (represented by their address/count pairs) to ’COMPARE’ and it will return a TRUE flag when the strings are different. This might seem a bit odd, but strcmp() does exactly the same. If you don’t like that you can always add ’0=’ to the end of ’COMPARE’ to reverse the flag.
You’ll soon find out that ANS-Forth’s ’COMPARE’ is case sensitive. Lucky for you, you can modify the behaviour of 4tH’s ’COMPARE’. Just define this before the ’[NEEDS’ directive:
[pragma] casesensitive
[needs lib/compare.4th]
32 string one s" Hans Bezemer" one place
32 string two s" HANS BEZEMER" two place
\ compare two chars
\ define string one
\ initialize string one
\ define string two
\ initialize string two one count two count compare \ compare two strings if
." Strings differ" else
\ message: strings ok
\ message: strings not ok ." Strings are the same" then cr \ send CR
Now ’COMPARE’ will do a case sensitive comparison.
8.18
Finding a substring
Sometimes you need to find a string within a string. ANS-Forth has defined a word for that too. It is called ’SEARCH’. You need to include search.4th in order to use it. Now lets find ”the” in this string:
[needs lib/search.4th] s" How the cow catches the hare" s" the" search \ search for ’the’
0= if ." not " then ." found: " type \ print the result
CHAPTER 8. 4TH ARRAYS 86
’SEARCH’ always returns a flag and a address/count pair. If it returns true, the substring was found; if it returns false, the substring was not found. Now that’s pretty straightforward, isn’t it? That means that the small program above will print: found:
When the substring was found and: not found:
When the substring was not found. But what kind of string does it return when the substring was not found? Well, the entire string you fed it, so this would have been its output if we had been looking for the substring ”now” instead of ”the”: not found: How the cow catches the hare
But in this specific example we are looking for ”the”. When found, ’SEARCH’ returns the string after the first occurrence of the substring we were looking for: found: the cow catches the hare
Why that? Why not a position? Well, first of all, you can look for the same substring again:
[needs lib/search.4th] s" How the cow catches the hare" s" the" search drop \ drop the flag
2dup type s" the" search drop
\ print the string
\ now search again type \ print the string
This will print: the cow catches the hare the hare
But if you still want to see a position instead of a string, you can simply define this:
[needs lib/search.4th]
: position
2>r over swap 2r> search 0= >r drop swap - r> if 1- then
; s" How the cow catches the hare" s" the" position . cr
That will take care of your problems. If the substring was found, ”POSITION” will return a positive number. If it wasn’t found, it will return a negative number. Note that ’SEARCH’ can be persuaded to do a case-sensitive comparison, just like ’COMPARE’:
[pragma] casesensitive
[needs lib/search.4th]
Now ’SEARCH’ will do a case sensitive comparison, just like ’COMPARE’.
डाउनलोड
जाहिरात