| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1f8e4b5 commit ee83c95
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -451,7 +451,7 @@ additional performance that `Buffer.allocUnsafe(size)` provides. | |||
| 451 | 451 | ||
| 452 | 452 | ### Class Method: Buffer.byteLength(string[, encoding]) | |
| 453 | 453 | ||
| 454 | - * `string` {String} | ||
| 454 | + * `string` {String | Buffer | TypedArray | DataView | ArrayBuffer} | ||
| 455 | 455 | * `encoding` {String} Default: `'utf8'` | |
| 456 | 456 | * Return: {Number} | |
| 457 | 457 | ||
@@ -470,6 +470,11 @@ console.log(`${str}: ${str.length} characters, ` + | |||
| 470 | 470 | // ½ + ¼ = ¾: 9 characters, 12 bytes | |
| 471 | 471 | ``` | |
| 472 | 472 | ||
| 473 | + When `string` is a `Buffer`/[`DataView`][]/[`TypedArray`][]/`ArrayBuffer`, | ||
| 474 | + returns the actual byte length. | ||
| 475 | + | ||
| 476 | + Otherwise, converts to `String` and returns the byte length of string. | ||
| 477 | + | ||
| 473 | 478 | ### Class Method: Buffer.compare(buf1, buf2) | |
| 474 | 479 | ||
| 475 | 480 | * `buf1` {Buffer} | |
@@ -1744,4 +1749,6 @@ console.log(buf); | |||
| 1744 | 1749 | [buffer_from_string]: #buffer_class_method_buffer_from_str_encoding | |
| 1745 | 1750 | [buffer_allocunsafe]: #buffer_class_method_buffer_allocraw_size | |
| 1746 | 1751 | [buffer_alloc]: #buffer_class_method_buffer_alloc_size_fill_encoding | |
| 1747 | - [`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from | ||
| 1752 | + [`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from | ||
| 1753 | + [`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView | ||
| 1754 | + [`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -340,11 +340,12 @@ function base64ByteLength(str, bytes) { | |||
| 340 | 340 | ||
| 341 | 341 | ||
| 342 | 342 | function byteLength(string, encoding) { | |
| 343 | - if (string instanceof Buffer) | ||
| 344 | - return string.length; | ||
| 343 | + if (typeof string !== 'string') { | ||
| 344 | + if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer) | ||
| 345 | + return string.byteLength; | ||
| 345 | 346 | ||
| 346 | - if (typeof string !== 'string') | ||
| 347 | 347 | string = '' + string; | |
| 348 | + } | ||
| 348 | 349 | ||
| 349 | 350 | var len = string.length; | |
| 350 | 351 | if (len === 0) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,19 +3,53 @@ | |||
| 3 | 3 | require('../common'); | |
| 4 | 4 | var assert = require('assert'); | |
| 5 | 5 | var Buffer = require('buffer').Buffer; | |
| 6 | + var SlowBuffer = require('buffer').SlowBuffer; | ||
| 6 | 7 | ||
| 7 | 8 | // coerce values to string | |
| 8 | 9 | assert.equal(Buffer.byteLength(32, 'binary'), 2); | |
| 9 | 10 | assert.equal(Buffer.byteLength(NaN, 'utf8'), 3); | |
| 10 | 11 | assert.equal(Buffer.byteLength({}, 'binary'), 15); | |
| 11 | 12 | assert.equal(Buffer.byteLength(), 9); | |
| 12 | 13 | ||
| 14 | + var buff = new Buffer(10); | ||
| 15 | + assert(ArrayBuffer.isView(buff)); | ||
| 16 | + var slowbuff = new SlowBuffer(10); | ||
| 17 | + assert(ArrayBuffer.isView(slowbuff)); | ||
| 18 | + | ||
| 13 | 19 | // buffer | |
| 14 | 20 | var incomplete = new Buffer([0xe4, 0xb8, 0xad, 0xe6, 0x96]); | |
| 15 | 21 | assert.equal(Buffer.byteLength(incomplete), 5); | |
| 16 | 22 | var ascii = new Buffer('abc'); | |
| 17 | 23 | assert.equal(Buffer.byteLength(ascii), 3); | |
| 18 | 24 | ||
| 25 | + // ArrayBuffer | ||
| 26 | + var buffer = new ArrayBuffer(8); | ||
| 27 | + assert.equal(Buffer.byteLength(buffer), 8); | ||
| 28 | + | ||
| 29 | + // TypedArray | ||
| 30 | + var int8 = new Int8Array(8); | ||
| 31 | + assert.equal(Buffer.byteLength(int8), 8); | ||
| 32 | + var uint8 = new Uint8Array(8); | ||
| 33 | + assert.equal(Buffer.byteLength(uint8), 8); | ||
| 34 | + var uintc8 = new Uint8ClampedArray(2); | ||
| 35 | + assert.equal(Buffer.byteLength(uintc8), 2); | ||
| 36 | + var int16 = new Int16Array(8); | ||
| 37 | + assert.equal(Buffer.byteLength(int16), 16); | ||
| 38 | + var uint16 = new Uint16Array(8); | ||
| 39 | + assert.equal(Buffer.byteLength(uint16), 16); | ||
| 40 | + var int32 = new Int32Array(8); | ||
| 41 | + assert.equal(Buffer.byteLength(int32), 32); | ||
| 42 | + var uint32 = new Uint32Array(8); | ||
| 43 | + assert.equal(Buffer.byteLength(uint32), 32); | ||
| 44 | + var float32 = new Float32Array(8); | ||
| 45 | + assert.equal(Buffer.byteLength(float32), 32); | ||
| 46 | + var float64 = new Float64Array(8); | ||
| 47 | + assert.equal(Buffer.byteLength(float64), 64); | ||
| 48 | + | ||
| 49 | + // DataView | ||
| 50 | + var dv = new DataView(new ArrayBuffer(2)); | ||
| 51 | + assert.equal(Buffer.byteLength(dv), 2); | ||
| 52 | + | ||
| 19 | 53 | // special case: zero length string | |
| 20 | 54 | assert.equal(Buffer.byteLength('', 'ascii'), 0); | |
| 21 | 55 | assert.equal(Buffer.byteLength('', 'HeX'), 0); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments