|
53 | 53 | * case would often result in infinite recursion. |
54 | 54 | * |
55 | 55 | * @exports Long |
| 56 | + * @class A Long class for representing a 64-bit two's-complement integer value. |
56 | 57 | * @param {number} low The low (signed) 32 bits of the long. |
57 | 58 | * @param {number} high The high (signed) 32 bits of the long. |
58 | | - * @param {boolean=} unsigned Whether unsigned or not. Defaults to false (signed). |
| 59 | + * @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed). |
59 | 60 | * @constructor |
60 | 61 | */ |
61 | 62 | var Long = function(low, high, unsigned) { |
|
107 | 108 | * @expose |
108 | 109 | */ |
109 | 110 | Long.fromInt = function(value, unsigned) { |
| 111 | + var obj, cachedObj; |
110 | 112 | if (!unsigned) { |
111 | 113 | value = value | 0; |
112 | 114 | if (-128 <= value && value < 128) { |
113 | | - var cachedObj = INT_CACHE[value]; |
| 115 | + cachedObj = INT_CACHE[value]; |
114 | 116 | if (cachedObj) return cachedObj; |
115 | 117 | } |
116 | | - var obj = new Long(value, value < 0 ? -1 : 0, false); |
| 118 | + obj = new Long(value, value < 0 ? -1 : 0, false); |
117 | 119 | if (-128 <= value && value < 128) { |
118 | 120 | INT_CACHE[value] = obj; |
119 | 121 | } |
120 | 122 | return obj; |
121 | 123 | } else { |
122 | 124 | value = value >>> 0; |
123 | 125 | if (0 <= value && value < 256) { |
124 | | - var cachedObj = UINT_CACHE[value]; |
| 126 | + cachedObj = UINT_CACHE[value]; |
125 | 127 | if (cachedObj) return cachedObj; |
126 | 128 | } |
127 | | - var obj = new Long(value, (value | 0) < 0 ? -1 : 0, true); |
| 129 | + obj = new Long(value, (value | 0) < 0 ? -1 : 0, true); |
128 | 130 | if (0 <= value && value < 256) { |
129 | 131 | UINT_CACHE[value] = obj; |
130 | 132 | } |
|
201 | 203 | if (str.length == 0) { |
202 | 204 | throw(new Error('number format error: empty string')); |
203 | 205 | } |
204 | | - if (typeof unsigned == 'number') { // For goog.math.Long compatibility |
| 206 | + if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") { |
| 207 | + return Long.ZERO; |
| 208 | + } |
| 209 | + if (typeof unsigned === 'number') { // For goog.math.Long compatibility |
205 | 210 | radix = unsigned; |
206 | 211 | unsigned = false; |
207 | 212 | } |
|
370 | 375 | if (this.isZero()) { |
371 | 376 | return '0'; |
372 | 377 | } |
| 378 | + var rem; |
373 | 379 | if (this.isNegative()) { // Unsigned Longs are never negative |
374 | 380 | if (this.equals(Long.MIN_SIGNED_VALUE)) { |
375 | 381 | // We need to change the Long value before it can be negated, so we remove |
376 | 382 | // the bottom-most digit in this base and then recurse to do the rest. |
377 | 383 | var radixLong = Long.fromNumber(radix); |
378 | 384 | var div = this.div(radixLong); |
379 | | - var rem = div.multiply(radixLong).subtract(this); |
| 385 | + rem = div.multiply(radixLong).subtract(this); |
380 | 386 | return div.toString(radix) + rem.toInt().toString(radix); |
381 | 387 | } else { |
382 | 388 | return '-' + this.negate().toString(radix); |
|
386 | 392 | // Do several (6) digits each time through the loop, so as to |
387 | 393 | // minimize the calls to the very expensive emulated div. |
388 | 394 | var radixToPower = Long.fromNumber(Math.pow(radix, 6)); |
389 | | - var rem = this; |
| 395 | + rem = this; |
390 | 396 | var result = ''; |
391 | 397 | while (true) { |
392 | 398 | var remDiv = rem.div(radixToPower); |
|
0 commit comments