Skip to content

Instantly share code, notes, and snippets.

@dunnousername
Last active August 11, 2019 22:56
Show Gist options
  • Save dunnousername/99c4568dc2af23eef544f67ef97e1a19 to your computer and use it in GitHub Desktop.
Save dunnousername/99c4568dc2af23eef544f67ef97e1a19 to your computer and use it in GitHub Desktop.
Bencode in smalltalk. (first smalltalk program). bencode_full.st is a later revision that should be able to both encode and decode.
Integer extend [
bencode: stream [
stream nextPutAll: 'i'.
stream nextPutAll: (self storeString).
stream nextPutAll: 'e'.
]
]
OrderedCollection extend [
bencode: stream [
stream nextPutAll: 'l'.
self do: [:a | a bencode: stream ].
stream nextPutAll: 'e'.
]
]
ByteArray extend [
bencode: stream [
stream nextPutAll: ((self size) storeString).
stream nextPutAll: ':'.
stream nextPutAll: self.
]
]
String extend [
bencode: stream [
self asByteArray bencode: stream
]
]
Dictionary extend [
bencode: stream [
stream nextPutAll: 'd'.
self keys sorted do: [:k |
k bencode: stream.
(self at: k) bencode: stream.
].
stream nextPutAll: 'e'.
]
]
x := Dictionary new.
x add: #a->4; add: #c->7; add: #b->3.
x bencode: stdout
Character extend [
bencodedType [
self == $i ifTrue: [^Integer].
self isDigit ifTrue: [^ByteArray].
self == $l ifTrue: [^OrderedCollection].
^Dictionary.
]
]
Integer extend [
bencode: stream [
stream nextPutAll: 'i'.
stream nextPutAll: (self storeString).
stream nextPutAll: 'e'.
]
]
Integer class extend [
bdecode: stream [
| r |
r := ((stream upTo: $e) select: [:a | (a isDigit) | (a == $-)]) asInteger.
"stream next."
^r.
]
]
OrderedCollection extend [
bencode: stream [
stream nextPutAll: 'l'.
self do: [:a | a bencode: stream ].
stream nextPutAll: 'e'.
]
]
OrderedCollection class extend [
bdecode: stream [
| r |
r := OrderedCollection new.
stream next.
[true] whileTrue: [
| a |
a := stream peek.
a == $e ifTrue: [
stream next.
^r.
].
r add: ((a bencodedType) bdecode: stream).
]
]
]
ByteArray extend [
bencode: stream [
stream nextPutAll: ((self size) storeString).
stream nextPutAll: ':'.
stream nextPutAll: self.
]
]
ByteArray class extend [
bdecode: stream [
| l |
l := ((stream upTo: $:) select: [:a | (a isDigit)]) asInteger.
"stream next."
^(stream next: l) asByteArray.
]
]
String extend [
bencode: stream [
self asByteArray bencode: stream
]
]
String class extend [
bdecode: stream [
^((ByteArray bdecode: stream) asString).
]
]
Dictionary extend [
bencode: stream [
stream nextPutAll: 'd'.
self keys sorted do: [:k |
k bencode: stream.
(self at: k) bencode: stream.
].
stream nextPutAll: 'e'.
]
]
Dictionary class extend [
bdecode: stream [
| r |
r := Dictionary new.
stream next.
[true] whileTrue: [
| a b aa bb |
a := stream peek.
a == $e ifTrue: [
stream next.
^r.
].
aa := (a bencodedType) bdecode: stream.
b := stream peek.
b == $e ifTrue: [
stream next.
^r.
].
bb := (b bencodedType) bdecode: stream.
r add: aa->bb.
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment