-USING: bencode tools.test ;
+USING: bencode linked-assocs tools.test ;
{ "i42e" } [ 42 >bencode ] unit-test
{ "i0e" } [ 0 >bencode ] unit-test
{ { "spam" 42 } } [ "l4:spami42ee" bencode> ] unit-test
-{ H{ { "bar" "spam" } { "foo" 42 } } } [
+{ LH{ { "bar" "spam" } { "foo" 42 } } } [
"d3:bar4:spam3:fooi42ee" bencode>
] unit-test
-USING: arrays assocs combinators hashtables io
-io.encodings.ascii io.encodings.string io.streams.string kernel
-math math.parser sequences strings ;
+USING: arrays assocs combinators io io.encodings.ascii
+io.encodings.string io.streams.string kernel linked-assocs math
+math.parser sequences strings ;
IN: bencode
GENERIC: >bencode ( obj -- bencode )
[ [ >bencode ] bi@ append ] { } assoc>map concat
"d" "e" surround ;
-<PRIVATE
-
DEFER: read-bencode
+<PRIVATE
+
: read-integer ( -- obj )
"e" read-until CHAR: e assert= string>number ;
: read-dictionary ( -- obj )
[
read-bencode [ read-bencode 2array ] [ f ] if* dup
- ] [ ] produce nip >hashtable ;
+ ] [ ] produce nip >linked-hash ;
: read-string ( prefix -- obj )
":" read-until CHAR: : assert= swap prefix
- string>number read ascii decode ;
+ string>number read "" like ;
+
+PRIVATE>
: read-bencode ( -- obj )
read1 {
[ read-string ]
} case ;
-PRIVATE>
-
: bencode> ( bencode -- obj )
[ read-bencode ] with-string-reader ;