1 ! Copyright (C) 2008 Slava Pestov.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: arrays kernel kernel.private math sequences
\r
4 sequences.private growable byte-arrays accessors parser
\r
9 { underlying byte-array }
\r
10 { length array-capacity } ;
\r
12 : <byte-vector> ( n -- byte-vector )
\r
13 (byte-array) 0 byte-vector boa ; inline
\r
15 : >byte-vector ( seq -- byte-vector )
\r
16 T{ byte-vector f B{ } 0 } clone-like ;
\r
19 drop dup byte-vector? [
\r
21 [ dup length byte-vector boa ] [ >byte-vector ] if
\r
24 M: byte-vector new-sequence
\r
25 drop [ (byte-array) ] [ >fixnum ] bi byte-vector boa ;
\r
27 M: byte-vector equal?
\r
28 over byte-vector? [ sequence= ] [ 2drop f ] if ;
\r
31 #! If we have an byte-array, we're done.
\r
32 #! If we have a byte-vector, and it's at full capacity,
\r
33 #! we're done. Otherwise, call resize-byte-array, which is a
\r
34 #! relatively fast primitive.
\r
35 drop dup byte-array? [
\r
37 [ length ] [ underlying>> ] bi
\r
39 [ nip ] [ resize-byte-array ] if
\r
40 ] [ >byte-array ] if
\r
43 M: byte-array new-resizable drop <byte-vector> ;
\r
45 : BV{ \ } [ >byte-vector ] parse-literal ; parsing
\r
47 M: byte-vector pprint* pprint-object ;
\r
48 M: byte-vector pprint-delims drop \ BV{ \ } ;
\r
49 M: byte-vector >pprint-sequence ;
\r
51 INSTANCE: byte-vector growable
\r