MCPcopy Create free account
hub / github.com/chronoxor/FastBinaryEncoding / toString

Method toString

projects/JavaScript/proto/int64.js:288–328  ·  view source on GitHub ↗

* Converts the Int64 to a string written in the specified radix * @this {!Int64} * @param {number=} radix Radix (2-36), defaults to 10 * @returns {string} Result string

(radix = 10)

Source from the content-addressed store, hash-verified

286 * @returns {string} Result string
287 */
288 toString (radix = 10) {
289 radix = radix || 10
290 if ((radix < 2) || (radix > 36)) {
291 throw new RangeError('Radix must be in range [2, 36]')
292 }
293 if (this.isZero) {
294 return '0'
295 }
296 if (this.isNegative) {
297 if (this.eq(INT64_MIN)) {
298 // We need to change the Long value before it can be negated, so we remove
299 // the bottom-most digit in this base and then recurse to do the rest.
300 let radixLong = Int64.fromNumber(radix)
301 let div = this.div(radixLong)
302 let rem1 = div.mul(radixLong).sub(this)
303 return div.toString(radix) + rem1.toInt32().toString(radix)
304 } else {
305 return '-' + this.neg().toString(radix)
306 }
307 }
308
309 // Do several (6) digits each time through the loop, so as to
310 // minimize the calls to the very expensive emulated div.
311 let radixToPower = Int64.fromNumber(Math.pow(radix, 6))
312 let rem = this
313 let result = ''
314 while (true) {
315 let remDiv = rem.div(radixToPower)
316 let intval = rem.sub(remDiv.mul(radixToPower)).toInt32() >>> 0
317 let digits = intval.toString(radix)
318 rem = remDiv
319 if (rem.isZero) {
320 return digits + result
321 } else {
322 while (digits.length < 6) {
323 digits = '0' + digits
324 }
325 result = '' + digits + result
326 }
327 }
328 }
329
330 /**
331 * Converts the Int64 to Int64

Callers 1

toStringMethod · 0.45

Calls 7

eqMethod · 0.95
divMethod · 0.95
negMethod · 0.95
fromNumberMethod · 0.45
subMethod · 0.45
mulMethod · 0.45
toInt32Method · 0.45

Tested by

no test coverage detected