Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding strings

1 view
Skip to first unread message

J.v.d.Ven

unread,
Aug 3, 1999, 3:00:00 AM8/3/99
to
\ When I was trying to add some parts of strings to another string I became
a bit disappointed
\ about Forth. The following one-liners solved the problem.

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


Julian V. Noble

unread,
Aug 11, 1999, 3:00:00 AM8/11/99
to
I have found these useful. They are not particularly original.

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

0 new messages