All files parseuint64dw.js

98.31% Statements 117/119
92.3% Branches 12/13
100% Functions 1/1
98.31% Lines 117/119

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 1201x 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 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 3x 3x 3x 5x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 3x 1x 1x 2x 2x 2x 2x     2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 8x 8x 8x 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';
 
// MODULES //
 
var Number = require( '@stdlib/number/ctor' );
var max = require( '@stdlib/math/base/special/max' );
var trim = require( '@stdlib/string/trim' );
 
 
// VARIABLES //
 
var TWO_32 = 0x100000000;
var RE_HEX = /^0x/i;
var RE_OCT = /^0o/i;
var RE_BIN = /^0b/i;
var RE_INV = /[^0-9]/; // Invalid decimal integer
 
 
// MAIN //
 
/**
* Parses an arbitrary-length string representation of an unsigned integer and converts to double word representation of unsigned 64 bit integer.
*
* @private
* @param {string} value - string representation of an unsigned integer
* @returns {Uint32Array} Unsigned 64-bit integer as high and low words
*
* @example
* var x = parseUint64dw( '0xffffffff0000ffff' );
* // returns [ 4294967295, 65535 ]
*
* x = parseUint64dw( '0x1ffffffff0000ffff' );
* // returns [ 4294967295, 65535 ]
*
* x = parseUint64dw( '0xffff' );
* // returns [ 0, 65535 ]
*
* x = parseUint64dw( '0o100' );
* // returns [ 0, 64 ]
*
* x = parseUint64dw( '0b100' );
* // returns [ 0, 4 ]
*
* x = parseUint64dw( '4294967296' );
* // returns [ 1, 0 ]
*/
function parseUint64dw( value ) {
	var high;
	var low;
	var tmp;
	var j;
	var i;
 
	value = trim( value );
	if ( RE_HEX.test( value ) ) {
		j = max( 2, value.length - 8 );
		i = max( 2, value.length - 16 );
		high = Number( '0x0' + value.slice( i, j ) );
		low = Number( '0x' + value.slice( j ) );
	}
	else if ( RE_OCT.test( value ) ) {
		j = max( 2, value.length - 16 );
		i = max( 2, value.length - 32 );
		high = Number( '0o0' + value.slice( i, j ) );
		low = Number( '0o' + value.slice( j ) );
	}
	else if ( RE_BIN.test( value ) ) {
		j = max( 2, value.length - 32 );
		i = max( 2, value.length - 64 );
		high = Number( '0b0' + value.slice( i, j ) );
		low = Number( '0b' + value.slice( j ) );
	}
	else if ( RE_INV.test( value ) ) {
		high = low = NaN;
	}
	else {
		// Chunked parsing for decimal string
		i = ( value.length % 6 ) || 6; // Processes the first chunk such as the remaining chunks are all evenly sized (6digit)
		if ( i + 6 <= 9 ) { // Takes big chunk when possible
			i += 6;
		}
		high = 0;
		low = Number( value.slice( 0, i ) );
		for ( ; i < value.length; i += 6 ) {
			tmp = Number( value.slice( i, i + 6 ) );
			low = ( low * 1e6 ) + tmp;
			tmp = ( low / TWO_32 ) >>> 0;
			low -= tmp * TWO_32;
			high = ( high * 1e6 ) + tmp;
			tmp = ( high / TWO_32 ) >>> 0;
			high -= tmp * TWO_32;
		}
	}
 
	return [ high, low ];
}
 
 
// EXPORTS //
 
module.exports = parseUint64dw;