From: Volker Mische Date: Sat, 20 Jan 2018 23:23:56 +0000 (+0100) Subject: Fix single character fast path X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=df827fb1321befa654a62ce83b0b4a0639beea8c;p=buffer.git Fix single character fast path This fixed is based on Node.js' commit d4f00fe1098b0d7b8444565e741d8b457fd9c091 https://github.com/nodejs/node/commit/d4f00fe1098b0d7b8444565e741d8b457fd9c091 --- diff --git a/index.js b/index.js index 042a787..f8f5184 100644 --- a/index.js +++ b/index.js @@ -1512,18 +1512,20 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { encoding = end end = this.length } - if (val.length === 1) { - var code = val.charCodeAt(0) - if (code < 256) { - val = code - } - } if (encoding !== undefined && typeof encoding !== 'string') { throw new TypeError('encoding must be a string') } if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { throw new TypeError('Unknown encoding: ' + encoding) } + if (val.length === 1) { + var code = val.charCodeAt(0) + if ((encoding === 'utf8' && code < 128) || + encoding === 'latin1') { + // Fast path: If `val` fits into a single byte, use that numeric value. + val = code + } + } } else if (typeof val === 'number') { val = val & 255 } diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index d7d4ee4..bbba135 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -1018,14 +1018,14 @@ common.expectsError(() => { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); -/* + common.expectsError(() => { Buffer.alloc(0x1000, 'c', 'hex'); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); -*/ + common.expectsError(() => { Buffer.alloc(1, Buffer.alloc(0)); }, {