| @@ 40-68 (lines=29) @@ | ||
| 37 | /** |
|
| 38 | * @param {number} bits The number of bits used by the integer. |
|
| 39 | */ |
|
| 40 | constructor(bits) { |
|
| 41 | /** |
|
| 42 | * The number of bits used by one number. |
|
| 43 | * @type {number} |
|
| 44 | */ |
|
| 45 | this.bits = bits; |
|
| 46 | /** |
|
| 47 | * The number of bytes used by one number. |
|
| 48 | * @type {number} |
|
| 49 | */ |
|
| 50 | this.bytes = bits < 8 ? 1 : Math.ceil(bits / 8); |
|
| 51 | /** |
|
| 52 | * @type {number} |
|
| 53 | * @protected |
|
| 54 | */ |
|
| 55 | this.max = Math.pow(2, bits) - 1; |
|
| 56 | /** |
|
| 57 | * @type {number} |
|
| 58 | * @protected |
|
| 59 | */ |
|
| 60 | this.min = 0; |
|
| 61 | /** @type {number} */ |
|
| 62 | let r = 8 - ((((bits - 1) | 7) + 1) - bits); |
|
| 63 | /** |
|
| 64 | * @type {number} |
|
| 65 | * @private |
|
| 66 | */ |
|
| 67 | this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1; |
|
| 68 | } |
|
| 69 | ||
| 70 | /** |
|
| 71 | * Write one unsigned integer to a byte buffer. |
|
| @@ 1-1 (lines=1) @@ | ||
| 1 | (function(e,b){"object"===typeof exports&&"undefined"!==typeof module?b(exports):"function"===typeof define&&define.amd?define(["exports"],b):(e=e||self,b(e.UintBuffer={}))})(this,function(e){var b=function(a){this.bits=a;this.bytes=8>a?1:Math.ceil(a/8);this.max=Math.pow(2,a)-1;this.min=0;a=8-((a-1|7)+1-a);this.lastByteMask_=Math.pow(2,0<a?a:8)-1};b.prototype.pack=function(a,d,c){c=void 0===c?0:c;if(d!==d||"number"!=typeof d)throw new TypeError;this.overflow(d);a[c]=(0>d?d+Math.pow(2,this.bits):d)& |
|
| 2 | 255;c++;for(var b=this.bytes,e=2;e<b;e++)a[c]=Math.floor(d/Math.pow(2,8*(e-1)))&255,c++;8<this.bits&&(a[c]=Math.floor(d/Math.pow(2,8*(this.bytes-1)))&this.lastByteMask_,c++);return c};b.prototype.unpack=function(a,d){var c=this.unpackUnsafe(a,void 0===d?0:d);this.overflow(c);return c};b.prototype.unpackUnsafe=function(a,d){for(var c=0,b=0;b<this.bytes;b++)c+=a[d+b]*Math.pow(256,b);return c};b.prototype.overflow=function(a){if(a>this.max||a<this.min)throw new RangeError;};e.UintBuffer=b;Object.defineProperty(e, |
|
| 3 | "__esModule",{value:!0})}); |
|
| 4 | ||