add recursion in size check to reduce code duplication
This commit is contained in:
parent
1eeabc2c08
commit
1486a8befa
1 changed files with 40 additions and 94 deletions
68
js/utils.js
68
js/utils.js
|
@ -341,71 +341,18 @@ Base64 = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
let new_length = this.length + length;
|
let new_length = this.length + length;
|
||||||
let mult = 1;
|
if (this.bits.length * this.bits.BYTES_PER_ELEMENT * 8 < new_length) {
|
||||||
while (mult * this.bits.length * this.bits.BYTES_PER_ELEMENT * 8 < new_length) {
|
//resize the internal repr by a factor of 2 before recursive calling
|
||||||
mult *= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mult > 1) {
|
|
||||||
//we have to expand the uint32array repr - double size
|
|
||||||
let bit_vec = [];
|
let bit_vec = [];
|
||||||
|
for (const int of this.bits) {
|
||||||
for (const uint of this.bits) {
|
|
||||||
bit_vec.push(uint);
|
|
||||||
}
|
|
||||||
if (typeof data === "string") {
|
|
||||||
let int = bit_vec[bit_vec.length - 1];
|
|
||||||
let bv_idx = this.length;
|
|
||||||
let updated_curr = false;
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
|
||||||
let char = Base64.toInt(data[i]);
|
|
||||||
let pre_pos = bv_idx % 32;
|
|
||||||
int |= (char << bv_idx);
|
|
||||||
bv_idx += 6;
|
|
||||||
let post_pos = bv_idx % 32;
|
|
||||||
if (post_pos < pre_pos) { //we have to have filled up the integer
|
|
||||||
if (bit_vec.length == this.bits.length && !updated_curr) {
|
|
||||||
bit_vec[bit_vec.length - 1] = int;
|
|
||||||
updated_curr = true;
|
|
||||||
} else {
|
|
||||||
bit_vec.push(int);
|
bit_vec.push(int);
|
||||||
}
|
}
|
||||||
int = (char >>> (6 - post_pos));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i == data.length - 1) {
|
|
||||||
if (bit_vec.length == this.bits.length && !updated_curr) {
|
|
||||||
bit_vec[bit_vec.length - 1] = int;
|
|
||||||
} else if (post_pos != 0) {
|
|
||||||
bit_vec.push(int);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (typeof data === "number") {
|
|
||||||
//convert to int just in case
|
|
||||||
let int = Math.round(data);
|
|
||||||
|
|
||||||
//range of numbers that "could" fit in a uint32 -> [0, 2^32) U [-2^31, 2^31)
|
|
||||||
if (data > 2**32 - 1 || data < -(2 ** 31)) {
|
|
||||||
throw new RangeError("Numerical data has to fit within a 32-bit integer range to instantiate a BitVector.");
|
|
||||||
}
|
|
||||||
//could be split between multiple new ints
|
|
||||||
//reminder that shifts implicitly mod 32
|
|
||||||
bit_vec[bit_vec.length - 1] |= ((int & ~((~0) << length)) << (this.length));
|
|
||||||
if (((this.length - 1) % 32 + 1) + length > 32) {
|
|
||||||
bit_vec.push(int >>> (32 - this.length));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new TypeError("BitVector must be appended with a Number or a B64 String");
|
|
||||||
}
|
|
||||||
|
|
||||||
//pad the end with 0s
|
|
||||||
for (let i = bit_vec.length; i < this.bits.length; i++) {
|
|
||||||
bit_vec.push(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
bit_vec.concat(Array.apply(0, this.bits.length));
|
||||||
this.bits = new Uint32Array(bit_vec);
|
this.bits = new Uint32Array(bit_vec);
|
||||||
} else {
|
return this.append(data, length);
|
||||||
|
}
|
||||||
|
|
||||||
//just write to the original bitvec
|
//just write to the original bitvec
|
||||||
let curr_idx = Math.floor(this.length / 32);
|
let curr_idx = Math.floor(this.length / 32);
|
||||||
let pos = this.length;
|
let pos = this.length;
|
||||||
|
@ -442,7 +389,6 @@ Base64 = (function () {
|
||||||
} else {
|
} else {
|
||||||
throw new TypeError("BitVector must be appended with a Number or a B64 String");
|
throw new TypeError("BitVector must be appended with a Number or a B64 String");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//update length
|
//update length
|
||||||
this.length += length;
|
this.length += length;
|
||||||
|
|
Loading…
Reference in a new issue