anew str.f \ for Win32Forth
: $extract ( adr-counted$ offset #chars - adr-offset$ #chars ) >r + r> ;
: count! ( adr len - ) swap c! ;
: drop-count ( adr len - adr-counted$ ) drop 1- ;
\ test
create string1 here 20 allot s" A very small " rot place
create string2 here 20 allot s" test1 " rot place
create result-string 40 allot
: test
result-string dup>r 0 count! \ clears the result-string
string1 1 2 $extract r@ +place
string1 8 6 $extract r@ +place
string2 1 4 $extract r@ +place
s" 2 " drop-count 1 1 $extract r@ +place
r> count type
;
test
\s test shows:
test A small test2 ok
Jos
You might want to add
: $. count type ;
if you are going to use it a lot.
\ These work with <=255 byte counted strings.
\ some string handling words
: text WORD PAD OVER C@ 1+ CMOVE PAD ;
: $" [CHAR] " text ; ( -- $adr) \ use as $" abcd"
\ more string handling words
: $! ( src dest -- ) OVER C@ 1+ CMOVE ;
: $+ ( $1 $2 --) \ string concatenation: $1 <- $1 + $2
OVER COUNT ( $1 $2 $1+1 n1 )
DUP >R + ( $1 $2 dst )
OVER COUNT ( $1 $2 dst src n2)
ROT SWAP CMOVE ( $1 $2) \ move chars
C@ R> + SWAP C! ; \ adjust length
create newpad 256 allot ok
$" Here is a string." ok.
count type Here is a string. ok
pad newpad $! ok
newpad count type Here is a string. ok
$" And here is another." ok.
count type And here is another. ok
newpad pad $+ ok
newpad count type Here is a string. And here is another. ok
--
Julian V. Noble
j...@virginia.edu
"Elegance is for tailors!" -- Ludwig Boltzmann