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 over >byte-array over ptr>> byte-array>memory
8 [ length ] dip buffer-reset ;
10 : string>buffer ( string -- buffer )
11 dup length <buffer> tuck buffer-set ;
13 : buffer-read-all ( buffer -- byte-array )
14 [ [ pos>> ] [ ptr>> ] bi <displaced-alien> ]
25 [ "hello world" "" ] [
26 "hello world" string>buffer
27 dup buffer-read-all >string
29 over buffer-read-all >string
34 "hello world" string>buffer
35 5 over buffer-read >string swap dispose
39 "hello world" string>buffer
40 [ buffer-length ] keep dispose
44 "hello" 1024 <buffer> [ buffer-set ] keep
45 " world" >byte-array over >buffer
46 dup buffer-read-all >string swap dispose
51 1 over buffer-consume [ buffer-pop ] keep dispose
54 "hello world" string>buffer "b" set
55 [ "hello world" ] [ 1000 "b" get buffer-read >string ] unit-test
59 [ 1000 "b" get n>buffer >string ] must-fail