2 USING: alien alien.c-types alien.data io.buffers kernel
3 kernel.private libc sequences tools.test namespaces byte-arrays
4 strings accessors destructors ;
6 : buffer-set ( string buffer -- )
7 [ ptr>> swap >byte-array binary-object memcpy ]
8 [ [ length ] dip buffer-reset ]
11 : string>buffer ( string -- buffer )
12 dup length <buffer> [ buffer-set ] keep ;
14 : buffer-read-all ( buffer -- byte-array )
15 [ [ pos>> ] [ ptr>> ] bi <displaced-alien> ]
26 [ "hello world" "" ] [
27 "hello world" string>buffer
28 dup buffer-read-all >string
30 over buffer-read-all >string
35 "hello world" string>buffer
36 5 over buffer-read >string swap dispose
40 "hello world" string>buffer
41 [ buffer-length ] keep dispose
45 "hello" 1024 <buffer> [ buffer-set ] keep
46 " world" >byte-array over >buffer
47 dup buffer-read-all >string swap dispose
52 1 over buffer-consume [ buffer-pop ] keep dispose
55 "hello world" string>buffer "b" set
56 [ "hello world" ] [ 1000 "b" get buffer-read >string ] unit-test
60 [ 1000 "b" get n>buffer >string ] must-fail
63 "hello world" string>buffer "b" set
64 [ "hello" CHAR: \s ] [ " " "b" get buffer-until [ >string ] dip ] unit-test