]> gitweb.factorcode.org Git - factor.git/commitdiff
bencode: support decoding byte-arrays.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 9 Jan 2020 19:57:20 +0000 (11:57 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 9 Jan 2020 19:57:20 +0000 (11:57 -0800)
extra/bencode/bencode-tests.factor
extra/bencode/bencode.factor

index b9a1bcb0b482e255e0db6d0c1d75add8ebd6e80e..7fc9bb22380c9c5e5faea1a4078476abb9bfa7eb 100644 (file)
@@ -7,6 +7,7 @@ USING: bencode linked-assocs tools.test ;
 { "4:spam" } [ "spam" >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
 
index b23ea09c7141f7e6fbcf352c18a05dd3d4297ed8..fc4537477f1717f149bc7fad7cebacbff5a4ac0c 100644 (file)
@@ -1,6 +1,6 @@
 USING: arrays assocs byte-arrays combinators io
-io.streams.string kernel linked-assocs math math.parser
-sequences strings ;
+io.encodings.binary io.streams.byte-array io.streams.string
+kernel linked-assocs math math.parser sequences strings ;
 IN: bencode
 
 GENERIC: >bencode ( obj -- bencode )
@@ -50,5 +50,10 @@ PRIVATE>
         [ read-string ]
     } 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 ;