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

Method fromString

projects/JavaScript/proto/int64.js:1075–1111  ·  view source on GitHub ↗

* Returns UInt64 representation of the given string, written using the specified radix. * @param {string} str The textual representation of the UInt64 * @param {number=} radix The radix in which the text is written (2-36), defaults to 10 * @returns {!UInt64} The corresponding UInt64 value

(str, radix = 10)

Source from the content-addressed store, hash-verified

1073 * @returns {!UInt64} The corresponding UInt64 value
1074 */
1075 static fromString (str, radix = 10) {
1076 if (str.length === 0) {
1077 throw new Error('Empty string!')
1078 }
1079 if ((str === 'NaN') || (str === 'Infinity') || (str === '+Infinity') || (str === '-Infinity')) {
1080 return UINT64_ZERO
1081 }
1082 radix = radix || 10
1083 if ((radix < 2) || (radix > 36)) {
1084 throw new RangeError('radix')
1085 }
1086
1087 let p = str.indexOf('-')
1088 if (p > 0) {
1089 throw new Error('Interior hyphen!')
1090 } else if (p === 0) {
1091 return UInt64.fromString(str.substring(1), radix).neg()
1092 }
1093
1094 // Do several (8) digits each time through the loop, so as to
1095 // minimize the calls to the very expensive emulated div.
1096 let radixToPower = UInt64.fromNumber(Math.pow(radix, 8))
1097
1098 let result = UINT64_ZERO
1099 for (let i = 0; i < str.length; i += 8) {
1100 let size = Math.min(8, str.length - i)
1101 let value = parseInt(str.substring(i, i + size), radix)
1102 if (size < 8) {
1103 let power = UInt64.fromNumber(Math.pow(radix, size))
1104 result = result.mul(power).add(UInt64.fromNumber(value))
1105 } else {
1106 result = result.mul(radixToPower)
1107 result = result.add(UInt64.fromNumber(value))
1108 }
1109 }
1110 return result
1111 }
1112
1113 /**
1114 * Converts the specified value to a UInt64 using the appropriate from* function for its type.

Callers

nothing calls this directly

Calls 5

negMethod · 0.45
fromStringMethod · 0.45
fromNumberMethod · 0.45
addMethod · 0.45
mulMethod · 0.45

Tested by

no test coverage detected