bencode: support decoding byte-arrays.

flac
John Benediktsson 2020-01-09 11:57:20 -08:00 committed by Steve Ayerhart
parent 1adabc3168
commit f3dc124db2
No known key found for this signature in database
GPG Key ID: 5BFD39C5359E967D
2 changed files with 9 additions and 3 deletions

View File

@ -7,6 +7,7 @@ USING: bencode linked-assocs tools.test ;
{ "4:spam" } [ "spam" >bencode ] unit-test { "4:spam" } [ "spam" >bencode ] unit-test
{ "3:\x01\x02\x03" } [ B{ 1 2 3 } >bencode ] unit-test { "3:\x01\x02\x03" } [ B{ 1 2 3 } >bencode ] unit-test
{ "\x01\x02\x03" } [ B{ 51 58 1 2 3 } bencode> ] unit-test
{ { "spam" 42 } } [ "l4:spami42ee" bencode> ] unit-test { { "spam" 42 } } [ "l4:spami42ee" bencode> ] unit-test

View File

@ -1,6 +1,6 @@
USING: arrays assocs byte-arrays combinators io USING: arrays assocs byte-arrays combinators io
io.streams.string kernel linked-assocs math math.parser io.encodings.binary io.streams.byte-array io.streams.string
sequences strings ; kernel linked-assocs math math.parser sequences strings ;
IN: bencode IN: bencode
GENERIC: >bencode ( obj -- bencode ) GENERIC: >bencode ( obj -- bencode )
@ -50,5 +50,10 @@ PRIVATE>
[ read-string ] [ read-string ]
} case ; } case ;
: bencode> ( bencode -- obj ) GENERIC: bencode> ( bencode -- obj )
M: byte-array bencode>
binary [ read-bencode ] with-byte-reader ;
M: string bencode>
[ read-bencode ] with-string-reader ; [ read-bencode ] with-string-reader ;