PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/fth/strings.fth

https://github.com/cataska/pforth
Forth | 97 lines | 81 code | 15 blank | 1 comment | 1 complexity | 955b5c39095f58ecc2dd8192c6cc01db MD5 | raw file
  1. \ @(#) strings.fth 98/01/26 1.2
  2. \ String support for PForth
  3. \
  4. \ Copyright Phil Burk 1994
  5. ANEW TASK-STRINGS.FTH
  6. : -TRAILING ( c-addr u1 -- c-addr u2 , strip trailing blanks )
  7. dup 0>
  8. IF
  9. BEGIN
  10. 2dup 1- chars + c@ bl =
  11. over 0> and
  12. WHILE
  13. 1-
  14. REPEAT
  15. THEN
  16. ;
  17. \ Structure of string table
  18. : $ARRAY ( )
  19. CREATE ( #strings #chars_max -- )
  20. dup ,
  21. 2+ * even-up allot
  22. DOES> ( index -- $addr )
  23. dup @ ( get #chars )
  24. rot * + 4 +
  25. ;
  26. \ Compare two strings
  27. : $= ( $1 $2 -- flag , true if equal )
  28. -1 -rot
  29. dup c@ 1+ 0
  30. DO dup c@ tolower
  31. 2 pick c@ tolower -
  32. IF rot drop 0 -rot LEAVE
  33. THEN
  34. 1+ swap 1+ swap
  35. LOOP 2drop
  36. ;
  37. : TEXT= ( addr1 addr2 count -- flag )
  38. >r -1 -rot
  39. r> 0
  40. DO dup c@ tolower
  41. 2 pick c@ tolower -
  42. IF rot drop 0 -rot LEAVE
  43. THEN
  44. 1+ swap 1+ swap
  45. LOOP 2drop
  46. ;
  47. : TEXT=? ( addr1 count addr2 -- flag , for JForth compatibility )
  48. swap text=
  49. ;
  50. : $MATCH? ( $string1 $string2 -- flag , case INsensitive )
  51. dup c@ 1+ text=
  52. ;
  53. : INDEX ( $string char -- false | address_char true , search for char in string )
  54. >r >r 0 r> r>
  55. over c@ 1+ 1
  56. DO over i + c@ over =
  57. IF rot drop
  58. over i + rot rot LEAVE
  59. THEN
  60. LOOP 2drop
  61. ?dup 0= 0=
  62. ;
  63. : $APPEND.CHAR ( $string char -- ) \ ugly stack diagram
  64. over count chars + c!
  65. dup c@ 1+ swap c!
  66. ;
  67. \ ----------------------------------------------
  68. : ($ROM) ( index address -- $string )
  69. ( -- index address )
  70. swap 0
  71. DO dup c@ 1+ + aligned
  72. LOOP
  73. ;
  74. : $ROM ( packed array of strings, unalterable )
  75. CREATE ( <name> -- )
  76. DOES> ( index -- $string ) ($rom)
  77. ;
  78. : TEXTROM ( packed array of strings, unalterable )
  79. CREATE ( <name> -- )
  80. DOES> ( index -- address count ) ($rom) count
  81. ;
  82. \ -----------------------------------------------