@@ -28,7 +28,8 @@ var _weakMap2 = _interopRequireDefault(_weakMap);
2828function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
2929
3030var propMap = new _weakMap2 . default ( ) ;
31- var bitsPerByte = 8 ;
31+
32+ var BITS_PER_BYTE = 8 ;
3233
3334var CharSet = function ( ) {
3435 function CharSet ( chars ) {
@@ -56,7 +57,7 @@ var CharSet = function () {
5657 bitsPerChar : bitsPerChar ,
5758 length : length ,
5859 ndxFn : _ndxFn ( bitsPerChar ) ,
59- charsPerChunk : ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) / bitsPerChar
60+ charsPerChunk : ( 0 , _lcm2 . default ) ( bitsPerChar , BITS_PER_BYTE ) / bitsPerChar
6061 } ;
6162 propMap . set ( this , privProps ) ;
6263 }
@@ -91,6 +92,12 @@ var CharSet = function () {
9192 value : function getNdxFn ( ) {
9293 return propMap . get ( this ) . ndxFn ;
9394 }
95+ } , {
96+ key : 'bytesNeeded' ,
97+ value : function bytesNeeded ( entropyBits ) {
98+ var count = Math . ceil ( entropyBits / this . bitsPerChar ( ) ) ;
99+ return Math . ceil ( count * this . bitsPerChar ( ) / BITS_PER_BYTE ) ;
100+ }
94101
95102 // Aliases
96103
@@ -104,6 +111,11 @@ var CharSet = function () {
104111 value : function ndxFn ( ) {
105112 return this . getNdxFn ( ) ;
106113 }
114+ } , {
115+ key : 'bitsPerChar' ,
116+ value : function bitsPerChar ( ) {
117+ return this . getBitsPerChar ( ) ;
118+ }
107119 } ] ) ;
108120 return CharSet ;
109121} ( ) ;
@@ -112,30 +124,32 @@ exports.default = CharSet;
112124
113125
114126var _ndxFn = function _ndxFn ( bitsPerChar ) {
115- // If bitsPerBytes is a multiple of bitsPerChar, we can slice off an integer number
127+ // If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
116128 // of chars per byte.
117- if ( ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) === bitsPerByte ) {
129+ if ( ( 0 , _lcm2 . default ) ( bitsPerChar , BITS_PER_BYTE ) === BITS_PER_BYTE ) {
118130 return function ( chunk , slice , bytes ) {
119131 var lShift = bitsPerChar ;
120- var rShift = bitsPerByte - bitsPerChar ;
132+ var rShift = BITS_PER_BYTE - bitsPerChar ;
121133 return ( bytes [ chunk ] << lShift * slice & 0xff ) >> rShift ;
122134 } ;
123135 }
124136 // Otherwise, while slicing off bits per char, we will possibly straddle a couple
125137 // of bytes, so a bit more work is involved
126138 else {
139+ var slicesPerChunk = ( 0 , _lcm2 . default ) ( bitsPerChar , BITS_PER_BYTE ) / BITS_PER_BYTE ;
127140 return function ( chunk , slice , bytes ) {
128- var slicesPerChunk = ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) / bitsPerByte ;
129141 var bNum = chunk * slicesPerChunk ;
130142
131- var rShift = bitsPerByte - bitsPerChar ;
132- var lOffset = Math . floor ( slice * bitsPerChar / bitsPerByte ) ;
133- var lShift = slice * bitsPerChar % bitsPerByte ;
143+ var offset = slice * bitsPerChar / BITS_PER_BYTE ;
144+ var lOffset = Math . floor ( offset ) ;
145+ var rOffset = Math . ceil ( offset ) ;
146+
147+ var rShift = BITS_PER_BYTE - bitsPerChar ;
148+ var lShift = slice * bitsPerChar % BITS_PER_BYTE ;
134149
135150 var ndx = ( bytes [ bNum + lOffset ] << lShift & 0xff ) >> rShift ;
136151
137- var rOffset = Math . ceil ( slice * bitsPerChar / bitsPerByte ) ;
138- var rShiftIt = ( ( rOffset + 1 ) * bitsPerByte - ( slice + 1 ) * bitsPerChar ) % bitsPerByte ;
152+ var rShiftIt = ( ( rOffset + 1 ) * BITS_PER_BYTE - ( slice + 1 ) * bitsPerChar ) % BITS_PER_BYTE ;
139153 if ( rShift < rShiftIt ) {
140154 ndx += bytes [ bNum + rOffset ] >> rShiftIt ;
141155 }
0 commit comments