| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 60d5f6b commit 9eaa721
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -664,23 +664,27 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) { | |||
| 664 | 664 | encoding = end; | |
| 665 | 665 | end = this.length; | |
| 666 | 666 | } | |
| 667 | - if (val.length === 1) { | ||
| 668 | - var code = val.charCodeAt(0); | ||
| 669 | - if (code < 256) | ||
| 670 | - val = code; | ||
| 671 | - } | ||
| 672 | - if (val.length === 0) { | ||
| 673 | - // Previously, if val === '', the Buffer would not fill, | ||
| 674 | - // which is rather surprising. | ||
| 675 | - val = 0; | ||
| 676 | - } | ||
| 667 | + | ||
| 677 | 668 | if (encoding !== undefined && typeof encoding !== 'string') { | |
| 678 | 669 | throw new TypeError('encoding must be a string'); | |
| 679 | 670 | } | |
| 680 | - if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { | ||
| 671 | + var normalizedEncoding = internalUtil.normalizeEncoding(encoding); | ||
| 672 | + if (normalizedEncoding === undefined) { | ||
| 681 | 673 | throw new TypeError('Unknown encoding: ' + encoding); | |
| 682 | 674 | } | |
| 683 | 675 | ||
| 676 | + if (val.length === 0) { | ||
| 677 | + // Previously, if val === '', the Buffer would not fill, | ||
| 678 | + // which is rather surprising. | ||
| 679 | + val = 0; | ||
| 680 | + } else if (val.length === 1) { | ||
| 681 | + var code = val.charCodeAt(0); | ||
| 682 | + if ((normalizedEncoding === 'utf8' && code < 128) || | ||
| 683 | + normalizedEncoding === 'latin1') { | ||
| 684 | + // Fast path: If `val` fits into a single byte, use that numeric value. | ||
| 685 | + val = code; | ||
| 686 | + } | ||
| 687 | + } | ||
| 684 | 688 | } else if (typeof val === 'number') { | |
| 685 | 689 | val = val & 255; | |
| 686 | 690 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -395,11 +395,37 @@ assert.throws(() => { | |||
| 395 | 395 | buf.fill(''); | |
| 396 | 396 | }, /^RangeError: out of range index$/); | |
| 397 | 397 | ||
| 398 | - | ||
| 399 | 398 | assert.deepStrictEqual( | |
| 400 | 399 | Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'), | |
| 401 | 400 | Buffer.from('61006200610062006100620061006200', 'hex')); | |
| 402 | 401 | ||
| 403 | 402 | assert.deepStrictEqual( | |
| 404 | 403 | Buffer.allocUnsafeSlow(15).fill('ab', 'utf16le'), | |
| 405 | 404 | Buffer.from('610062006100620061006200610062', 'hex')); | |
| 405 | + | ||
| 406 | + assert.deepStrictEqual( | ||
| 407 | + Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'), | ||
| 408 | + Buffer.from('61006200610062006100620061006200', 'hex')); | ||
| 409 | + assert.deepStrictEqual( | ||
| 410 | + Buffer.allocUnsafeSlow(16).fill('a', 'utf16le'), | ||
| 411 | + Buffer.from('61006100610061006100610061006100', 'hex')); | ||
| 412 | + | ||
| 413 | + assert.strictEqual( | ||
| 414 | + Buffer.allocUnsafeSlow(16).fill('a', 'utf16le').toString('utf16le'), | ||
| 415 | + 'a'.repeat(8)); | ||
| 416 | + assert.strictEqual( | ||
| 417 | + Buffer.allocUnsafeSlow(16).fill('a', 'latin1').toString('latin1'), | ||
| 418 | + 'a'.repeat(16)); | ||
| 419 | + assert.strictEqual( | ||
| 420 | + Buffer.allocUnsafeSlow(16).fill('a', 'utf8').toString('utf8'), | ||
| 421 | + 'a'.repeat(16)); | ||
| 422 | + | ||
| 423 | + assert.strictEqual( | ||
| 424 | + Buffer.allocUnsafeSlow(16).fill('Љ', 'utf16le').toString('utf16le'), | ||
| 425 | + 'Љ'.repeat(8)); | ||
| 426 | + assert.strictEqual( | ||
| 427 | + Buffer.allocUnsafeSlow(16).fill('Љ', 'latin1').toString('latin1'), | ||
| 428 | + '\t'.repeat(16)); | ||
| 429 | + assert.strictEqual( | ||
| 430 | + Buffer.allocUnsafeSlow(16).fill('Љ', 'utf8').toString('utf8'), | ||
| 431 | + 'Љ'.repeat(8)); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments