2 USING: accessors alien alien.data arrays byte-arrays destructors
3 io.buffers kernel libc namespaces sequences strings tools.test ;
5 : buffer-set ( string buffer -- )
6 [ ptr>> swap >byte-array binary-object memcpy ]
7 [ [ length ] dip buffer-reset ]
10 : string>buffer ( string -- buffer )
11 dup length <buffer> [ buffer-set ] keep ;
13 : buffer-read-all ( buffer -- byte-array )
14 [ buffer@ ] [ buffer-length ] bi memory>byte-array ;
23 { "hello world" "" } [
24 "hello world" string>buffer
25 dup buffer-read-all >string
27 over buffer-read-all >string
32 "hello world" string>buffer
33 5 over buffer-read >string swap dispose
37 "hello world" string>buffer
38 [ buffer-length ] keep dispose
42 "hello" 1024 <buffer> [ buffer-set ] keep
43 " world" >byte-array binary-object pick buffer-write
44 dup buffer-read-all >string swap dispose
49 1 over buffer-consume [ buffer-pop ] keep dispose
52 "hello world" string>buffer "b" set
53 { "hello world" } [ 1000 "b" get buffer-read >string ] unit-test
57 [ 1000 "b" get buffer+ >string ] must-fail
60 "hello world" string>buffer "b" set
61 { "hello" CHAR: \s } [ " " "b" get buffer-read-until [ >string ] dip ] unit-test
64 "hello world" string>buffer "b" set
65 { "hello worl" CHAR: d } [ "d" "b" get buffer-read-until [ >string ] dip ] unit-test
68 "hello world" string>buffer "b" set
69 { "hello world" f } [ "\n" "b" get buffer-read-until [ >string ] dip ] unit-test
72 { 4 B{ 1 2 3 4 0 0 0 0 0 0 } } [
74 [ B{ 1 2 3 4 } binary-object rot buffer-write ]
75 [ 10 <byte-array> [ 10 rot buffer-read-into ] keep ] bi
78 { 4 { 1 2 3 4 f f f f f f } } [
80 [ B{ 1 2 3 4 } binary-object rot buffer-write ]
81 [ 10 f <array> [ 10 rot buffer-read-into ] keep ] bi