home *** CD-ROM | disk | FTP | other *** search
- \ Lesson 6 Part 5 ( F-PC 3.5 Tutorial by Jack Brown )
- COMMENT:
- Packing Forth Strings ( continued )
-
- Here is what the word CPACK defined below will do for us.
- Move the un-counted string of length n at memory location addr-from to
- memory location addr-to using Forth's packed string format. This means
- that the first byte at addr-to must be the string length.
- COMMENT;
- : CPACK ( addr-from addr-to n -- )
- SWAP 2DUP C! ( addr-from n addr-to ) \ Store string count.
- 1+ SWAP CMOVE ; \ skip over count and move string.
- COMMENT:
- Try the following example to verify the correct operation of CPACK
- CREATE BUFFER2 80 ALLOT <enter>
- READLINE <enter>
- BUFFER1 BUFFER2 LEN @ CPACK <enter>
- BUFFER2 COUNT DUMP <enter>
- BUFFER2 COUNT TYPE <enter>
-
- One commonly required string operation is the truncation of chopping of
- n characters from the beginning of a string. This can be accomplished
- by the Forth word /STRING "chop-string". In the high level definition
- of /STRING below addr'= addr+n and len'= len-n . Note that there is
- no checking performed to insure that len > n so programmer beware. You
- should also be aware that the original string remains intact and should
- you want to retain the string in its shortened form you should use the
- word CPACK previously defined to move it to its new resting place.
- COMMENT;
- \ Chopping n characters from the beginning of a string
- : /STRING ( addr len n addr' len' )
- ROT OVER + -ROT - ;
- COMMENT:
- Another common operation required is the removal of any trailing blanks
- at the end of a string before it is displayed or moved to some other
- location. This can be accomplished by the forth word -TRAILING
- COMMENT;
- \ Remove trailing blanks from a string.
- : -TRAILING ( addr count1 -- addr count2 )
- DUP 0
- ?DO \ Examine each character if any.
- 2DUP + 1- \ Address of last character.
- C@ BL <> \ Is this character a blank?
- IF LEAVE THEN \ If its not we are done.
- 1- \ Decrease count by 1 to shorten.
- LOOP ;
- COMMENT:
- Problem 6.6
- a) Write a version of /STRING that provides an error message at run time
- if the number of characters to be chopped is greater than the
- original string length.
- b) What does -TRAILING do i) if len=0? ii) if len=1? iii) if string is
- all blanks?
- c) Write word a high level definition for
- -LEADING ( addr len -- addr' len' )
- Which trims off an leading blanks from a the string at addr returning
- the adjusted address as addr' and the shortened length as len'.
- What would your definition do i) if len=0? ii) if len=1?
- iii) if the input string is all blanks?
-
- F-PC has a number of other string primitives. You may find SKIP which
- looks for a mismatching character and SCAN which looks for a matching
- character useful.
-
- SKIP ( addr len char -- addr' len' )
- Given the address and length of a string, and a character to look for,
- run through the string while we continue to find the character. Leave
- the address of the *mismatch* and the length of the remaining string.
-
- SCAN ( addr len char -- addr' len' )
- Given the address and length of a string, and a character to look for,
- run through the string until we find the character. Leave the address
- of the *match* and the length of the remaining string.
-
- To keep this straight think.... SKIP to mismatch, SCAN for match
-
- Converting a Digit Only String Into a Number
-
- CONVERT ( ud1 addr1 -- ud2 addr2 )
- Convert a counted string starting at addr1 or an uncounted string
- starting at addr1+1 accumulating number into ud1. Conversion stops at
- first non digit character whose location, addr2, is left on the stack.
- addr1 is usually the address of a counted or packed digit string. The
- first digit of the string to be converted will be at addr1+1 . ud2 is
- ud1 with the addition of the accumulated digits. The character count or
- digit that may be at addr1 is not used by CONVERT. Examples:
-
- CREATE DSTRING ," -456.123" <enter> ok
- 0 0 DSTRING CONVERT .S <enter> [3] 0 0 29916 ok
- C@ EMIT <enter> - ok D. <enter> 0 ok
- \ Nothing is converted since the non digit - is encountered immediately
- 0 0 DSTRING 1+ CONVERT .S <enter> [3] 456 0 29920 ok
- DUP C@ . <enter> 46 ok DUP C@ EMIT <enter> . ok
- \ 456 is converted to binary, conversion is stopped by decimal point.
- CONVERT .S <enter> [3] 62907 6 29924 ok
- C@ . <enter> 32 ok D. <enter> 456123 ok
- \ Conversion is restarted where we left off and 123 are accumulated.
- \ Conversion is stopped by the trailing blank.
- COMMENT;
- ( Please Move to Lesson 6 Part 6 )
-
-
-