home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-11 | 5.0 KB | 129 lines | [TEXT/McSk] |
- ( Strings 10/15/95 23:30:19 )
- \
- \ These words deal with 0 terminated strings as in Ron Kneusel's CGIShell.
- \ <ftp://kreeft.intmed.mcw.edu/Q/pub/forth/cgishell.sit.hqx>
- \
- \ Several words in this set are borrowed from rtk's CGIShell, some renamed,
- \ some modified. The names maintain compatability with the word-set in
- \ _Library of Forth Routines and Utilities_ by James D. Terry
- \ (c) 1986 Shadow Lawn Press ISBN 0-452-25841-3
- \
- \ In comments, string is the starting address of a zero terminated string,
- \ and len is the length not including the zero. String[255] is a length
- \ byte preceded string, with a max length of 255 bytes.
- \
- \ String format:
- \ string address is first byte ->This is a string.0<- Ends with a zero
-
- \ Length and $clear get used a lot - do them in ml.
- : LENGTH ( string -- len ) \ length of the string at addr
- ( was: dup >r BEGIN dup c@ WHILE 1+ REPEAT r> - ; )
- ,$ 3016 \ move (ps),d0
- ,$ 4a33 ,$ 0000 \ @0: tst.b 0(bp,d0.w)
- ,$ 6706 \ beq.s @1
- ,$ 0640 ,$ 0001 \ addi #1,d0
- ,$ 60f4 \ bra.s @0
- ,$ 9056 \ @1: sub (ps),d0
- ,$ 3c80 ; \ move d0,(ps)
-
- : $CLEAR ( string -- ) \ erase a string ( equivalent to: 0 swap c! ; )
- ,$ 301E ,$ 4233 ,$ 0000 ; \ move (ps)+,d0 clr.b 0(bp,d0.w)
-
- \ The next 4 words are directly from Ron's CGI Framework.
-
- \ Convert between null terminated and length byte preceeded type strings.
- : >NULL ( string[255] -- ) \ convert a string[255] into a string
- dup c@ 2dup + >r swap dup 1+ swap rot cmove r> $clear ;
-
- : >COUNT ( string -- ) \ convert a string into a string[255]
- dup length >r dup dup 1+ r cmove r> swap c! ;
-
-
- \ Terminal I/O.
- : 0TYPE ( string -- ) \ type null terminated string
- dup length dup IF type ELSE 2drop THEN ;
-
- : ACCEPT ( string len -- ) \ like expect but stores zero at end of line
- 2dup 1+ 0 fill >r dup r> expect dup length 1- + $clear ; ( bug fixed)
-
-
- \ Test a string's content.
- : $= ( string1 string2 -- f ) \ true if string2,len2 = string1,len1
- dup length 1+ -1 swap 2swap rot 0 DO \ set flag to true
- over r + c@ over r + c@ = \ check each byte
- 0= IF rot 1+ rot rot leave THEN \ change flag to false
- LOOP 2drop ;
-
-
- \ Manipulate strings.
- : $COPY ( source.string dest.string -- ) \ copy source to dest
- over length 1+ cmove ;
-
- : $+ ( source.string dest.string -- ) \ append source to the end of dest
- dup length + $copy ;
-
- : $LEFT ( string len -- ) \ clip string to len chars
- over length min + $clear ;
-
- : $RIGHT ( string len -- ) \ clip string to rightmost len characters
- over length over - 0> IF
- over length over - rot dup rot + swap rot 1+ cmove
- ELSE 2drop THEN ;
-
- : $MID ( string start len -- ) \ clip string to len section at start
- rot rot over length swap - 1+ >r dup r> $right swap $left ;
-
- : $UPPER ( string -- ) dup >count dup upper dup >null drop ; \ uppercase
-
-
- \ Find and replace with strings.
- variable POS ( local variable )
- : $FIND ( string1 string2 -- pos ) \ find string2 in string1; 0 if unfound
- 0 pos !
- over length over length - 2+ 1 DO
- over here $copy
- here over length r swap $mid
- here over
- $= IF r pos ! leave THEN
- LOOP 2drop
- pos @ ;
-
- : $REPLACE ( dest.string1 find.string2 replace.string3 -- )
- rot >r swap
- r over $find ?dup IF \ IF string2 is found in string1
- r here $copy \ THEN replace string2 with string3
- r over 1- $left \ modify string1
- rot r $+
- swap length + \ !!! IMPORTANT !!!
- here length swap - 1+ \ DOES NOT CHECK FOR OVERWRITE
- here swap $right \ String1 MUST accomodate any
- here r> $+ \ additional bytes from string3
- ELSE 2drop r> drop THEN ;
-
- \ Create and assign strings of several varieties.
- : $CONSTANT \ compiling: ( -- ) name a string terminated with '}'
- CREATE 125 word here c@ 1+ dup 2 mod + allot 0 [compile] ,
- DOES> count drop ; \ runtime action: ( -- string )
- \ This uses a curley brace because they aren't used much on web pages.
- \ eg: $constant ESERROR Empty stack!}
-
- : $VARIABLE CREATE 1+ allot ; \ compiling: ( len -- ) name an empty string
- \ eg: 80 $variable INPUTLINE inputline ${ Hi there!}
-
- : $ARRAY \ create named string arrays - name from input stream
- CREATE dup , * allot \ compiling: ( number_of_.strings len -- )
- DOES> dup @ rot * + 2+ ; \ runtime: ( string_number -- string )
- \ eg: 15 64 $array ERRORMESSAGES
- \ 0 errorMessages ${ Error!}
-
- \ NOTE: Constants and variables are identical except that constants
- \ have no room to grow, but variables _may_ have extra memory
- \ allotted to them to grow into. Also constants are assigned
- \ when they are created, while variables (and arrays, which are
- \ lists of variables) must be assigned seperately (see below).
-
- : ${ ( string -- ) \ assign text to a string from the input stream.
- 125 word here >null here swap $copy ;
- \ eg: inputLine ${ Something to say!} *** NO OVERWRITE CHECK ***
-
-