13.14Tokenizing strings. 4tH 4tH Forth compiler
إعلان
إعلان
CHAPTER 13. SPECIAL LIBRARIES 220
[needs lib/time.4th] today ." year:" . ." month:" . ." day:" . cr
Just don’t ask me how this thing works, Everett F. Carter figured this one out. ”TODAY” does the easy work. ”JDATE” converts the Julian day to the Gregorian date. There is also a way to convert a Gregorian date to a Julian day, called ”JDAY”:
[needs lib/time.4th]
26 02 2002 jday 64 - jdate today ." year:" . ." month:" . ." day:" . cr
This can be quite handy if you want to calculate which date it was 64 days ago. ANS-Forth also defines a word that does it all called ”TIME&DATE”. This word throws seconds, minutes, hours, day, month and year (TOS) on the stack, but always returns GMT:
[needs lib/ansfacil.4th] time&date . . . . . . cr
And finally, we even got a word that returns the date of easter:
[needs lib/easter.4th]
2005 easterSunday ." year:" . ." month:" . ." day:" . cr
Well, tell me, isn’t that kind of neat?
13.14
Tokenizing strings
Sometimes you want to split up a string in several different parts. This is called ”tokenizing”. Doing it with 4tH is (as usual ;-) quite easy. Just include tokenize.4th. Now you got several words to get what you want. tokenize.4th creates a deferred word
14 called ”IS-TYPE”. It decides whether a character is of a certain type. In this example, we just want to know whether it is a lowercase ’a’: include lib/tokenize.4th
:noname [char] a = ; is is-type
Now it’s time to play ball: s" 01234aBcDe01234" scan> type cr
”SCAN>” will now skip all characters unless it is an ’a’. When it is found, it stops and returns the remainder of the string: aBcDe01234
Yes, ”SCAN>” starts at the beginning of the string. But there is also a word that starts at the end of the string:
14
See section 11.7.
CHAPTER 13. SPECIAL LIBRARIES 221 s" 01234aBcDe01234" scan< type cr
It returns a different result too:
01234a
And what about the rest of the string? Well, that is discarded. But if you need it, there is also a word that just splits up the string: s" 01234aBcDe01234" split> type cr type cr
So, ”SPLIT>” returns two strings:
01234 aBcDe01234
And of course, he’s got a little brother that works the other way around: s" 01234aBcDe01234" split< type cr type cr
”SPLIT<” returns two strings too:
01234a
BcDe01234
That was quite easy. But what if you want to find the first non-digit? That’s what we got ”SKIP>” for! ”SKIP>” skips all characters of a certain kind. We’ve already seen how we can distinguish characters in section 8.26. So in this case we just got to include istype.4th
and assign ”IS-DIGIT” to ”IS-TYPE”: include lib/tokenize.4th
include lib/istype.4th
’ is-digit is is-type s" 01234aBcDe01234" skip> type cr
”SKIP>” returns a single string: aBcDe01234
And he has a counterpart too: include lib/tokenize.4th
include lib/istype.4th
’ is-digit is is-type s" 01234aBcDe01234" skip< type cr
May be this result will surprise you, although it is completely correct:
01234aBcDe
What exactly did we ask for? We wanted the first non-digit, starting from the end. ’e’ is the first non-digit, so ”SKIP<” is completely correct.
تنزيل
إعلان