Commit 0405cec4 authored by OzzieIsaacs's avatar OzzieIsaacs

Code cosmetics

Bugfix "Error load settings"
parent 378a0ca6
...@@ -30,20 +30,20 @@ bitjs.io = bitjs.io || {}; ...@@ -30,20 +30,20 @@ bitjs.io = bitjs.io || {};
* @param {ArrayBuffer} ab An ArrayBuffer object or a Uint8Array. * @param {ArrayBuffer} ab An ArrayBuffer object or a Uint8Array.
* @param {boolean} rtl Whether the stream reads bits from the byte starting * @param {boolean} rtl Whether the stream reads bits from the byte starting
* from bit 7 to 0 (true) or bit 0 to 7 (false). * from bit 7 to 0 (true) or bit 0 to 7 (false).
* @param {Number} opt_offset The offset into the ArrayBuffer * @param {Number} optOffset The offset into the ArrayBuffer
* @param {Number} opt_length The length of this BitStream * @param {Number} optLength The length of this BitStream
*/ */
bitjs.io.BitStream = function(ab, rtl, opt_offset, opt_length) { bitjs.io.BitStream = function(ab, rtl, optOffset, optLength) {
if (!ab || !ab.toString || ab.toString() !== "[object ArrayBuffer]") { if (!ab || !ab.toString || ab.toString() !== "[object ArrayBuffer]") {
throw "Error! BitArray constructed with an invalid ArrayBuffer object"; throw "Error! BitArray constructed with an invalid ArrayBuffer object";
} }
var offset = opt_offset || 0; var offset = optOffset || 0;
var length = opt_length || ab.byteLength; var length = optLength || ab.byteLength;
this.bytes = new Uint8Array(ab, offset, length); this.bytes = new Uint8Array(ab, offset, length);
this.bytePtr = 0; // tracks which byte we are on this.bytePtr = 0; // tracks which byte we are on
this.bitPtr = 0; // tracks which bit we are on (can have values 0 through 7) this.bitPtr = 0; // tracks which bit we are on (can have values 0 through 7)
this.peekBits = rtl ? this.peekBits_rtl : this.peekBits_ltr; this.peekBits = rtl ? this.peekBitsRtl : this.peekBitsLtr;
}; };
...@@ -57,12 +57,12 @@ bitjs.io = bitjs.io || {}; ...@@ -57,12 +57,12 @@ bitjs.io = bitjs.io || {};
* @param {boolean=} movePointers Whether to move the pointer, defaults false. * @param {boolean=} movePointers Whether to move the pointer, defaults false.
* @return {number} The peeked bits, as an unsigned number. * @return {number} The peeked bits, as an unsigned number.
*/ */
bitjs.io.BitStream.prototype.peekBits_ltr = function(n, movePointers) { bitjs.io.BitStream.prototype.peekBitsLtr = function(n, movePointers) {
if (n <= 0 || typeof n != typeof 1) { if (n <= 0 || typeof n != typeof 1) {
return 0; return 0;
} }
var movePointers = movePointers || false, movePointers = movePointers || false,
bytePtr = this.bytePtr, bytePtr = this.bytePtr,
bitPtr = this.bitPtr, bitPtr = this.bitPtr,
result = 0, result = 0,
...@@ -77,21 +77,20 @@ bitjs.io = bitjs.io || {}; ...@@ -77,21 +77,20 @@ bitjs.io = bitjs.io || {};
if (bytePtr >= bytes.length) { if (bytePtr >= bytes.length) {
throw "Error! Overflowed the bit stream! n=" + n + ", bytePtr=" + bytePtr + ", bytes.length=" + throw "Error! Overflowed the bit stream! n=" + n + ", bytePtr=" + bytePtr + ", bytes.length=" +
bytes.length + ", bitPtr=" + bitPtr; bytes.length + ", bitPtr=" + bitPtr;
return -1;
} }
var numBitsLeftInThisByte = (8 - bitPtr); var numBitsLeftInThisByte = (8 - bitPtr);
var mask;
if (n >= numBitsLeftInThisByte) { if (n >= numBitsLeftInThisByte) {
var mask = (BITMASK[numBitsLeftInThisByte] << bitPtr); mask = (BITMASK[numBitsLeftInThisByte] << bitPtr);
result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn); result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn);
bytePtr++; bytePtr++;
bitPtr = 0; bitPtr = 0;
bitsIn += numBitsLeftInThisByte; bitsIn += numBitsLeftInThisByte;
n -= numBitsLeftInThisByte; n -= numBitsLeftInThisByte;
} } else {
else { mask = (BITMASK[n] << bitPtr);
var mask = (BITMASK[n] << bitPtr);
result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn); result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn);
bitPtr += n; bitPtr += n;
...@@ -119,7 +118,7 @@ bitjs.io = bitjs.io || {}; ...@@ -119,7 +118,7 @@ bitjs.io = bitjs.io || {};
* @param {boolean=} movePointers Whether to move the pointer, defaults false. * @param {boolean=} movePointers Whether to move the pointer, defaults false.
* @return {number} The peeked bits, as an unsigned number. * @return {number} The peeked bits, as an unsigned number.
*/ */
bitjs.io.BitStream.prototype.peekBits_rtl = function(n, movePointers) { bitjs.io.BitStream.prototype.peekBitsRtl = function(n, movePointers) {
if (n <= 0 || typeof n != typeof 1) { if (n <= 0 || typeof n != typeof 1) {
return 0; return 0;
} }
...@@ -173,8 +172,8 @@ bitjs.io = bitjs.io || {}; ...@@ -173,8 +172,8 @@ bitjs.io = bitjs.io || {};
*/ */
bitjs.io.BitStream.prototype.getBits = function() { bitjs.io.BitStream.prototype.getBits = function() {
return (((((this.bytes[this.bytePtr] & 0xff) << 16) + return (((((this.bytes[this.bytePtr] & 0xff) << 16) +
((this.bytes[this.bytePtr+1] & 0xff) << 8) + ((this.bytes[this.bytePtr + 1] & 0xff) << 8) +
((this.bytes[this.bytePtr+2] & 0xff))) >>> (8-this.bitPtr)) & 0xffff); ((this.bytes[this.bytePtr + 2] & 0xff))) >>> (8 - this.bitPtr)) & 0xffff);
}; };
...@@ -203,11 +202,11 @@ bitjs.io = bitjs.io || {}; ...@@ -203,11 +202,11 @@ bitjs.io = bitjs.io || {};
// from http://tools.ietf.org/html/rfc1951#page-11 // from http://tools.ietf.org/html/rfc1951#page-11
// "Any bits of input up to the next byte boundary are ignored." // "Any bits of input up to the next byte boundary are ignored."
while (this.bitPtr != 0) { while (this.bitPtr !== 0) {
this.readBits(1); this.readBits(1);
} }
var movePointers = movePointers || false; movePointers = movePointers || false;
var bytePtr = this.bytePtr, var bytePtr = this.bytePtr,
bitPtr = this.bitPtr; bitPtr = this.bitPtr;
...@@ -235,13 +234,13 @@ bitjs.io = bitjs.io || {}; ...@@ -235,13 +234,13 @@ bitjs.io = bitjs.io || {};
* out of an ArrayBuffer. In this buffer, everything must be byte-aligned. * out of an ArrayBuffer. In this buffer, everything must be byte-aligned.
* *
* @param {ArrayBuffer} ab The ArrayBuffer object. * @param {ArrayBuffer} ab The ArrayBuffer object.
* @param {number=} opt_offset The offset into the ArrayBuffer * @param {number=} optOffset The offset into the ArrayBuffer
* @param {number=} opt_length The length of this BitStream * @param {number=} optLength The length of this BitStream
* @constructor * @constructor
*/ */
bitjs.io.ByteStream = function(ab, opt_offset, opt_length) { bitjs.io.ByteStream = function(ab, optOffset, optLength) {
var offset = opt_offset || 0; var offset = optOffset || 0;
var length = opt_length || ab.byteLength; var length = optLength || ab.byteLength;
this.bytes = new Uint8Array(ab, offset, length); this.bytes = new Uint8Array(ab, offset, length);
this.ptr = 0; this.ptr = 0;
}; };
...@@ -256,8 +255,9 @@ bitjs.io = bitjs.io || {}; ...@@ -256,8 +255,9 @@ bitjs.io = bitjs.io || {};
*/ */
bitjs.io.ByteStream.prototype.peekNumber = function(n) { bitjs.io.ByteStream.prototype.peekNumber = function(n) {
// TODO: return error if n would go past the end of the stream? // TODO: return error if n would go past the end of the stream?
if (n <= 0 || typeof n != typeof 1) if (n <= 0 || typeof n !== typeof 1){
return -1; return -1;
}
var result = 0; var result = 0;
// read from last byte to first byte and roll them in // read from last byte to first byte and roll them in
...@@ -382,7 +382,7 @@ bitjs.io = bitjs.io || {}; ...@@ -382,7 +382,7 @@ bitjs.io = bitjs.io || {};
* @constructor * @constructor
*/ */
bitjs.io.ByteBuffer = function(numBytes) { bitjs.io.ByteBuffer = function(numBytes) {
if (typeof numBytes != typeof 1 || numBytes <= 0) { if (typeof numBytes !== typeof 1 || numBytes <= 0) {
throw "Error! ByteBuffer initialized with '" + numBytes + "'"; throw "Error! ByteBuffer initialized with '" + numBytes + "'";
} }
this.data = new Uint8Array(numBytes); this.data = new Uint8Array(numBytes);
...@@ -447,12 +447,12 @@ bitjs.io = bitjs.io || {}; ...@@ -447,12 +447,12 @@ bitjs.io = bitjs.io || {};
*/ */
bitjs.io.ByteBuffer.prototype.writeSignedNumber = function(num, numBytes) { bitjs.io.ByteBuffer.prototype.writeSignedNumber = function(num, numBytes) {
if (numBytes < 1) { if (numBytes < 1) {
throw 'Trying to write into too few bytes: ' + numBytes; throw "Trying to write into too few bytes: " + numBytes;
} }
var HALF = Math.pow(2, (numBytes * 8) - 1); var HALF = Math.pow(2, (numBytes * 8) - 1);
if (num >= HALF || num < -HALF) { if (num >= HALF || num < -HALF) {
throw 'Trying to write ' + num + ' into only ' + numBytes + ' bytes'; throw "Trying to write " + num + "" into only " + numBytes + " bytes";
} }
// Roll 8-bits at a time into an array of bytes. // Roll 8-bits at a time into an array of bytes.
...@@ -474,7 +474,7 @@ bitjs.io = bitjs.io || {}; ...@@ -474,7 +474,7 @@ bitjs.io = bitjs.io || {};
for (var i = 0; i < str.length; ++i) { for (var i = 0; i < str.length; ++i) {
var curByte = str.charCodeAt(i); var curByte = str.charCodeAt(i);
if (curByte < 0 || curByte > 255) { if (curByte < 0 || curByte > 255) {
throw 'Trying to write a non-ASCII string!'; throw "Trying to write a non-ASCII string!";
} }
this.insertByte(curByte); this.insertByte(curByte);
} }
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment