Skip to content

Commit 705474a

Browse files
committed
Merge pull request #2 from jhermsmeier/next
Add `encoding` parameter to `#decode()`
2 parents ccda93c + b8fa4ce commit 705474a

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var result = bencode.decode( data )
8585
Automagically convert bytestrings to strings:
8686

8787
```javascript
88-
var result = bencode.decode( data, true )
88+
var result = bencode.decode( data, 'utf8' )
8989
```
9090

9191
#### Output
@@ -109,12 +109,12 @@ var result = bencode.decode( data, true )
109109
110110
Returns `String`
111111

112-
### bencode.decode( *data*, *toString* )
112+
### bencode.decode( *data*, *encoding* )
113113

114114
> `Buffer` __data__
115-
> `Boolean` __toString__
115+
> `String` __encoding__
116116
117-
If `toString` is set to something truthy,
118-
bytestrings are automatically converted to strings.
117+
If `encoding` is set, bytestrings are
118+
automatically converted to strings.
119119

120120
Returns `Object` | `Array` | `Buffer` | `String` | `Number`

benchmark/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@ var fs = require( 'fs' )
77
var suite = new Benchmark.Suite
88

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

1313
suite
14-
.add( 'new#encode( Object )', function () {
14+
.add( 'new.encode( Object )', function () {
1515
bencode.encode( data )
1616
})
17-
.add( 'old#encode( Object )', function () {
17+
.add( 'old.encode( Object )', function () {
1818
old.encode( data )
1919
})
20-
.add( 'new#decode( Buffer )', function () {
20+
.add( 'new.decode( Buffer )', function () {
2121
bencode.decode( buffer )
2222
})
23-
.add( 'new#decode( Buffer, true )', function () {
24-
bencode.decode( buffer, true )
23+
.add( 'new.decode( Buffer, "ascii" )', function () {
24+
bencode.decode( buffer, 'ascii' )
2525
})
26-
.add( 'old#decode( String )', function () {
26+
.add( 'new.decode( Buffer, "utf8" )', function () {
27+
bencode.decode( buffer, 'utf8' )
28+
})
29+
.add( 'old.decode( String )', function () {
2730
old.decode( string )
2831
})
2932
.on( 'cycle', function ( event, bench ) {

bencode.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,17 @@ encode.list = function( data ) {
6363
* Decodes bencoded data.
6464
*
6565
* @param {Buffer} data
66-
* @param {Boolean} toString
66+
* @param {String} encoding
6767
* @return {Object|Array|Buffer|String|Number}
6868
*/
69-
function decode( data, toString ) {
69+
function decode( data, encoding ) {
7070

7171
if( !(this instanceof decode) ) {
72-
return new decode( data, toString )
72+
return new decode( data, encoding )
7373
}
7474

75-
this.stringify = !!toString
76-
77-
this.data = data
75+
this.encoding = encoding || null
76+
this.data = data
7877

7978
return this.next()
8079

@@ -88,7 +87,7 @@ decode.prototype = {
8887
case 0x64: return this.dictionary()
8988
case 0x6C: return this.list()
9089
case 0x69: return this.integer()
91-
default: return this.bytes()
90+
default: return this.bytes()
9291
}
9392

9493
},
@@ -166,8 +165,8 @@ decode.prototype = {
166165

167166
this.forward( sepl )
168167

169-
return this.stringify
170-
? bytes.toString()
168+
return this.encoding
169+
? bytes.toString( this.encoding )
171170
: bytes
172171

173172
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22

33
"name": "bencode",
4-
"version": "0.2.0",
4+
"version": "0.3.0",
55
"description": "Bencode de/encoder",
66
"keywords": [ "torrent", "bittorrent", "bencode", "bdecode", "bencoding" ],
77

0 commit comments

Comments
 (0)