@@ -51,19 +51,30 @@ export class Buffer extends Uint8Array {
5151
5252 inspect ( ) : string {
5353 let byteLength = this . byteLength ;
54- let dataStart = this . dataStart ;
5554 let INSPECT_MAX_BYTES = this . INSPECT_MAX_BYTES ;
55+ if ( INSPECT_MAX_BYTES == 0 || byteLength == 0 ) return "<Buffer >" ;
56+
57+ // Calculate if an elipsis will be in the string
5658 let elipsisEnd = byteLength > INSPECT_MAX_BYTES ;
5759 let maxBytes = elipsisEnd ? INSPECT_MAX_BYTES : byteLength ;
60+
61+ // find the start of the buffer
62+ let dataStart = this . dataStart ;
63+
5864 // formula for calculating end string length (3 * bytes) + 8
5965 // Example: Buffer.from([1, 2, 3, 4, 5]).inspect() == '<Buffer 01 02 03 04 05>'
6066 let stringLength = 3 * maxBytes + 8 ;
6167 if ( elipsisEnd ) stringLength += 3 ; // add 3 characters for elipsis
62- let buffer = __alloc ( stringLength << 1 , idof < String > ( ) ) ;
6368
69+ // create the result
70+ let result = __alloc ( stringLength << 1 , idof < String > ( ) ) ;
71+
72+ // copy the 16 "<Buffer " bytes
6473 let source = "<Buffer " ;
65- memory . copy ( buffer , changetype < usize > ( source ) , 16 ) ; // copy the 16 "<Buffer " bytes
66- let writeOffset = buffer + 16 ;
74+ memory . copy ( result , changetype < usize > ( source ) , 16 ) ;
75+
76+ // Start writing at index 8
77+ let writeOffset : usize = result + 16 ;
6778 for ( let i = 0 ; i < maxBytes ; i ++ , writeOffset += 6 ) {
6879 let byte = load < u8 > ( dataStart + < usize > i ) ;
6980 let top = ( byte >>> 4 ) & 0xF ;
@@ -84,6 +95,6 @@ export class Buffer extends Uint8Array {
8495 }
8596 }
8697
87- return changetype < string > ( buffer ) ;
98+ return changetype < string > ( result ) ;
8899 }
89100}
0 commit comments