@@ -1156,8 +1156,11 @@ export class Parser {
11561156 ctx . pushCode (
11571157 `while(buffer.readUInt8(offset++) !== 0 && offset - ${ start } < ${ len } );`
11581158 ) ;
1159+ //ctx.pushCode(
1160+ // `${name} = buffer.toString('${encoding}', ${start}, offset - ${start} < ${len} ? offset - 1 : offset);`
1161+ //);
11591162 ctx . pushCode (
1160- `${ name } = buffer.toString('${ encoding } ', ${ start } , offset - ${ start } < ${ len } ? offset - 1 : offset);`
1163+ `${ name } = buffer.toString('${ encoding } ', ${ start } , buffer.readUInt8( offset -1) == 0 ? offset - 1 : offset);`
11611164 ) ;
11621165 } else if ( this . options . length ) {
11631166 const len = ctx . generateOption ( this . options . length ) ;
@@ -1202,36 +1205,41 @@ export class Parser {
12021205 // Compute padding length
12031206 const padLen = ctx . generateTmpVariable ( ) ;
12041207 ctx . pushCode ( `${ padLen } = ${ optLength } - ${ tmpBuf } .length;` ) ;
1205- const padCharVar = ctx . generateTmpVariable ( ) ;
1206- let padChar = ' ' ;
1207- if ( this . options . padd && typeof this . options . padd === 'string' ) {
1208- const code = this . options . padd . charCodeAt ( 0 ) ;
1209- if ( code < 0x80 ) {
1210- padChar = String . fromCharCode ( code ) ;
1208+ if ( this . options . zeroTerminated ) {
1209+ ctx . pushCode ( `smartBuffer.writeBuffer(${ tmpBuf } );` ) ;
1210+ ctx . pushCode ( `if (${ padLen } > 0) { smartBuffer.writeUInt8(0x00); }` ) ;
1211+ } else {
1212+ const padCharVar = ctx . generateTmpVariable ( ) ;
1213+ let padChar = this . options . stripNull ? '\u0000' : ' ' ;
1214+ if ( this . options . padd && typeof this . options . padd === 'string' ) {
1215+ const code = this . options . padd . charCodeAt ( 0 ) ;
1216+ if ( code < 0x80 ) {
1217+ padChar = String . fromCharCode ( code ) ;
1218+ }
1219+ }
1220+ ctx . pushCode ( `${ padCharVar } = "${ padChar } ";` ) ;
1221+ if ( this . options . padding === 'left' ) {
1222+ // Add heading padding spaces
1223+ ctx . pushCode (
1224+ `if (${ padLen } > 0) {smartBuffer.writeString(${ padCharVar } .repeat(${ padLen } ));}`
1225+ ) ;
1226+ }
1227+ // Copy the temporary string buffer to current smartBuffer
1228+ ctx . pushCode ( `smartBuffer.writeBuffer(${ tmpBuf } );` ) ;
1229+ if ( this . options . padding !== 'left' ) {
1230+ // Add trailing padding spaces
1231+ ctx . pushCode (
1232+ `if (${ padLen } > 0) {smartBuffer.writeString(${ padCharVar } .repeat(${ padLen } ));}`
1233+ ) ;
12111234 }
1212- }
1213- ctx . pushCode ( `${ padCharVar } = "${ padChar } ";` ) ;
1214- if ( this . options . padding === 'left' ) {
1215- // Add heading padding spaces
1216- ctx . pushCode (
1217- `if (${ padLen } > 0) {smartBuffer.writeString(${ padCharVar } .repeat(${ padLen } ));}`
1218- ) ;
1219- }
1220- // Copy the temporary string buffer to current smartBuffer
1221- ctx . pushCode ( `smartBuffer.writeBuffer(${ tmpBuf } );` ) ;
1222- if ( this . options . padding !== 'left' ) {
1223- // Add trailing padding spaces
1224- ctx . pushCode (
1225- `if (${ padLen } > 0) {smartBuffer.writeString(${ padCharVar } .repeat(${ padLen } ));}`
1226- ) ;
12271235 }
12281236 } else {
12291237 ctx . pushCode (
12301238 `smartBuffer.writeString(${ name } , "${ this . options . encoding } ");`
12311239 ) ;
1232- }
1233- if ( this . options . zeroTerminated ) {
1234- ctx . pushCode ( 'smartBuffer.writeUInt8(0x00);' ) ;
1240+ if ( this . options . zeroTerminated ) {
1241+ ctx . pushCode ( 'smartBuffer.writeUInt8(0x00);' ) ;
1242+ }
12351243 }
12361244 }
12371245
0 commit comments