Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > string concaten...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 11 of 43 Topic 4028 of 4053
Post > Topic >>

string concatenation (was: What's right with FORTH ...)

by anton@[EMAIL PROTECTED] (Anton Ertl) May 1, 2008 at 03:28 PM

Jonah Thomas <jethomas5@[EMAIL PROTECTED]
> writes:
>The weakness I see is concatenating strings.

Yes, that's one of the few things that I define beyond what the
standard provides.  Here's what I have used lately:

: s+ { addr1 u1 addr2 u2 -- addr u }
    u1 u2 + allocate throw { addr }
    addr1 addr u1 move
    addr2 addr u1 + u2 move
    addr u1 u2 +
;

: append { addr1 u1 addr2 u2 -- addr u }
    addr1 u1 u2 + dup { u } resize throw { addr }
    addr2 addr u1 + u2 move
    addr u ;

S+ is for the case where the concatenated string should be newly
allocated.  APPEND is for the case where the first string is ALLOCATEd
and can be freed in the process.  Both words return ALLOCATEd strings.
A typical usage is:

    [ libtool-command s"  --silent --mode=compile gcc -I " s+
    s" includedir" getenv append ] sliteral
    s"  -O -c " s+ lib-filename 2@[EMAIL PROTECTED]
 append s" .c -o " append
    lib-filename 2@[EMAIL PROTECTED]
 append s" .lo" append ( c-addr u )
    2dup system drop free throw $? abort" libtool compile failed"

I.e., first you create an ALLOCATEd string with S+, then you APPEND to
it, then you use it (in SYSTEM this time), and finally you FREE it.

If I don't have to worry about FREEing, I can just use S+ everywhere.

All of that is not particularly efficient, though.

The other set of string words I use now and then is for comparison:

: str= ( c-addr1 u1 c-addr2 u2 -- f ) \ gforth
    compare 0= ;

: string-prefix? ( c-addr1 u1 c-addr2 u2 -- f ) \ gforth
    \G Is @[EMAIL PROTECTED]
 u2} a prefix of @[EMAIL PROTECTED]
 u1}?
    tuck 2>r min 2r> str= ;

: str< ( c-addr1 u1 c-addr2 u2 -- f ) \ gforth
    compare 0< ;

I don't compare strings that often, but when I do, usually one of
these words fits.

These words are defined in compat/strcomp.fs, so they can be used on
any standard system.

>If the point is to send the string to a file or a pipe etc, you don't
>have to concatenate strings, just send all the parts in the right order.
>You can get by sending the first part as soon as you know it, and then
>you don't have to keep track of it. 

I have been thinking lately about enhancing Forth with pipes/streams.
This would not just allow to use multiple hardware threads, it could
also help with some of the usual problems by introducing a new way of
factoring: Factor your computation into filters that talk to each
other.

For string concatenation, one could pipe the substrings to a filter
that just generates the full string; or one could pipe it to a filter
that outputs the individual characters as a stream (that would be a
stream representation of the string), which could then be piped into
other components.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2008:
http://www.complang.tuwien.ac.at/anton/euroforth/ef08.html




 43 Posts in Topic:
What's right with FORTH and wrong with c.l.f.
Richard Owlett <rowlet  2008-04-30 16:14:51 
Re: What's right with FORTH and wrong with c.l.f.
mhx@[EMAIL PROTECTED] (M  2008-05-01 00:00:16 
Re: What's right with FORTH and wrong with c.l.f.
mhx@[EMAIL PROTECTED] (M  2008-05-01 10:54:15 
Re: What's right with FORTH and wrong with c.l.f.
Albert van der Horst <  2008-05-01 14:39:26 
Re: What's right with FORTH and wrong with c.l.f.
mhx@[EMAIL PROTECTED] (M  2008-05-01 10:59:29 
Re: What's right with FORTH and wrong with c.l.f.
Albert van der Horst <  2008-05-01 14:31:31 
Re: What's right with FORTH and wrong with c.l.f.
brian.fox@[EMAIL PROTECTE  2008-04-30 18:14:16 
Re: What's right with FORTH and wrong with c.l.f.
Jonah Thomas <jethomas  2008-05-01 02:58:08 
Re: What's right with FORTH and wrong with c.l.f.
"The Beez'" <  2008-05-01 03:51:29 
Re: What's right with FORTH and wrong with c.l.f.
Jonah Thomas <jethomas  2008-05-01 09:57:10 
string concatenation (was: What's right with FORTH ...)
anton@[EMAIL PROTECTED]   2008-05-01 15:28:43 
Re: string concatenation (was: What's right with FORTH ...)
stephenXXX@[EMAIL PROTECT  2008-05-01 22:53:25 
Re: string concatenation (was: What's right with FORTH ...)
anton@[EMAIL PROTECTED]   2008-05-02 19:02:14 
Re: What's right with FORTH and wrong with c.l.f.
John Passaniti <nntp@[  2008-05-01 22:17:22 
Re: What's right with FORTH and wrong with c.l.f.
Albert van der Horst <  2008-05-01 14:57:46 
Re: What's right with FORTH and wrong with c.l.f.
Bruce McFarling <agila  2008-05-01 13:04:35 
Re: What's right with FORTH and wrong with c.l.f.
Jonah Thomas <jethomas  2008-05-01 18:34:31 
Re: What's right with FORTH and wrong with c.l.f.
Dick van Oudheusden <d  2008-05-02 03:07:49 
Re: What's right with FORTH and wrong with c.l.f.
Richard Owlett <rowlet  2008-05-02 15:58:00 
Re: What's right with FORTH and wrong with c.l.f.
Bruce McFarling <agila  2008-05-02 04:51:55 
Re: What's right with FORTH and wrong with c.l.f.
Aleksej Saushev <asau@  2008-05-02 22:02:30 
Re: What's right with FORTH and wrong with c.l.f.
Marc Olschok <nobody@[  2008-05-02 18:20:38 
Re: What's right with FORTH and wrong with c.l.f.
Aleksej Saushev <asau@  2008-05-03 09:56:47 
Re: What's right with FORTH and wrong with c.l.f.
Marc Olschok <nobody@[  2008-05-03 20:16:22 
Re: What's right with FORTH and wrong with c.l.f.
Aleksej Saushev <asau@  2008-05-04 23:59:26 
Re: What's right with FORTH and wrong with c.l.f.
Marc Olschok <nobody@[  2008-05-05 21:45:18 
Re: What's right with FORTH and wrong with c.l.f.
Aleksej Saushev <asau@  2008-05-07 21:46:15 
Re: What's right with FORTH and wrong with c.l.f.
Marc Olschok <nobody@[  2008-05-12 18:20:59 
Re: What's right with FORTH and wrong with c.l.f.
stephenXXX@[EMAIL PROTECT  2008-05-04 18:46:53 
Re: What's right with FORTH and wrong with c.l.f.
Richard Owlett <rowlet  2008-05-02 16:04:39 
Re: What's right with FORTH and wrong with c.l.f.
Bruce McFarling <agila  2008-05-02 11:45:25 
Re: What's right with FORTH and wrong with c.l.f.
Aleksej Saushev <asau@  2008-05-03 11:44:38 
Re: What's right with FORTH and wrong with c.l.f.
Aleksej Saushev <asau@  2008-05-03 11:57:11 
Re: What's right with FORTH and wrong with c.l.f.
Elizabeth D Rather <er  2008-05-03 07:27:16 
Re: What's right with FORTH and wrong with c.l.f.
Aleksej Saushev <asau@  2008-05-04 23:26:57 
Re: What's right with FORTH and wrong with c.l.f.
John Passaniti <nntp@[  2008-05-05 21:41:12 
Re: What's right with FORTH and wrong with c.l.f.
stephenXXX@[EMAIL PROTECT  2008-05-07 10:16:30 
Re: What's right with FORTH and wrong with c.l.f.
Dick van Oudheusden <d  2008-05-04 03:04:44 
Re: What's right with FORTH and wrong with c.l.f.
Bruce McFarling <agila  2008-05-05 16:35:43 
Re: What's right with FORTH and wrong with c.l.f.
astrobe <fdubois76@[EM  2008-05-07 05:08:02 
Re: What's right with FORTH and wrong with c.l.f.
Alex McDonald <blog@[E  2008-05-07 05:56:35 
Re: What's right with FORTH and wrong with c.l.f.
pml540114@[EMAIL PROTECTE  2008-05-15 06:21:25 
Re: What's right with FORTH and wrong with c.l.f.
Jonah Thomas <jethomas  2008-05-15 21:31:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 5:34:24 CDT 2008.