@@ -28,7 +28,8 @@ var _weakMap2 = _interopRequireDefault(_weakMap);
28
28
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
29
29
30
30
var propMap = new _weakMap2 . default ( ) ;
31
- var bitsPerByte = 8 ;
31
+
32
+ var BITS_PER_BYTE = 8 ;
32
33
33
34
var CharSet = function ( ) {
34
35
function CharSet ( chars ) {
@@ -56,7 +57,7 @@ var CharSet = function () {
56
57
bitsPerChar : bitsPerChar ,
57
58
length : length ,
58
59
ndxFn : _ndxFn ( bitsPerChar ) ,
59
- charsPerChunk : ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) / bitsPerChar
60
+ charsPerChunk : ( 0 , _lcm2 . default ) ( bitsPerChar , BITS_PER_BYTE ) / bitsPerChar
60
61
} ;
61
62
propMap . set ( this , privProps ) ;
62
63
}
@@ -91,6 +92,12 @@ var CharSet = function () {
91
92
value : function getNdxFn ( ) {
92
93
return propMap . get ( this ) . ndxFn ;
93
94
}
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
+ }
94
101
95
102
// Aliases
96
103
@@ -104,6 +111,11 @@ var CharSet = function () {
104
111
value : function ndxFn ( ) {
105
112
return this . getNdxFn ( ) ;
106
113
}
114
+ } , {
115
+ key : 'bitsPerChar' ,
116
+ value : function bitsPerChar ( ) {
117
+ return this . getBitsPerChar ( ) ;
118
+ }
107
119
} ] ) ;
108
120
return CharSet ;
109
121
} ( ) ;
@@ -112,30 +124,32 @@ exports.default = CharSet;
112
124
113
125
114
126
var _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
116
128
// of chars per byte.
117
- if ( ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) === bitsPerByte ) {
129
+ if ( ( 0 , _lcm2 . default ) ( bitsPerChar , BITS_PER_BYTE ) === BITS_PER_BYTE ) {
118
130
return function ( chunk , slice , bytes ) {
119
131
var lShift = bitsPerChar ;
120
- var rShift = bitsPerByte - bitsPerChar ;
132
+ var rShift = BITS_PER_BYTE - bitsPerChar ;
121
133
return ( bytes [ chunk ] << lShift * slice & 0xff ) >> rShift ;
122
134
} ;
123
135
}
124
136
// Otherwise, while slicing off bits per char, we will possibly straddle a couple
125
137
// of bytes, so a bit more work is involved
126
138
else {
139
+ var slicesPerChunk = ( 0 , _lcm2 . default ) ( bitsPerChar , BITS_PER_BYTE ) / BITS_PER_BYTE ;
127
140
return function ( chunk , slice , bytes ) {
128
- var slicesPerChunk = ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) / bitsPerByte ;
129
141
var bNum = chunk * slicesPerChunk ;
130
142
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 ;
134
149
135
150
var ndx = ( bytes [ bNum + lOffset ] << lShift & 0xff ) >> rShift ;
136
151
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 ;
139
153
if ( rShift < rShiftIt ) {
140
154
ndx += bytes [ bNum + rOffset ] >> rShiftIt ;
141
155
}
0 commit comments