@@ -497,36 +497,28 @@ KaitaiStream.endianness = new Int8Array(new Int16Array([1]).buffer)[0] > 0;
497497// ========================================================================
498498
499499KaitaiStream . prototype . readBytes = function ( len ) {
500- return this . mapUint8Array ( len ) ;
500+ return this . mapUint8Array ( len , len ) ;
501501} ;
502502
503503KaitaiStream . prototype . readBytesFull = function ( ) {
504- return this . mapUint8Array ( this . size - this . pos ) ;
504+ return this . readBytes ( this . size - this . pos ) ;
505505} ;
506506
507507KaitaiStream . prototype . readBytesTerm = function ( terminator , include , consume , eosError ) {
508- var blen = this . size - this . pos ;
509- var u8 = new Uint8Array ( this . _buffer , this . _byteOffset + this . pos ) ;
510- for ( var i = 0 ; i < blen && u8 [ i ] !== terminator ; i ++ ) ; // find first zero byte
511- if ( i === blen ) {
508+ var len = this . size - this . pos ;
509+ var u8 = new Uint8Array ( this . _buffer , this . _byteOffset + this . pos , len ) ;
510+ var i = u8 . findIndex ( function ( value ) { return value === terminator ; } ) ;
511+ if ( i < 0 ) {
512512 // we've read all the buffer and haven't found the terminator
513513 if ( eosError ) {
514514 throw "End of stream reached, but no terminator " + terminator + " found" ;
515- } else {
516- return this . mapUint8Array ( i ) ;
517- }
518- } else {
519- var arr ;
520- if ( include ) {
521- arr = this . mapUint8Array ( i + 1 ) ;
522- } else {
523- arr = this . mapUint8Array ( i ) ;
524- }
525- if ( consume ) {
526- this . pos += 1 ;
527515 }
528- return arr ;
516+ return this . readBytes ( len ) ;
529517 }
518+ return this . mapUint8Array (
519+ include ? i + 1 : i ,
520+ consume ? i + 1 : i
521+ ) ;
530522} ;
531523
532524// Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions
@@ -817,15 +809,17 @@ KaitaiStream.prototype.ensureBytesLeft = function(length) {
817809 Nice for quickly reading in data.
818810
819811 @param {number } length Number of elements to map.
812+ @param {number } [consume=length] Number of elements to consume (default to `length`).
820813 @return {Object } Uint8Array to the KaitaiStream backing buffer.
821814 */
822- KaitaiStream . prototype . mapUint8Array = function ( length ) {
815+ KaitaiStream . prototype . mapUint8Array = function ( length , consume = length ) {
823816 length |= 0 ;
817+ consume |= 0 ;
824818
825819 this . ensureBytesLeft ( length ) ;
826820
827821 var arr = new Uint8Array ( this . _buffer , this . byteOffset + this . pos , length ) ;
828- this . pos += length ;
822+ this . pos += consume ;
829823 return arr ;
830824} ;
831825
0 commit comments