From de3f25cb122ab4e51937320ca7d0f7e8dbaf9f76 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 7 May 2014 17:03:15 -0700 Subject: [PATCH] Buffer#fill supports strings --- index.js | 24 ++++++++++++++---------- test/buffer.js | 24 ++++++++++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index e61cd2d..7a84e85 100644 --- a/index.js +++ b/index.js @@ -893,11 +893,6 @@ Buffer.prototype.fill = function (value, start, end) { if (!start) start = 0 if (!end) end = this.length - if (typeof value === 'string') { - value = value.charCodeAt(0) - } - - assert(typeof value === 'number' && !isNaN(value), 'value is not a number') assert(end >= start, 'end < start') // Fill 0 bytes; we're done @@ -907,8 +902,17 @@ Buffer.prototype.fill = function (value, start, end) { assert(start >= 0 && start < this.length, 'start out of bounds') assert(end >= 0 && end <= this.length, 'end out of bounds') - for (var i = start; i < end; i++) { - this[i] = value + var i + if (typeof value === 'number') { + for (i = start; i < end; i++) { + this[i] = value + } + } else { + var bytes = utf8ToBytes(value.toString()) + var len = bytes.length + for (i = start; i < end; i++) { + this[i] = bytes[i % len] + } } return this @@ -1054,9 +1058,9 @@ function utf8ToBytes (str) { var byteArray = [] for (var i = 0; i < str.length; i++) { var b = str.charCodeAt(i) - if (b <= 0x7F) - byteArray.push(str.charCodeAt(i)) - else { + if (b <= 0x7F) { + byteArray.push(b) + } else { var start = i if (b >= 0xD800 && b <= 0xDFFF) i++ var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%') diff --git a/test/buffer.js b/test/buffer.js index 2f306c6..6c953e9 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -114,7 +114,7 @@ test('utf8 to binary', function (t) { }) test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { - t.plan(2*(2*2*2+2)) + t.plan(2 * (2 * 2 * 2 + 2)) var hex = [ '03', '0300', '0003', '03000000', '00000003', 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd' @@ -150,7 +150,7 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { }) test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { - t.plan(3*(2*2*2+2)) + t.plan(3 * (2 * 2 * 2 + 2)) var hex = [ '', '03', '00', '030000', '000000', '', 'fd', 'ff', 'fdffff', 'ffffff' @@ -198,7 +198,9 @@ test('concat() a varying number of buffers', function (t) { var zero = [] var one = [ new B('asdf') ] var long = [] - for (var i = 0; i < 10; i++) long.push(new B('asdf')) + for (var i = 0; i < 10; i++) { + long.push(new B('asdf')) + } var flatZero = B.concat(zero) var flatOne = B.concat(one) @@ -213,13 +215,23 @@ test('concat() a varying number of buffers', function (t) { t.end() }) -test('fill', function(t) { +test('fill', function (t) { var b = new B(10) b.fill(2) t.equal(b.toString('hex'), '02020202020202020202') t.end() }) +test('fill (string)', function (t) { + var b = new B(10) + b.fill('abc') + t.equal(b.toString(), 'abcabcabca') + b.fill('Õ§') + console.log(b[0], b[1], b[2], b[3], b[4]) + t.equal(b.toString(), 'Õ§Õ§Õ§Õ§Õ§') + t.end() +}) + test('copy() empty buffer with sourceEnd=0', function (t) { var source = new B([42]) var destination = new B([43]) @@ -228,7 +240,7 @@ test('copy() empty buffer with sourceEnd=0', function (t) { t.end() }) -test('copy() after slice()', function(t) { +test('copy() after slice()', function (t) { var source = new B(200) var dest = new B(200) var expected = new B(200) @@ -243,7 +255,7 @@ test('copy() after slice()', function(t) { t.end() }) -test('base64 ignore whitespace', function(t) { +test('base64 ignore whitespace', function (t) { var text = '\n YW9ldQ== ' var buf = new B(text, 'base64') t.equal(buf.toString(), 'aoeu') -- 2.34.1