With half decent string handling like in lina it is one-cent-flute to
add Ruby inspired, #-formatted strings to Forth.
The brilliant idea is to use # to introduce a Forth word.
This is for the beta version of lina64 with nosecurity.
Your professional Forth probably has no security :-) then
don't bother.
printed during compilation
we go to ROME with 123456788 cows
format 5 0 results in:
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
vamos a ROME con $0000,0000,0000,0000,0000,0000,075B,CD14 vacas
format 5 1 results in:
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
If you prefer one liners.
\ Print X1 .. Xn using the format STRING.
: .format BEGIN &# $/ $. OVER WHILE BL $/ 2SWAP 2>R EVALUATE 2R>
OVER WHILE REPEAT THEN 2DROP ;
String-slash $/ has been introduced many times. You can steal it from
ciforth, there is a reference implementation in the library forth.lab.
Beware for counterfeit. If you use your garden variety string SPLIT, you
will be in for nasty surprises w.r.t. empty strings.
Groetjes Albert
Hugh, chalk me up for posting real Forth code.
--
-- Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
> With half decent string handling like in lina it is one-cent-flute to
> add Ruby inspired, #-formatted strings to Forth.
> The brilliant idea is to use # to introduce a Forth word.
> This is for the beta version of lina64 with nosecurity.
> Your professional Forth probably has no security :-) then
> don't bother.
> printed during compilation
> we go to ROME with 123456788 cows
> format 5 0 results in:
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> vamos a ROME con $0000,0000,0000,0000,0000,0000,075B,CD14 vacas
> format 5 1 results in:
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
> If you prefer one liners.
> \ Print X1 .. Xn using the format STRING.
> : .format BEGIN &# $/ $. OVER WHILE BL $/ 2SWAP 2>R EVALUATE 2R>
> OVER WHILE REPEAT THEN 2DROP ;
> String-slash $/ has been introduced many times. You can steal it from
> ciforth, there is a reference implementation in the library forth.lab.
> Beware for counterfeit. If you use your garden variety string SPLIT, you
> will be in for nasty surprises w.r.t. empty strings.
> Groetjes Albert
> Hugh, chalk me up for posting real Forth code.
> --
> --
> Albert van der Horst, UTRECHT,THE NETHERLANDS
> Economic growth -- being exponential -- ultimately falters.
> albert@spe&ar&c.xs4all.nl &=nhttp://home.hccnet.nl/a.w.m.van.der.horst
Hi!
Some time ago I wrote this string formatter in lina:
8<---
\ ******************
\ *** nmold$.fs ***
\ ******************
\ *** PROVIDES: ****
\ MOLD$ ( $N .. $1 FORMAT$ BUFFER$ -- BUFFER$.ADDR #BYTES.WRITED
OVERFLOWED? )
\ String Formatter
\ $N - n-th string constant parameter
\ $1 - first string constant parameter
\ FORMAT$ - string constant for `template' of form "ccc..%S..%S..."
\ BUFFER$ - destination buffer
\ OVERFLOWED? - flag if destination buffer was overflowed
\ ****************
On May 8, 6:04 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
wrote:
> With half decent string handling like in lina it is one-cent-flute to
> add Ruby inspired, #-formatted strings to Forth.
> The brilliant idea is to use # to introduce a Forth word.
> ...
> If you prefer one liners.
> \ Print X1 .. Xn using the format STRING.
> : .format BEGIN &# $/ $. OVER WHILE BL $/ 2SWAP 2>R EVALUATE 2R>
> OVER WHILE REPEAT THEN 2DROP ;
> String-slash $/ has been introduced many times. You can steal it from
> ciforth, there is a reference implementation in the library forth.lab.
> Beware for counterfeit. If you use your garden variety string SPLIT, you
> will be in for nasty surprises w.r.t. empty strings.
modified for garden-variety SPLIT
: .format
BEGIN
[char] # split type
dup
WHILE
1 /string
BL split 2SWAP 2>R EVALUATE 2R>
dup 0=
UNTIL THEN
2DROP ;
> On May 8, 6:04 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
> wrote:
>> With half decent string handling like in lina it is one-cent-flute to
>> add Ruby inspired, #-formatted strings to Forth.
>> The brilliant idea is to use # to introduce a Forth word.
>> ...
>> If you prefer one liners.
>> \ Print X1 .. Xn using the format STRING.
>>: .format BEGIN &# $/ $. OVER WHILE BL $/ 2SWAP 2>R EVALUATE 2R>
>> OVER WHILE REPEAT THEN 2DROP ;
>> String-slash $/ has been introduced many times. You can steal it from
>> ciforth, there is a reference implementation in the library forth.lab.
>> Beware for counterfeit. If you use your garden variety string SPLIT, you
>> will be in for nasty surprises w.r.t. empty strings.
> modified for garden-variety SPLIT
>: .format
> BEGIN
> [char] # split type
> dup
> WHILE
> 1 /string
> BL split 2SWAP 2>R EVALUATE 2R>
> dup 0=
> UNTIL THEN
> 2DROP ;
Why the '1 /string' ? Does the splitting leave the character where the
string is split, in the string?
Coos Haak wrote:
> Op Thu, 10 May 2012 20:49:55 +1000 schreef Ed:
> > On May 8, 6:04 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
> > wrote:
> >> With half decent string handling like in lina it is one-cent-flute to
> >> add Ruby inspired, #-formatted strings to Forth.
> >> The brilliant idea is to use # to introduce a Forth word.
> >> ...
> >> If you prefer one liners.
> >> \ Print X1 .. Xn using the format STRING.
> >>: .format BEGIN &# $/ $. OVER WHILE BL $/ 2SWAP 2>R EVALUATE 2R>
> >> OVER WHILE REPEAT THEN 2DROP ;
> >> String-slash $/ has been introduced many times. You can steal it from
> >> ciforth, there is a reference implementation in the library forth.lab.
> >> Beware for counterfeit. If you use your garden variety string SPLIT, you
> >> will be in for nasty surprises w.r.t. empty strings.
> > modified for garden-variety SPLIT
> >: .format
> > BEGIN
> > [char] # split type
> > dup
> > WHILE
> > 1 /string
> > BL split 2SWAP 2>R EVALUATE 2R>
> > dup 0=
> > UNTIL THEN
> > 2DROP ;
> Why the '1 /string' ? Does the splitting leave the character where the
> string is split, in the string?
Yes, because the scanned character may represent the start of a
substring in which case you'll want to split the string just before the
character. If the character is a delimiter and no longer required the
app can always discard it with 1 /STRING .
Ed <inva...@nospam.com> wrote:
>On May 8, 6:04 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
>wrote:
>> With half decent string handling like in lina it is one-cent-flute to
>> add Ruby inspired, #-formatted strings to Forth.
>> The brilliant idea is to use # to introduce a Forth word.
>> ...
>> If you prefer one liners.
>> \ Print X1 .. Xn using the format STRING.
>> : .format BEGIN &# $/ $. OVER WHILE BL $/ 2SWAP 2>R EVALUATE 2R>
>> OVER WHILE REPEAT THEN 2DROP ;
>> String-slash $/ has been introduced many times. You can steal it from
>> ciforth, there is a reference implementation in the library forth.lab.
>> Beware for counterfeit. If you use your garden variety string SPLIT, you
>> will be in for nasty surprises w.r.t. empty strings.
>modified for garden-variety SPLIT
>: .format
> BEGIN
> [char] # split type
> dup
> WHILE
> 1 /string
> BL split 2SWAP 2>R EVALUATE 2R>
> dup 0=
> UNTIL THEN
> 2DROP ;
This probably works. It is not equivalent as this will always
print the BL after a formatter. Another 1 /string helps out.
: .format
BEGIN
[char] # split type
dup
WHILE
1 /string
BL split 2SWAP 2>R EVALUATE 2R>
1 /string
dup 0=
UNTIL THEN
2DROP ;
This is exactly what I meant, with my $/ you have no need to
carefully analyse lots of cases. If the address is NULL you
have no string. If the length is 0 there is a string allbeith short.
Use it appropriately and it works.
Groetjes Albert
--
-- Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
> In article <jog6hj$7o...@news-01.bur.connect.com.au>,
> Ed <inva...@nospam.com> wrote:
> ...
> >modified for garden-variety SPLIT
> >: .format
> > BEGIN
> > [char] # split type
> > dup
> > WHILE
> > 1 /string
> > BL split 2SWAP 2>R EVALUATE 2R>
> > dup 0=
> > UNTIL THEN
> > 2DROP ;
> This probably works. It is not equivalent as this will always
> print the BL after a formatter. Another 1 /string helps out.
> : .format
> BEGIN
> [char] # split type
> dup
> WHILE
> 1 /string
> BL split 2SWAP 2>R EVALUATE 2R>
> 1 /string
> dup 0=
> UNTIL THEN
> 2DROP ;
Not quite. It would fail if the second string is zero.
> This is exactly what I meant, with my $/ you have no need to
> carefully analyse lots of cases. If the address is NULL you
> have no string. If the length is 0 there is a string allbeith short.
> Use it appropriately and it works.
Not sure I understand the need for a null address. Could not
your example have been written to test for zero length? e.g.
: .format
BEGIN
&# $/ $.
dup
WHILE
BL $/ 2SWAP 2>R EVALUATE 2R>
dup
WHILE
REPEAT THEN
2DROP ;
The direct equivalent using SPLIT would be
: .format
BEGIN
[char] # split type
dup
WHILE
1 /string
BL split 2SWAP 2>R EVALUATE 2R>
dup
WHILE
1 /string
REPEAT THEN
2DROP ;
SPLIT is per Wil Baden, SwiftForth etc. The rule for discarding the
delimiter seems to be: Apply 1 /STRING to the second string but
only if the length is not zero. For applications where the delimiter is
a blank it may be possible to omit the step altogether - though not
in your application :)