home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l6p050.seq < prev    next >
Text File  |  1990-04-22  |  5KB  |  104 lines

  1. \ Lesson 6 Part 5  ( F-PC 3.5 Tutorial by Jack Brown )
  2. COMMENT:
  3. Packing Forth Strings ( continued )
  4.  
  5. Here is what the word CPACK defined below will do for us.
  6. Move the un-counted string of length n at memory location addr-from to
  7. memory location addr-to using Forth's packed string format.  This means
  8. that the first byte at addr-to must be the string length.
  9. COMMENT;
  10. : CPACK  ( addr-from addr-to n -- )
  11.          SWAP 2DUP  C!    ( addr-from n addr-to ) \ Store string count.
  12.          1+  SWAP  CMOVE  ;  \ skip over count and move string.
  13. COMMENT:
  14. Try the following example to verify the correct operation of CPACK
  15.   CREATE BUFFER2   80 ALLOT <enter>
  16.   READLINE  <enter>
  17.   BUFFER1  BUFFER2  LEN @  CPACK  <enter>
  18.   BUFFER2  COUNT  DUMP    <enter>
  19.   BUFFER2  COUNT  TYPE    <enter>
  20.  
  21. One commonly required string operation is the truncation of chopping of
  22. n characters from the beginning of a string.  This can be accomplished
  23. by the Forth word /STRING  "chop-string".  In the high level definition
  24. of /STRING below  addr'= addr+n  and  len'= len-n .  Note that there is
  25. no checking performed to insure that len > n so programmer beware.  You
  26. should also be aware that the original string remains intact and should
  27. you want to retain the string in its shortened form you should use the
  28. word CPACK previously defined to move it to its new resting place.
  29. COMMENT;
  30. \ Chopping n characters from the beginning of a string
  31. : /STRING  ( addr len n  addr' len' )
  32.         ROT OVER + -ROT - ;
  33. COMMENT:
  34. Another common operation required is the removal of any trailing blanks
  35. at the end of a string before it is displayed or moved to some other
  36. location.  This can be accomplished by the forth word -TRAILING
  37. COMMENT;
  38. \  Remove trailing blanks from a string.
  39. :  -TRAILING ( addr count1 -- addr count2 )
  40.         DUP  0
  41.         ?DO                \ Examine each character if any.
  42.             2DUP + 1-      \ Address of last character.
  43.             C@ BL <>       \ Is this character a blank?
  44.             IF LEAVE THEN  \ If its not we are done.
  45.             1-             \ Decrease count by 1 to shorten.
  46.          LOOP ;
  47. COMMENT:
  48. Problem 6.6
  49. a) Write a version of /STRING that provides an error message at run time
  50.    if the number of characters to be chopped is greater than the
  51.    original string length.
  52. b) What does -TRAILING do i) if len=0? ii) if len=1? iii) if string is
  53.    all blanks?
  54. c) Write word a high level definition for
  55.    -LEADING ( addr len -- addr' len' )
  56.    Which trims off an leading blanks from a the string at addr returning
  57.    the adjusted address as addr' and the shortened length as len'.
  58.    What would your definition do  i)  if len=0? ii) if len=1?
  59.    iii) if the input string is all blanks?
  60.  
  61. F-PC has a number of other string primitives.  You may find SKIP which
  62. looks for a mismatching character and SCAN which looks for a matching
  63. character useful.
  64.  
  65. SKIP    ( addr len char -- addr' len' )
  66. Given the address and length of a string, and a character to look for,
  67. run through the string while we continue to find the character.  Leave
  68. the address of the *mismatch* and the length of the remaining string.
  69.  
  70. SCAN    ( addr len char -- addr' len' )
  71. Given the address and length of a string, and a character to look for,
  72. run through the string until we find the character.  Leave the address
  73. of the *match* and the length of the remaining string.
  74.  
  75. To keep this straight think.... SKIP to mismatch, SCAN for match
  76.  
  77. Converting a Digit Only String Into a Number
  78.  
  79. CONVERT  ( ud1 addr1 -- ud2 addr2 )
  80. Convert a counted string starting at addr1 or an uncounted string
  81. starting at addr1+1 accumulating number into ud1. Conversion stops at
  82. first non digit character whose location, addr2, is left on the stack.
  83. addr1 is usually the address of a counted or packed digit string.  The
  84. first digit of the string to be converted will be at addr1+1 . ud2 is
  85. ud1 with the addition of the accumulated digits. The character count or
  86. digit that may be at addr1 is not used by CONVERT.  Examples:
  87.  
  88. CREATE DSTRING ," -456.123" <enter> ok
  89. 0 0 DSTRING CONVERT .S  <enter> [3]      0       0   29916  ok
  90. C@ EMIT <enter>  - ok   D. <enter> 0  ok
  91. \ Nothing is converted since the non digit - is encountered immediately
  92. 0 0 DSTRING 1+ CONVERT .S  <enter> [3]    456       0   29920  ok
  93. DUP C@ . <enter> 46  ok DUP C@ EMIT <enter> . ok
  94. \ 456 is converted to binary, conversion is stopped by decimal point.
  95. CONVERT .S  <enter> [3]  62907       6   29924  ok
  96. C@ . <enter> 32  ok  D. <enter> 456123  ok
  97. \ Conversion is restarted where we left off and 123 are accumulated.
  98. \ Conversion is stopped by the trailing blank.
  99. COMMENT;
  100. ( Please Move to Lesson 6 Part 6 )
  101.  
  102.  
  103.  
  104.