buffer: refactor Buffer.prototype.inspect()

Replace toString().match().join() with toString().replace().trim(). This
enables the elimination of a length check becuase replace() will return
empty string if Buffer is empty whereas match() returns null.

PR-URL: https://github.com/nodejs/node/pull/11600
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Rich Trott 2017-02-28 00:15:07 -08:00 committed by James M Snell
parent 0d4bbf757c
commit 4c05d6a0b7

View File

@ -531,12 +531,10 @@ Buffer.prototype.equals = function equals(b) {
Buffer.prototype[internalUtil.customInspectSymbol] = function inspect() { Buffer.prototype[internalUtil.customInspectSymbol] = function inspect() {
var str = ''; var str = '';
var max = exports.INSPECT_MAX_BYTES; var max = exports.INSPECT_MAX_BYTES;
if (this.length > 0) { str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
if (this.length > max) if (this.length > max)
str += ' ... '; str += ' ... ';
} return `<${this.constructor.name} ${str}>`;
return '<' + this.constructor.name + ' ' + str + '>';
}; };
Buffer.prototype.inspect = Buffer.prototype[internalUtil.customInspectSymbol]; Buffer.prototype.inspect = Buffer.prototype[internalUtil.customInspectSymbol];