Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 55x 55x 55x 55x 55x 55x 55x 44x 44x 55x 11x 11x 11x 11x 55x 55x 55x 55x 55x 55x 11x 11x 11x 11x 11x 2x 2x 2x 2x 11x 53x 53x 53x 53x 55x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 2x 2x 2x 2x 101x 101x 103x 46x 46x 46x 46x 46x 55x 55x 55x 103x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// VARIABLES //
var TWO_32 = 0x100000000; // 2^32
/* eslint-disable array-element-newline */
// TODO: Explain derivation
// Chunk sizes for optimal parsing in within JavaScript's integer 53-bit precision.
var CHUNKMAP = [
// <chunk1_len>, <chunk2_len>, <multiplier> // radix
53, 11, 2048, // 2
33, 8, 6561, // 3
26, 6, 4096, // 4
22, 6, 15625, // 5
20, 5, 7776, // 6
18, 5, 16807, // 7
17, 5, 32768, // 8
16, 5, 59049, // 9
15, 5, 100000, // 10
15, 4, 14641, // 11
14, 4, 20736, // 12
14, 4, 28561, // 13
13, 4, 38416, // 14
13, 4, 50625, // 15
13, 3, 4096, // 16
12, 4, 83521, // 17
12, 4, 104976, // 18
12, 4, 130321, // 19
12, 3, 8000, // 20
12, 3, 9261, // 21
11, 4, 234256, // 22
11, 4, 279841, // 23
11, 3, 13824, // 24
11, 3, 15625, // 25
11, 3, 17576, // 26
11, 3, 19683, // 27
11, 3, 21952, // 28
10, 4, 707281, // 29
10, 4, 810000, // 30
10, 3, 29791, // 31
10, 3, 32768, // 32
10, 3, 35937, // 33
10, 3, 39304, // 34
10, 3, 42875, // 35
10, 3, 46656 // 36
];
/* eslint-enable array-element-newline */
/**
* Parses a string representation of an unsigned 64-bit integer and converts to a double word representation as two 32-bit unsigned integers.
*
* @private
* @param {string} value - string representation of an unsigned integer
* @param {PositiveInteger} radix - radix (base) to use for string conversion (2-36)
* @param {PositiveInteger} len1 - max length of the first chunk
* @param {PositiveInteger} len2 - max length of the second chunk
* @param {PositiveInteger} mult - multiplier for the place value calculation (radix raised to the power of len2)
* @param {Array} out - output array
* @returns {Array} high and low words of an unsigned 64-bit integer
*
* @example
* var v = chunkedParse( '1111111111111111111111111111111111111111111111111111111111111111', 2, CHUNKMAP[0], CHUNKMAP[1], CHUNKMAP[2], [ 0, 0 ] );
* // returns [ 4294967295, 4294967295 ]
*
* v = chunkedParse( '18446744073709551615', 10, CHUNKMAP[24], CHUNKMAP[25], CHUNKMAP[26], [ 0, 0 ] );
* // returns [ 4294967295, 4294967295 ]
*
* v = chunkedParse( '3w5e11264sgsf', 36, CHUNKMAP[102], CHUNKMAP[103], CHUNKMAP[104], [ 0, 0 ] );
* // returns [ 4294967295, 4294967295 ]
*
*/
function chunkedParse( value, radix, len1, len2, mult, out ) {
var chunk1;
var chunk2;
var hi;
var lo;
if ( value.length <= len1 ) {
// Parse in a single pass if value is within MAX_SAFE_INTEGER
chunk1 = parseInt( value, radix );
} else {
// Otherwise use a big chunk (start-aligned) and a small chunk (end-aligned)
chunk1 = parseInt( value.slice( 0, -len2 ), radix ); // Everything until last `len2` digits
chunk2 = parseInt( value.slice( -len2 ), radix ); // `len2` digits from the end
}
hi = ( chunk1 / TWO_32 ) >>> 0; // Integer division by 2^32
lo = chunk1 >>> 0; // 32-bit truncation
// Process chunk2 if applicable
if ( value.length > len1 ) {
lo = ( lo * mult ) + chunk2;
hi = ( hi * mult ) + ( ( lo / TWO_32 ) >>> 0 );
lo >>>= 0; // 32-bit truncation
if ( hi >= TWO_32 ) {
// Too big for Uint64
out[0] = out[1] = -1;
return out;
}
}
out[0] = hi;
out[1] = lo;
return out;
}
// MAIN //
/**
* Parses a string representation of an unsigned 64-bit integer and converts to a double word representation as two 32-bit unsigned integers.
*
* @private
* @param {string} value - string representation of an unsigned integer
* @param {PositiveInteger} radix - radix (base) to use for string conversion (2-36)
* @param {Array} out - output array
* @returns {Array} high and low words of an unsigned 64-bit integer
*
* @example
* var v = parse( '18446744073709551615', 10, [ 0, 0 ] );
* // returns [ 4294967295, 4294967295 ]
*
* v = parse( 'ffffffffffffffff', 16, [ 0, 0] );
* // returns [ 4294967295, 4294967295 ]
*
* v = parse( '3w5e11264sgsf', 36, [ 0, 0] );
* // returns [ 4294967295, 4294967295 ]
*/
function parse( value, radix, out ) {
var len1;
var len2;
var mult;
var idx;
// Compute the index into a pre-computed strided table:
idx = 3 * ( radix-2 );
// Use a pre-computed table to select chunk lengths and the multiplier
len1 = CHUNKMAP[ idx ];
len2 = CHUNKMAP[ idx+1 ];
mult = CHUNKMAP[ idx+2 ];
if ( value.length > len1+len2 ) {
// Too big for Uint64
out[0] = out[1] = -1;
return out;
}
// Fast path for bases 2, 4, and 16 due to evenly splittable 32-bit halves
if ( radix === 2 || radix === 4 || radix === 16 ) {
len1 = len2 = ( len1 + len2 ) / 2;
out[0] = parseInt( '0' + value.slice( 0, -len2 ), radix );
out[1] = parseInt( value.slice( -len2 ), radix );
return out;
}
// General path for other bases
return chunkedParse( value, radix, len1, len2, mult, out );
}
// EXPORTS //
module.exports = parse;
|