Move byte-vectors and io.streams.byte-array to basis
parent
f8bce98850
commit
ca8091443c
|
@ -1,37 +0,0 @@
|
|||
USING: arrays byte-arrays help.markup help.syntax kernel
|
||||
byte-vectors.private combinators ;
|
||||
IN: byte-vectors
|
||||
|
||||
ARTICLE: "byte-vectors" "Byte vectors"
|
||||
"A byte vector is a resizable mutable sequence of unsigned bytes. Byte vector words are found in the " { $vocab-link "byte-vectors" } " vocabulary."
|
||||
$nl
|
||||
"Byte vectors form a class:"
|
||||
{ $subsection byte-vector }
|
||||
{ $subsection byte-vector? }
|
||||
"Creating byte vectors:"
|
||||
{ $subsection >byte-vector }
|
||||
{ $subsection <byte-vector> }
|
||||
"Literal syntax:"
|
||||
{ $subsection POSTPONE: BV{ }
|
||||
"If you don't care about initial capacity, a more elegant way to create a new byte vector is to write:"
|
||||
{ $code "BV{ } clone" } ;
|
||||
|
||||
ABOUT: "byte-vectors"
|
||||
|
||||
HELP: byte-vector
|
||||
{ $description "The class of resizable byte vectors. See " { $link "byte-vectors" } " for information." } ;
|
||||
|
||||
HELP: <byte-vector>
|
||||
{ $values { "n" "a positive integer specifying initial capacity" } { "byte-vector" byte-vector } }
|
||||
{ $description "Creates a new byte vector that can hold " { $snippet "n" } " bytes before resizing." } ;
|
||||
|
||||
HELP: >byte-vector
|
||||
{ $values { "seq" "a sequence" } { "byte-vector" byte-vector } }
|
||||
{ $description "Outputs a freshly-allocated byte vector with the same elements as a given sequence." }
|
||||
{ $errors "Throws an error if the sequence contains elements other than integers." } ;
|
||||
|
||||
HELP: BV{
|
||||
{ $syntax "BV{ elements... }" }
|
||||
{ $values { "elements" "a list of bytes" } }
|
||||
{ $description "Marks the beginning of a literal byte vector. Literal byte vectors are terminated by " { $link POSTPONE: } } "." }
|
||||
{ $examples { $code "BV{ 1 2 3 12 }" } } ;
|
|
@ -1,17 +0,0 @@
|
|||
IN: byte-vectors.tests
|
||||
USING: tools.test byte-vectors vectors sequences kernel
|
||||
prettyprint ;
|
||||
|
||||
[ 0 ] [ 123 <byte-vector> length ] unit-test
|
||||
|
||||
: do-it
|
||||
123 [ over push ] each ;
|
||||
|
||||
[ t ] [
|
||||
3 <byte-vector> do-it
|
||||
3 <vector> do-it sequence=
|
||||
] unit-test
|
||||
|
||||
[ t ] [ BV{ } byte-vector? ] unit-test
|
||||
|
||||
[ "BV{ }" ] [ BV{ } unparse ] unit-test
|
|
@ -1,44 +0,0 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: arrays kernel kernel.private math sequences
|
||||
sequences.private growable byte-arrays accessors ;
|
||||
IN: byte-vectors
|
||||
|
||||
TUPLE: byte-vector
|
||||
{ underlying byte-array }
|
||||
{ length array-capacity } ;
|
||||
|
||||
: <byte-vector> ( n -- byte-vector )
|
||||
<byte-array> 0 byte-vector boa ; inline
|
||||
|
||||
: >byte-vector ( seq -- byte-vector )
|
||||
T{ byte-vector f B{ } 0 } clone-like ;
|
||||
|
||||
M: byte-vector like
|
||||
drop dup byte-vector? [
|
||||
dup byte-array?
|
||||
[ dup length byte-vector boa ] [ >byte-vector ] if
|
||||
] unless ;
|
||||
|
||||
M: byte-vector new-sequence
|
||||
drop [ <byte-array> ] [ >fixnum ] bi byte-vector boa ;
|
||||
|
||||
M: byte-vector equal?
|
||||
over byte-vector? [ sequence= ] [ 2drop f ] if ;
|
||||
|
||||
M: byte-array like
|
||||
#! If we have an byte-array, we're done.
|
||||
#! If we have a byte-vector, and it's at full capacity,
|
||||
#! we're done. Otherwise, call resize-byte-array, which is a
|
||||
#! relatively fast primitive.
|
||||
drop dup byte-array? [
|
||||
dup byte-vector? [
|
||||
[ length ] [ underlying>> ] bi
|
||||
2dup length eq?
|
||||
[ nip ] [ resize-byte-array ] if
|
||||
] [ >byte-array ] if
|
||||
] unless ;
|
||||
|
||||
M: byte-array new-resizable drop <byte-vector> ;
|
||||
|
||||
INSTANCE: byte-vector growable
|
|
@ -1 +0,0 @@
|
|||
Growable byte arrays
|
|
@ -1 +0,0 @@
|
|||
collections
|
|
@ -1,34 +0,0 @@
|
|||
USING: help.syntax help.markup io byte-arrays quotations ;
|
||||
IN: io.streams.byte-array
|
||||
|
||||
ABOUT: "io.streams.byte-array"
|
||||
|
||||
ARTICLE: "io.streams.byte-array" "Byte-array streams"
|
||||
"Byte array streams:"
|
||||
{ $subsection <byte-reader> }
|
||||
{ $subsection <byte-writer> }
|
||||
"Utility combinators:"
|
||||
{ $subsection with-byte-reader }
|
||||
{ $subsection with-byte-writer } ;
|
||||
|
||||
HELP: <byte-reader>
|
||||
{ $values { "byte-array" byte-array }
|
||||
{ "encoding" "an encoding descriptor" }
|
||||
{ "stream" "a new byte reader" } }
|
||||
{ $description "Creates an input stream reading from a byte array using an encoding." } ;
|
||||
|
||||
HELP: <byte-writer>
|
||||
{ $values { "encoding" "an encoding descriptor" }
|
||||
{ "stream" "a new byte writer" } }
|
||||
{ $description "Creates an output stream writing data to a byte array using an encoding." } ;
|
||||
|
||||
HELP: with-byte-reader
|
||||
{ $values { "encoding" "an encoding descriptor" }
|
||||
{ "quot" quotation } { "byte-array" byte-array } }
|
||||
{ $description "Calls the quotation in a new dynamic scope with " { $link input-stream } " rebound to an input stream for reading from a byte array using an encoding." } ;
|
||||
|
||||
HELP: with-byte-writer
|
||||
{ $values { "encoding" "an encoding descriptor" }
|
||||
{ "quot" quotation }
|
||||
{ "byte-array" byte-array } }
|
||||
{ $description "Calls the quotation in a new dynamic scope with " { $link output-stream } " rebound to an output stream writing data to a byte array using an encoding." } ;
|
|
@ -1,9 +0,0 @@
|
|||
USING: tools.test io.streams.byte-array io.encodings.binary
|
||||
io.encodings.utf8 io kernel arrays strings ;
|
||||
|
||||
[ B{ 1 2 3 } ] [ binary [ { 1 2 3 } write ] with-byte-writer ] unit-test
|
||||
[ B{ 1 2 3 } ] [ { 1 2 3 } binary [ 3 read ] with-byte-reader ] unit-test
|
||||
|
||||
[ B{ BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 10111111 BIN: 11101111 BIN: 10000000 BIN: 10111111 BIN: 11011111 BIN: 10000000 CHAR: x } ]
|
||||
[ { BIN: 101111111000000111111 BIN: 1111000000111111 BIN: 11111000000 CHAR: x } utf8 [ write ] with-byte-writer ] unit-test
|
||||
[ { BIN: 101111111000000111111 } t ] [ { BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 10111111 } utf8 <byte-reader> contents dup >array swap string? ] unit-test
|
|
@ -1,16 +0,0 @@
|
|||
USING: byte-arrays byte-vectors kernel io.encodings io.streams.string
|
||||
sequences io namespaces io.encodings.private accessors ;
|
||||
IN: io.streams.byte-array
|
||||
|
||||
: <byte-writer> ( encoding -- stream )
|
||||
512 <byte-vector> swap <encoder> ;
|
||||
|
||||
: with-byte-writer ( encoding quot -- byte-array )
|
||||
[ <byte-writer> ] dip [ output-stream get ] compose with-output-stream*
|
||||
dup encoder? [ stream>> ] when >byte-array ; inline
|
||||
|
||||
: <byte-reader> ( byte-array encoding -- stream )
|
||||
[ >byte-vector dup reverse-here ] dip <decoder> ;
|
||||
|
||||
: with-byte-reader ( byte-array encoding quot -- )
|
||||
[ <byte-reader> ] dip with-input-stream* ; inline
|
Loading…
Reference in New Issue