home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
source
/
bstring.seq
< prev
next >
Wrap
Text File
|
1990-04-21
|
5KB
|
171 lines
\ This string package originally appeared in
\ Dr. Dobbs Number 50 and was written by
\ Mr. Ralph Dean
\ Search for end of string adr and leave string length on top.
: SEARCH ( addr -- len )
DUP BEGIN \ addr addr
DUP C@ \ addr addr char
SWAP 1+ SWAP \ addr addr+1 char
0= UNTIL \ addr addr+1+len
SWAP - 1- ; \ len
\ Defining word to create new strings.
: STRING ( len -- ) \ when compiling
( -- addr len ) \ when executing
CREATE ABS 255 MIN 1 MAX \ len has been range checked.
DUP C,
0 DO 0 C, LOOP 0 C, \ -- initialize as nulls.
DOES> 1+ DUP SEARCH ;
\ Store string. Usage: string1 string2 S!
\ Target string is replaced by new string.
: S! ( addr1 len1 addr2 len2 -- )
DROP DUP 1- C@ \ addr1 len1 addr2 mlen
ROT MIN 1 MAX \ addr1 addr2 len <- length to store
2DUP + 0 SWAP C! \ addr1 addr2 len mark end with a null
MOVE ;
\ Store substring. Usage: string1 string2 SUB!
\ Only substring of target is replaced.
: SUB! ( addr1 len1 addr2 len2 -- )
ROT MIN 1 MAX \ addr1 addr2 len <- sub string length.
MOVE ;
\ Temporary storage for string operations.
CREATE TEMP 256 ALLOT
\ Usage: 5 10 string MID$
: MID$ ( posn len1 addr2 len2 -- addr len )
SWAP >R ROT MIN 1 MAX \ len1 len2 posn
SWAP OVER MAX OVER - 1+
SWAP R> + 1- SWAP
OVER SEARCH MIN ;
\ Usage: 6 string LEFT$
: LEFT$ ( posn addr1 len1 -- addr len )
>R >R 1 SWAP R> R> MID$ ;
\ Usage: 6 string RIGHT$
: RIGHT$ ( posn addr1 len1 -- addr len )
256 -ROT MID$ ;
\ Concatenate two strings.
\ Usage: string1 string2 S+ string3 S!
: S+ ( addr1 len1 addr2 len2 -- addr len )
ROT >R ROT R> TUCK
TEMP SWAP MOVE
TUCK + 255 MIN DUP >R
OVER - SWAP TEMP + SWAP MOVE
R> 0 OVER TEMP + C!
TEMP SWAP ;
\ Return current string length. Usage: string LEN
: LEN ( addr len -- len )
NIP ;
\ Return max string length. Usage: string MLEN
: MLEN ( addr len -- mlen )
DROP 1- C@ ;
\ Convert single number to a string.
: STR$ ( n -- addr len )
S>D TUCK DABS
<# 0 HOLD #S ROT SIGN #> 1- ;
\ Convert string to a number.
: VAL ( addr len -- dn )
PAD 2DUP C! 1+ SWAP CMOVE \ Move string to PAD
BL PAD COUNT + C! \ Add a blank at the end
PAD NUMBER DROP ;
: " \ " {text}" ( -- addr len )
STATE @ IF [COMPILE] "
ELSE ASCII " WORD
PAD 257 ERASE DUP COUNT
PAD SWAP MOVE PAD SWAP C@
THEN ; IMMEDIATE
\ String input. Usage: string $IN
: $IN ( addr len -- )
OVER SWAP MLEN EXPECT ;
\ String equality. Usage string1 string2 S=
\ Leaves a true flag if strings are equal.
: S= ( addr1 len1 addr2 len2 -- flag )
ROT OVER =
IF TRUE SWAP 0 ?DO DROP OVER C@ OVER C@ =
IF 1 1 D+ TRUE
ELSE FALSE LEAVE
THEN LOOP
ELSE DROP FALSE
THEN NIP NIP ;
\ String array.
\ Usage: 5 20 SARRAY NAMES
\ " JACK" 1 NAME S! 1 NAME TYPE
\ " JOHN" 2 NAME S! 2 NAME TYPE etc...
: SARRAY ( n len -- ) \ when compiling
CREATE ABS 255 MIN 1 MAX SWAP
0 ?DO DUP DUP C,
0 ?DO BL C, LOOP 0 C,
LOOP DROP
DOES> SWAP 1- OVER C@
2+ * + 1+ DUP SEARCH ;
\ Ralph Dean's FORTH implementation of SOUNDEX program that
\ originally appeared in the May 1980 Byte Magazine.
\
\ Executing SOUND will cause a prompt for the name.
\ The name is terminated after 30 characters or <enter>.
\ The soundex code is then computed and typed out.
\ The string variable S$ conatains the code produced.
\ For more information on Soundex codes see the original
\ Byte article.
FORTH DEFINITIONS DECIMAL
30 STRING N$ \ Input string whose soundex code is to be found.
4 STRING S$ \ Output string containing soundex code.
1 STRING K$ 1 STRING L$
: NAME ( -- ) \ Prompt for input of last name.
CR ." Last Name? " N$ $IN ;
: FIRST1 ( -- ) \ Move first character to S$
1 N$ LEFT$ S$ S! ;
: ITH ( n m -- k )
N$ MID$ DROP C@ 64 - ;
: KTH ( k -- )
DUP " 01230120022455012623010202"
MID$ K$ S! ;
: BLS ( -- )
S$ K$ S+ S$ S! ;
: TEST ( -- flag )
K$ L$ S= K$ " 0" S= OR 0= ;
: IST ( n n flag )
DUP 1 < OVER 26 > OR 0= ;
\ Compute soundex code
: COMP ( -- )
N$ LEN 1+ 2
DO I I ITH IST
IF KTH TEST IF BLS THEN
ELSE DROP
THEN
K$ L$ S!
LOOP ;
\ This is the Program. BROWN , BRUN , BRAWN all give B650
: SOUNDEX ( -- )
NAME FIRST1 N$ LEN 2 >
IF COMP THEN S$ " 0000" S+ S$ S!
CR ." Soundex Code = " S$ TYPE CR ;