Skip to content

Commit 508cbaa

Browse files
BridgeARMylesBorins
authored andcommitted
buffer: simplify code
This refactors some code for simplicity. It also removes a call indirection used in the buffers custom inspect function. PR-URL: #25151 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 57148f3 commit 508cbaa

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

‎lib/buffer.js‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,13 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
298298
// If --zero-fill-buffers command line argument is set, a zero-filled
299299
// buffer is returned.
300300
functionSlowBuffer(length){
301+
constlen=+length;
301302
// eslint-disable-next-line eqeqeq
302-
if(+length!=length)
303+
if(len!=length)
303304
length=0;
304-
assertSize(+length);
305-
returncreateUnsafeBuffer(+length);
305+
else
306+
assertSize(len);
307+
returncreateUnsafeBuffer(len);
306308
}
307309

308310
Object.setPrototypeOf(SlowBuffer.prototype,Uint8Array.prototype);
@@ -319,9 +321,8 @@ function allocate(size) {
319321
poolOffset+=size;
320322
alignPool();
321323
returnb;
322-
}else{
323-
returncreateUnsafeBuffer(size);
324324
}
325+
returncreateUnsafeBuffer(size);
325326
}
326327

327328
functionfromString(string,encoding){
@@ -639,21 +640,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
639640
}
640641

641642
constlen=this.length;
642-
if(len===0)
643-
return'';
644643

645-
if(!start||start<0)
644+
if(start<=0)
646645
start=0;
647646
elseif(start>=len)
648647
return'';
648+
else
649+
start|=0;
649650

650651
if(end===undefined||end>len)
651652
end=len;
652-
elseif(end<=0)
653-
return'';
654-
655-
start|=0;
656-
end|=0;
653+
else
654+
end|=0;
657655

658656
if(end<=start)
659657
return'';
@@ -672,10 +670,10 @@ Buffer.prototype.equals = function equals(otherBuffer) {
672670
};
673671

674672
// Override how buffers are presented by util.inspect().
675-
Buffer.prototype[customInspectSymbol]=functioninspect(){
676-
varstr='';
677-
varmax=exports.INSPECT_MAX_BYTES;
678-
str=this.toString('hex',0,max).replace(/(.{2})/g,'$1 ').trim();
673+
Buffer.prototype[customInspectSymbol]=functioninspect(recurseTimes,ctx){
674+
constmax=exports.INSPECT_MAX_BYTES;
675+
constactualMax=Math.min(max,this.length);
676+
letstr=this.hexSlice(0,actualMax).replace(/(.{2})/g,'$1 ').trim();
679677
constremaining=this.length-max;
680678
if(remaining>0)
681679
str+=` ... ${remaining} more byte${remaining>1 ? 's' : ''}`;
@@ -977,9 +975,8 @@ Buffer.prototype.toJSON = function toJSON() {
977975
for(vari=0;i<this.length;++i)
978976
data[i]=this[i];
979977
return{type: 'Buffer', data };
980-
}else{
981-
return{type: 'Buffer',data: []};
982978
}
979+
return{type: 'Buffer',data: []};
983980
};
984981

985982
functionadjustOffset(offset,length){

0 commit comments

Comments
 (0)