diff options
author | Anna Henningsen <anna@addaleax.net> | 2016-11-29 05:03:24 -0600 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2016-12-12 16:58:40 +0100 |
commit | d4f00fe1098b0d7b8444565e741d8b457fd9c091 (patch) | |
tree | a97e31dbeb3f0e98c45d0a9f5beec1da3788db32 /test/parallel/test-buffer-fill.js | |
parent | 9ee915be76f9e4cc8bc43020a721a03257f9b984 (diff) | |
download | android-node-v8-d4f00fe1098b0d7b8444565e741d8b457fd9c091.tar.gz android-node-v8-d4f00fe1098b0d7b8444565e741d8b457fd9c091.tar.bz2 android-node-v8-d4f00fe1098b0d7b8444565e741d8b457fd9c091.zip |
buffer: fix single-character string filling
Fix the fast path for `buffer.fill()` with a single-character string.
The fast path only works for strings that are equivalent to a
single-byte buffer, but that condition was not checked properly
for the `utf8` or `utf16le` encodings and is always true for the
`latin1` encoding.
This change fixes these problems.
Fixes: https://github.com/nodejs/node/issues/9836
PR-URL: https://github.com/nodejs/node/pull/9837
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'test/parallel/test-buffer-fill.js')
-rw-r--r-- | test/parallel/test-buffer-fill.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js index 68b8bbd69c..eecb14abb0 100644 --- a/test/parallel/test-buffer-fill.js +++ b/test/parallel/test-buffer-fill.js @@ -395,7 +395,6 @@ assert.throws(() => { buf.fill(''); }, /^RangeError: out of range index$/); - assert.deepStrictEqual( Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'), Buffer.from('61006200610062006100620061006200', 'hex')); @@ -403,3 +402,30 @@ assert.deepStrictEqual( assert.deepStrictEqual( Buffer.allocUnsafeSlow(15).fill('ab', 'utf16le'), Buffer.from('610062006100620061006200610062', 'hex')); + +assert.deepStrictEqual( + Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'), + Buffer.from('61006200610062006100620061006200', 'hex')); +assert.deepStrictEqual( + Buffer.allocUnsafeSlow(16).fill('a', 'utf16le'), + Buffer.from('61006100610061006100610061006100', 'hex')); + +assert.strictEqual( + Buffer.allocUnsafeSlow(16).fill('a', 'utf16le').toString('utf16le'), + 'a'.repeat(8)); +assert.strictEqual( + Buffer.allocUnsafeSlow(16).fill('a', 'latin1').toString('latin1'), + 'a'.repeat(16)); +assert.strictEqual( + Buffer.allocUnsafeSlow(16).fill('a', 'utf8').toString('utf8'), + 'a'.repeat(16)); + +assert.strictEqual( + Buffer.allocUnsafeSlow(16).fill('Љ', 'utf16le').toString('utf16le'), + 'Љ'.repeat(8)); +assert.strictEqual( + Buffer.allocUnsafeSlow(16).fill('Љ', 'latin1').toString('latin1'), + '\t'.repeat(16)); +assert.strictEqual( + Buffer.allocUnsafeSlow(16).fill('Љ', 'utf8').toString('utf8'), + 'Љ'.repeat(8)); |