Skip to content

Commit

Permalink
Merge pull request #2 from jhermsmeier/next
Browse files Browse the repository at this point in the history
Add `encoding` parameter to `#decode()`
  • Loading branch information
themasch committed Jul 23, 2012
2 parents ccda93c + b8fa4ce commit 705474a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var result = bencode.decode( data )
Automagically convert bytestrings to strings:

```javascript
var result = bencode.decode( data, true )
var result = bencode.decode( data, 'utf8' )
```

#### Output
Expand All @@ -109,12 +109,12 @@ var result = bencode.decode( data, true )
Returns `String`

### bencode.decode( *data*, *toString* )
### bencode.decode( *data*, *encoding* )

> `Buffer` __data__
> `Boolean` __toString__
> `String` __encoding__
If `toString` is set to something truthy,
bytestrings are automatically converted to strings.
If `encoding` is set, bytestrings are
automatically converted to strings.

Returns `Object` | `Array` | `Buffer` | `String` | `Number`
17 changes: 10 additions & 7 deletions benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ var fs = require( 'fs' )
var suite = new Benchmark.Suite

var buffer = fs.readFileSync( __dirname + '/test.torrent' )
var data = bencode.decode( buffer, true )
var data = bencode.decode( buffer, 'ascii' )
var string = bencode.encode( data )

suite
.add( 'new#encode( Object )', function () {
.add( 'new.encode( Object )', function () {
bencode.encode( data )
})
.add( 'old#encode( Object )', function () {
.add( 'old.encode( Object )', function () {
old.encode( data )
})
.add( 'new#decode( Buffer )', function () {
.add( 'new.decode( Buffer )', function () {
bencode.decode( buffer )
})
.add( 'new#decode( Buffer, true )', function () {
bencode.decode( buffer, true )
.add( 'new.decode( Buffer, "ascii" )', function () {
bencode.decode( buffer, 'ascii' )
})
.add( 'old#decode( String )', function () {
.add( 'new.decode( Buffer, "utf8" )', function () {
bencode.decode( buffer, 'utf8' )
})
.add( 'old.decode( String )', function () {
old.decode( string )
})
.on( 'cycle', function ( event, bench ) {
Expand Down
17 changes: 8 additions & 9 deletions bencode.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@ encode.list = function( data ) {
* Decodes bencoded data.
*
* @param {Buffer} data
* @param {Boolean} toString
* @param {String} encoding
* @return {Object|Array|Buffer|String|Number}
*/
function decode( data, toString ) {
function decode( data, encoding ) {

if( !(this instanceof decode) ) {
return new decode( data, toString )
return new decode( data, encoding )
}

this.stringify = !!toString

this.data = data
this.encoding = encoding || null
this.data = data

return this.next()

Expand All @@ -88,7 +87,7 @@ decode.prototype = {
case 0x64: return this.dictionary()
case 0x6C: return this.list()
case 0x69: return this.integer()
default: return this.bytes()
default: return this.bytes()
}

},
Expand Down Expand Up @@ -166,8 +165,8 @@ decode.prototype = {

this.forward( sepl )

return this.stringify
? bytes.toString()
return this.encoding
? bytes.toString( this.encoding )
: bytes

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{

"name": "bencode",
"version": "0.2.0",
"version": "0.3.0",
"description": "Bencode de/encoder",
"keywords": [ "torrent", "bittorrent", "bencode", "bdecode", "bencoding" ],

Expand Down

0 comments on commit 705474a

Please sign in to comment.