* Returns Int64 representation of the given string, written using the specified radix. * @param {string} str The textual representation of the Int64 * @param {number=} radix The radix in which the text is written (2-36), defaults to 10 * @returns {!Int64} The corresponding Int64 value
(str, radix = 10)
| 159 | * @returns {!Int64} The corresponding Int64 value |
| 160 | */ |
| 161 | static fromString (str, radix = 10) { |
| 162 | if (str.length === 0) { |
| 163 | throw new Error('Empty string!') |
| 164 | } |
| 165 | if ((str === 'NaN') || (str === 'Infinity') || (str === '+Infinity') || (str === '-Infinity')) { |
| 166 | return INT64_ZERO |
| 167 | } |
| 168 | radix = radix || 10 |
| 169 | if ((radix < 2) || (radix > 36)) { |
| 170 | throw new RangeError('radix') |
| 171 | } |
| 172 | |
| 173 | let p = str.indexOf('-') |
| 174 | if (p > 0) { |
| 175 | throw new Error('Interior hyphen!') |
| 176 | } else if (p === 0) { |
| 177 | return Int64.fromString(str.substring(1), radix).neg() |
| 178 | } |
| 179 | |
| 180 | // Do several (8) digits each time through the loop, so as to |
| 181 | // minimize the calls to the very expensive emulated div. |
| 182 | let radixToPower = Int64.fromNumber(Math.pow(radix, 8)) |
| 183 | |
| 184 | let result = INT64_ZERO |
| 185 | for (let i = 0; i < str.length; i += 8) { |
| 186 | let size = Math.min(8, str.length - i) |
| 187 | let value = parseInt(str.substring(i, i + size), radix) |
| 188 | if (size < 8) { |
| 189 | let power = Int64.fromNumber(Math.pow(radix, size)) |
| 190 | result = result.mul(power).add(Int64.fromNumber(value)) |
| 191 | } else { |
| 192 | result = result.mul(radixToPower) |
| 193 | result = result.add(Int64.fromNumber(value)) |
| 194 | } |
| 195 | } |
| 196 | return result |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Converts the specified value to a Int64 using the appropriate from* function for its type. |
no test coverage detected