master/library/data/strings.retro
Commiter: Charles Childers
Author: Charles Childers
Revision: 4de7dc8b06
File Size: 2.3 KB
(May 01, 2010 22:39 UTC) About 2 years ago
add chomp to data/strings
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( String Manipulation )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Copyright [c] 2009 - 2010, Luke Parrish )
( Copyright [c] 2010, Marc Simpson )
( License: ISC )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
: prepend ( $$-$ )
here -rot
2 for
dup here swap
getLength dup
allot copy
next
0 , ;
: append ( $$-$ ) swap prepend ;
: split ( $c-$$ )
tib !
here swap
dup getLength dup
for 2dup r - +
@ dup tib @
=if drop
here 1+ -rot
0 then ,
next
0 , 2drop ;
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Convert counted and zero-terminated strings. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
{{
create buffer 64 allot
---reveal---
: cs ( $-an ) tempString dup getLength ;
: zt ( an-$ ) tuck buffer swap copy 0 swap buffer + ! buffer tempString ;
: cstype fori over + @ emit nexti ;
}}
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Append/prepend counted strings. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
{{
: sav ( n-n ) ` dup ` push ;
---reveal---
: csprepend ( anan-an )
here sav swap sav dup allot copy
here swap sav dup allot copy
pop pop + pop swap ;
: csappend ( anan-an ) push -rot pop -rot prepend ;
}}
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Get input into a string )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
: accept$ ( $c- )
push repeat ekey dup r =if pop 2drop 0 swap ! ;then swap !+ again ;
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Search for a character in a string. Returns the address or )
( 0 if not found. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
: scan ( $c-a )
push
repeat @+
dup 0 =if rdrop 2drop 0 ;then
r =if rdrop 1- ;then
again ;
( Trim trailing spaces from a string ~~~~~~~~~~~~~~~~~~~~~~~~ )
: chomp ( $-$ )
dup dup getLength + 1-
dup @ 32 =if 0 swap ! heap -- dup 1- -- chomp ;then drop ; |