Menu

I need a BREAK?

One of the strangest library routines 4tH has to offer must be BREAK? It is closely related to its C equivalent strpbrk(), although it doesn't return a pointer but a boolean. You can find BREAK? in at least half a dozen other 4tH libraries. So far so good. But why is it a strange word?

Well, simply because it has a very awkward stack diagram. It takes a character, a flag and two string addresses. That's weird. First of all, what is that boolean doing there. Second, why doesn't it take a true ANS string, address and count and does all of its dirty BOUNDS work in the privacy of its own definition?

To answer that question, we have to take a good look at this definition, which is fairly short:

: break? ?do over i c@ = if 0= leave then loop nip ;

It becomes clear that the addresses are immediately consumed by a ?DO which quickly loops through the string, fetching its individual characters and comparing them to the character right under the top of the stack. If the character is equal, it flips the boolean and exits. If the boolean was TRUE, it becomes FALSE and vice versa, so the initial value of the boolean determines what is actually returned.

That is useful. Sometimes it is bad when a character is matched and sometimes it's just what you wanted. In KPRE.4TH it is part of a word that returns TRUE when a certain character is found. In LATEX.4TH it determines whether a character needs to be escaped. In SBREAK.4TH it decides whether the search should continue or not. By determining the value of the flag beforehand, you can save yourself a 0= later on.

BREAK? is often part of a tight loop. More than once the string addresses are put on the return stack straight away and fetched just before BREAK? is called. When BOUNDS were part of its definition it would have to be executed over and over again. In a tight loop. Not a good idea. BTW, that's exactly the reason why the string addresses come before the flag and the character. Throwing 'em on the stack is the first thing that happens - leaving a pretty shallow stack diagram for you to play with.

It is clear that BREAK? wasn't really designed. It was just a factor that popped up again and again until it was time to give it its own central place in the library. And every time a similar problem came up it was reused again.

BREAK? is just a true Forth story, since extreme factoring is Forth's middle name. The result may sometimes be a BREAK?, an unlikely idiom that is that useful that it becomes a concept on its own.