Diff for /gforth/string.fs between versions 1.6 and 1.18

version 1.6, 2005/12/31 15:46:10 version 1.18, 2012/09/17 18:46:46
Line 1 Line 1
 \ dynamic string handling                              10aug99py  \ dynamic string handling                              10aug99py
   
 \ Copyright (C) 2000,2005 Free Software Foundation, Inc.  \ Copyright (C) 2000,2005,2007,2010,2011 Free Software Foundation, Inc.
   
 \ This file is part of Gforth.  \ This file is part of Gforth.
   
 \ Gforth is free software; you can redistribute it and/or  \ Gforth is free software; you can redistribute it and/or
 \ modify it under the terms of the GNU General Public License  \ modify it under the terms of the GNU General Public License
 \ as published by the Free Software Foundation; either version 2  \ as published by the Free Software Foundation, either version 3
 \ of the License, or (at your option) any later version.  \ of the License, or (at your option) any later version.
   
 \ This program is distributed in the hope that it will be useful,  \ This program is distributed in the hope that it will be useful,
Line 15 Line 15
 \ GNU General Public License for more details.  \ GNU General Public License for more details.
   
 \ You should have received a copy of the GNU General Public License  \ You should have received a copy of the GNU General Public License
 \ along with this program; if not, write to the Free Software  \ along with this program. If not, see http://www.gnu.org/licenses/.
 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.  
   
 : delete   ( buffer size count -- )  [IFUNDEF] $!
   over min >r  r@ - ( left over )  dup 0>  : delete   ( buffer size n -- ) \ gforth-string
   IF  2dup swap dup  r@ +  -rot swap move  THEN  + r> bl fill ;      \G deletes the first @var{n} bytes from a buffer and fills the
       \G rest at the end with blanks.
 [IFUNDEF] insert      over min >r  r@ - ( left over )  dup 0>
 : insert   ( string length buffer size -- )      IF  2dup swap dup  r@ +  -rot swap move  THEN  + r> bl fill ;
   rot over min >r  r@ - ( left over )  
   over dup r@ +  rot move   r> move  ;  : insert   ( string length buffer size -- ) \ gforth-string
 [THEN]      \G inserts a string at the front of a buffer. The remaining
       \G bytes are moved on.
 : $padding ( n -- n' )      rot over min >r  r@ - ( left over )
   [ 6 cells ] Literal + [ -4 cells ] Literal and ;      over dup r@ +  rot move   r> move  ;
 : $! ( addr1 u addr2 -- )  
   dup @ IF  dup @ free throw  THEN  : $padding ( n -- n' ) \ gforth-string
   over $padding allocate throw over ! @      [ 6 cells ] Literal + [ -4 cells ] Literal and ;
   over >r  rot over cell+  r> move 2dup ! + cell+ bl swap c! ;  : $! ( addr1 u addr2 -- ) \ gforth-string string-store
 : $@len ( addr -- u )  @ @ ;      \G stores a string at an address, If there was a string there
 : $@ ( addr1 -- addr2 u )  @ dup cell+ swap @ ;      \G already, that string will be lost.
 : $!len ( u addr -- )      dup @ IF  dup @ free throw  THEN
   over $padding over @ swap resize throw over ! @ ! ;      over $padding allocate throw over ! @
 : $del ( addr off u -- )   >r >r dup $@ r> /string r@ delete      over >r  rot over cell+  r> move 2dup ! + cell+ bl swap c! ;
   dup $@len r> - swap $!len ;  : $@len ( addr -- u ) \ gforth-string string-fetch-len
 : $ins ( addr1 u addr2 off -- ) >r      \G returns the length of the stored string.
   2dup dup $@len rot + swap $!len  $@ 1+ r> /string insert ;      @ @ ;
 : $+! ( addr1 u addr2 -- ) dup $@len $ins ;  : $@ ( addr1 -- addr2 u ) \ gforth-string string-fetch
 : $off ( addr -- )  dup @ free throw off ;      \G returns the stored string.
       @ dup cell+ swap @ ;
   : $!len ( u addr -- ) \ gforth-string string-store-len
       \G changes the length of the stored string.  Therefore we must
       \G change the memory area and adjust address and count cell as
       \G well.
       over $padding over @ swap resize throw over ! @ ! ;
   : $del ( addr off u -- ) \ gforth-string string-del
       \G deletes @var{u} bytes from a string with offset @var{off}.
       >r >r dup $@ r> /string r@ delete
       dup $@len r> - swap $!len ;
   : $ins ( addr1 u addr2 off -- ) \ gforth-string string-ins
       \G inserts a string at offset @var{off}.
       >r 2dup dup $@len rot + swap $!len  $@ 1+ r> /string insert ;
   : $+! ( addr1 u addr2 -- ) \ gforth-string string-plus-store
       \G appends a string to another.
       dup $@len $ins ;
   : $off ( addr -- ) \ gforth-string string-off
       \G releases a string.
       dup @ dup IF  free throw off  ELSE  2drop  THEN ;
   : $init ( addr -- ) \ gforth-string string-init
       \G initializes a string to empty (doesn't look at what was there before).
       >r r@ off s" " r> $! ;
   
 \ dynamic string handling                              12dec99py  \ dynamic string handling                              12dec99py
   
 : $split ( addr u char -- addr1 u1 addr2 u2 )  : $split ( addr u char -- addr1 u1 addr2 u2 ) \ gforth-string string-split
   >r 2dup r> scan dup >r dup IF  1 /string  THEN      \G divides a string into two, with one char as separator (e.g. '?'
   2swap r> - 2swap ;      \G for arguments in an HTML query)
       >r 2dup r> scan dup >r dup IF  1 /string  THEN
 : $iter ( .. $addr char xt -- .. ) { char xt }      2swap r> - 2swap ;
   $@ BEGIN  dup  WHILE  char $split >r >r xt execute r> r>  
      REPEAT  2drop ;  : $iter ( .. $addr char xt -- .. ) \ gforth-string string-iter
       \G takes a string apart piece for piece, also with a character as
       \G separator. For each part a passed token will be called. With
       \G this you can take apart arguments -- separated with '&' -- at
       \G ease.
       >r >r
       $@ BEGIN  dup  WHILE  r@ $split i' -rot >r >r execute r> r>
       REPEAT  2drop rdrop rdrop ;
   
   \ string array words
   
   : $[] ( n addr -- addr' ) >r
       r@ @ 0= IF  s" " r@ $!  THEN
       r@ $@ 2 pick cells /string
       dup cell < IF
           2drop r@ $@len
           over 1+ cells r@ $!len
           r@ $@ rot /string 0 fill
           r@ $@ 2 pick cells /string
       THEN  drop nip rdrop ;
   
   : $[]! ( addr u n $addr -- )  $[] $! ;
   : $[]+! ( addr u n $addr -- )  $[] $+! ;
   : $[]@ ( n $addr -- addr u )  $[] dup @ IF $@ ELSE drop s" " THEN ;
   
   : $over ( addr u $addr off -- )
       \G overwrite string at offset off with addr u
       swap >r
       r@ @ 0= IF  s" " r@ $!  THEN
       2dup + r@ $@len > IF
           2dup + r@ $@len tuck max r@ $!len
           r@ $@ rot /string bl fill
       THEN
       r> $@ rot /string rot umin move ;
   [THEN]
   

Removed from v.1.6  
changed lines
  Added in v.1.18


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>