@@ -61,33 +61,33 @@ const _compactUInt8ArrayFrom0To31Numbers = _0To31Numbers => {
6161 let byteIndex = 0 ;
6262
6363 const saveByte = ( number ) => {
64- console . log ( `save ${ number } (${ number . toString ( 2 ) . padStart ( 8 , "0" ) } )` )
64+ // console.log(`save ${number} (${number.toString(2).padStart(8, "0")})`)
6565 compactUInt8Array [ byteIndex ] = number ;
6666 byteIndex += 1 ;
6767 numberToBeSaved = 0 ;
6868 bitPosition = 0 ;
6969 } ;
7070 _0To31Numbers . forEach ( number => {
71- if ( bitPosition + LETTER_LENGTH <= BYTE_LENGTH ) { // can fit at once
71+ if ( bitPosition + LETTER_LENGTH < BYTE_LENGTH ) { // can fit at once
7272 const toShift = BYTE_LENGTH - LETTER_LENGTH - bitPosition ;
7373 numberToBeSaved += number << toShift ;
7474 bitPosition += LETTER_LENGTH ;
7575
76- if ( bitPosition === BYTE_LENGTH ) {
76+ if ( bitPosition + 1 === BYTE_LENGTH ) {
7777 saveByte ( numberToBeSaved ) ;
7878 }
7979 } else { // cannot fit at once
80- const shiftRight = - BYTE_LENGTH + bitPosition + LETTER_LENGTH ; // 2
81- const missedBits = LETTER_LENGTH - shiftRight ; // 3
82- const clamped = number >> shiftRight ;
83- numberToBeSaved += clamped ;
80+ const shiftRight = - BYTE_LENGTH + LETTER_LENGTH + bitPosition ; // 2
81+ const spaceLeft = LETTER_LENGTH - shiftRight ; // 3
82+
83+ const firstHalfToAdd = number >> shiftRight ;
84+ const secondHalf = number - ( firstHalfToAdd << shiftRight )
85+ numberToBeSaved += firstHalfToAdd ;
8486
8587 saveByte ( numberToBeSaved ) ;
8688
87- const substractHead = clamped << shiftRight ;
88- const rest = number - substractHead ;
89- numberToBeSaved += rest << ( BYTE_LENGTH + 1 ) - missedBits ;
90- bitPosition += missedBits - 1 ; //3
89+ numberToBeSaved += secondHalf << ( BYTE_LENGTH - LETTER_LENGTH + spaceLeft ) ;
90+ bitPosition += LETTER_LENGTH - spaceLeft ; //3
9191 }
9292 } ) ;
9393 // todo handle all cases
@@ -97,7 +97,7 @@ const _compactUInt8ArrayFrom0To31Numbers = _0To31Numbers => {
9797 } //else {
9898 // compactUInt8Array[byteIndex] = remainder;
9999 // }
100- console . log ( "3_compactUInt8ArrayFrom0To31Numbers RESULT" , compactUInt8Array )
100+ // console.log("3_compactUInt8ArrayFrom0To31Numbers RESULT",compactUInt8Array)
101101 return compactUInt8Array ;
102102} ;
103103
@@ -214,11 +214,11 @@ const decode = uInt8Array => {
214214 return string ;
215215} ;
216216
217- console . log ( 37 , _compactUInt8ArrayFrom0To31Numbers ( [ 1 , 2 , 3 ] ) )
218- console . log ( 38 , Uint8Array . from ( [
219- 0b00001000 , // 1-2
220- 0b10000110 , // 2-3
221- ] ) )
217+ // console.log(37,_compactUInt8ArrayFrom0To31Numbers([1,2,3]))
218+ // console.log(38,Uint8Array.from([
219+ // 0b00001000, // 1-2
220+ // 0b10000110, // 2-3
221+ // ]))
222222// console.log(encode('abc def zxw aaa zzz'));
223223// console.log(encode('a - z'));
224224// console.log(encode('a'));
0 commit comments