Skip to content

Commit 08a09bb

Browse files
committed
Use some more Binary/F methods for Buffer
1 parent b8bb6e9 commit 08a09bb

5 files changed

Lines changed: 58 additions & 79 deletions

File tree

doc/api.markdown

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ All of the examples in the documentation can be run similarly.
3131

3232
Pure Javascript is Unicode friendly but not nice to pure binary data. When
3333
dealing with TCP streams or the file system, it's necessary to handle octet
34-
streams. Node has several stratagies for manipulating, creating, and
34+
streams. Node has several strategies for manipulating, creating, and
3535
consuming octet streams.
3636

37-
Raw data is stored in instaces of the `Buffer` class. A `Buffer` is similar
37+
Raw data is stored in instances of the `Buffer` class. A `Buffer` is similar
3838
to an array of integers but correspond to a raw memory allocation outside
3939
the V8 heap. A `Buffer` cannot be resized.
4040
Access the class at `require('buffer').Buffer`.
@@ -59,29 +59,14 @@ Binary (`"binary"`). `"ascii"` and `"binary"` only look at the first 8 bits
5959
of the 16bit JavaScript string characters. The following `Buffer` methods
6060
allow decoding and encoding of strings:
6161

62-
- **`buffer.utf8Write(string, offset)`**: Writes `string` to the buffer at
63-
`offset` using UTF-8 encoding. Returns the number of octets written. If
62+
- **`buffer.write(string, encoding, offset)`**: Writes `string` to the buffer at
63+
`offset` using the given encoding. Returns number of octets written. If
6464
`buffer` did not contain enough space to fit the entire string it will write
65-
a partial amount of the string. However, this method will not write partial
66-
characters.
65+
a partial amount of the string. In the case of `encoding=='utf8'`, the
66+
method will not write partial characters.
6767

68-
- **`buffer.binaryWrite(string, offset)`**: Writes `string` to the buffer at
69-
`offset` using binary encoding - that is it will only use the first 8 bits
70-
of each character. Write a partial string if not enough space remains.
71-
Returns number of octets written.
72-
73-
- **`buffer.asciiWrite(string, offset)`**: Writes `string` to the buffer at
74-
`offset` using ASCII encoding. Faster than `utf8Write()`. Write a partial
75-
string if not enough space remains. Returns number of octets written.
76-
77-
- **`buffer.utf8Slice(start, end)`**: Decodes and returns a string assuming
78-
UTF-8 encoding beginning at `start` and ending at `end`.
79-
80-
- **`buffer.binarySlice(start, end)`**: Decodes and returns a string assuming
81-
binary encoding beginning at `start` and ending at `end`.
82-
83-
- **`buffer.asciiSlice(start, end)`**: Decodes and returns a string assuming
84-
ASCII encoding beginning at `start` and ending at `end`.
68+
- **`buffer.toString(encoding, start, end)`**: Decodes and returns a string assuming
69+
in the given encoding beginning at `start` and ending at `end`.
8570

8671

8772

lib/buffer.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,46 @@ Buffer.prototype.inspect = function () {
1818
};
1919

2020
Buffer.prototype.toString = function (encoding, start, stop) {
21-
encoding = encoding || 'utf8';
21+
encoding = (encoding || 'utf8').toLowerCase();
2222
if (!start) start = 0;
2323
if (!stop) stop = this.length;
2424

25-
if (encoding == 'utf8') {
26-
return this.utf8Slice(start, stop);
27-
} else if (encoding == 'ascii') {
28-
return this.asciiSlice(start, stop);
29-
} else if (encoding == 'binary') {
30-
return this.binarySlice(start, stop);
31-
} else {
32-
throw new Error('Unknown encoding');
25+
switch (encoding) {
26+
case 'utf8':
27+
case 'utf-8':
28+
return this.utf8Slice(start, stop);
29+
30+
case 'ascii':
31+
return this.asciiSlice(start, stop);
32+
33+
case 'binary':
34+
return this.binarySlice(start, stop);
35+
36+
default:
37+
throw new Error('Unknown encoding');
3338
}
3439
};
3540

41+
Buffer.prototype.write = function (string, encoding, offset) {
42+
encoding = (encoding || 'utf8').toLowerCase();
43+
switch (encoding) {
44+
case 'utf8':
45+
case 'utf-8':
46+
return this.utf8Write(string, offset);
47+
48+
case 'ascii':
49+
return this.asciiWrite(string, offset);
50+
51+
case 'binary':
52+
return this.binaryWrite(string, offset);
53+
54+
default:
55+
throw new Error('Unknown encoding');
56+
}
57+
};
58+
59+
60+
61+
62+
3663

lib/http.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function newParser (type) {
3131

3232
// Only servers will get URL events.
3333
parser.onURL = function (b, start, len) {
34-
var slice = b.asciiSlice(start, start+len);
34+
var slice = b.toString('ascii', start, start+len);
3535
if (parser.incoming.url) {
3636
parser.incoming.url += slice;
3737
} else {
@@ -41,7 +41,7 @@ function newParser (type) {
4141
};
4242

4343
parser.onHeaderField = function (b, start, len) {
44-
var slice = b.asciiSlice(start, start+len).toLowerCase();
44+
var slice = b.toString('ascii', start, start+len).toLowerCase();
4545
if (parser.value) {
4646
parser.incoming._addHeaderLine(parser.field, parser.value);
4747
parser.field = null;
@@ -55,7 +55,7 @@ function newParser (type) {
5555
};
5656

5757
parser.onHeaderValue = function (b, start, len) {
58-
var slice = b.asciiSlice(start, start+len);
58+
var slice = b.toString('ascii', start, start+len);
5959
if (parser.value) {
6060
parser.value += slice;
6161
} else {
@@ -88,20 +88,7 @@ function newParser (type) {
8888
if (!enc) {
8989
parser.incoming.emit('data', b.slice(start, start+len));
9090
} else {
91-
var string;
92-
switch (enc) {
93-
case 'utf8':
94-
string = b.utf8Slice(start, start+len);
95-
break;
96-
case 'ascii':
97-
string = b.asciiSlice(start, start+len);
98-
break;
99-
case 'binary':
100-
string = b.binarySlice(start, start+len);
101-
break;
102-
default:
103-
throw new Error('Unsupported encoding ' + enc + '. Use Buffer');
104-
}
91+
var string = b.toString(enc, start, start+len);
10592
parser.incoming.emit('data', string);
10693
}
10794
};

lib/net.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -308,22 +308,7 @@ function initStream (self) {
308308
// Optimization: emit the original buffer with end points
309309
if (self.ondata) self.ondata(pool, start, end);
310310
} else {
311-
// TODO remove me - we should only output Buffer
312-
313-
var string;
314-
switch (self._encoding) {
315-
case 'utf8':
316-
string = pool.utf8Slice(start, end);
317-
break;
318-
case 'ascii':
319-
string = pool.asciiSlice(start, end);
320-
break;
321-
case 'binary':
322-
string = pool.binarySlice(start, end);
323-
break;
324-
default:
325-
throw new Error('Unsupported encoding ' + self._encoding + '. Use Buffer');
326-
}
311+
var string = pool.toString(self._encoding, start, end);
327312
self.emit('data', string);
328313
}
329314
}
@@ -442,21 +427,16 @@ Stream.prototype._writeOut = function (data, encoding) {
442427
allocNewPool();
443428
}
444429

445-
if (encoding == 'binary') {
446-
bytesWritten = pool.binaryWrite(data, pool.used);
447-
charsWritten = bytesWritten;
448-
} else if (encoding == 'ascii') {
449-
bytesWritten = pool.asciiWrite(data, pool.used);
450-
charsWritten = bytesWritten;
451-
452-
} else {
430+
if (encoding == 'utf8' || encoding == 'utf-8') {
453431
// default to utf8
454-
bytesWritten = pool.utf8Write(data, pool.used);
432+
bytesWritten = pool.write(data, 'utf8', pool.used);
455433
// XXX Hacky way to find out the number of characters written.
456434
// Waiting for a more optimal way: http://codereview.chromium.org/1539013
457-
var _s = pool.utf8Slice(pool.used, pool.used + bytesWritten);
435+
var _s = pool.toString('utf8', pool.used, pool.used + bytesWritten);
458436
charsWritten = _s.length;
459-
437+
} else {
438+
bytesWritten = pool.write(data, encoding, pool.used);
439+
charsWritten = bytesWritten;
460440
}
461441

462442
assert(bytesWritten > 0);

test/simple/test-buffer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ for (var j = 0; j < 500; j++) {
3434
for (var i = 0; i < asciiString.length; i++) {
3535
b[i] = asciiString.charCodeAt(i);
3636
}
37-
var asciiSlice = b.asciiSlice(0, asciiString.length);
37+
var asciiSlice = b.toString('ascii', 0, asciiString.length);
3838
assert.equal(asciiString, asciiSlice);
3939

4040
var written = b.asciiWrite(asciiString, offset);
4141
assert.equal(asciiString.length, written);
42-
var asciiSlice = b.asciiSlice(offset, offset+asciiString.length);
42+
var asciiSlice = b.toString('ascii', offset, offset+asciiString.length);
4343
assert.equal(asciiString, asciiSlice);
4444

4545
var sliceA = b.slice(offset, offset+asciiString.length);
@@ -91,7 +91,7 @@ var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
9191
var buffer = new Buffer(32);
9292
var size = buffer.utf8Write(testValue, 0);
9393
puts('bytes written to buffer: ' + size);
94-
var slice = buffer.utf8Slice(0, size);
94+
var slice = buffer.toString('utf8', 0, size);
9595
assert.equal(slice, testValue);
9696

9797

0 commit comments

Comments
 (0)