All files parse.js

100% Statements 135/135
100% Branches 13/13
100% Functions 1/1
100% Lines 135/135

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 1361x 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 116x 116x 116x 116x 116x 116x 116x 116x 32x 32x 32x 32x 32x 32x 32x 6x 6x 32x 32x 32x 18x 18x 18x 18x 18x 18x 18x 18x 32x 32x 84x 116x 25x 25x 25x 25x 25x 25x 25x 59x 116x 23x 23x 23x 23x 23x 23x 23x 36x 116x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 13x 13x 116x 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 max = require( '@stdlib/math/base/special/fast/max' );
var Number = require( '@stdlib/number/ctor' );
 
 
// VARIABLES //
 
var TWO_32 = 0x100000000; // 2^32
var RE_DEC = /^[0-9]+$/;
var RE_HEX = /^0[Xx][0-9A-Fa-f]+$/;
var RE_BIN = /^0[Bb][01]+$/;
var RE_OCT = /^0[Oo][0-7]+$/;
 
 
// MAIN //
 
/**
* Parses an arbitrary-length string representation of an unsigned integer and converts to a double word representation of unsigned 64 bit integer.
*
* @private
* @param {string} value - string representation of an unsigned integer
* @returns {Array} high and low words of an unsigned 64-bit integer
*
* @example
* var v = parseString( '0xffffffff0000ffff' );
* // returns [ 4294967295, 65535 ]
*
* v = parseString( '0x1ffffffff0000ffff' );
* // returns [ 4294967295, 65535 ]
*
* v = parseString( '0xffff' );
* // returns [ 0, 65535 ]
*
* v = parseString( '0o100' );
* // returns [ 0, 64 ]
*
* v = parseString( '0b100' );
* // returns [ 0, 4 ]
*
* v = parseString( '4294967296' );
* // returns [ 1, 0 ]
*/
function parseString( value ) {
	var high;
	var low;
	var tmp;
	var j;
	var i;
 
	if ( RE_DEC.test( value ) ) {
		// Chunked parsing for decimal string...
		// Process the first chunk so that the remaining chunks are all evenly sized (6 digits).
		// Because 6 is the maximum number of digits for a decimal integer that can be safely multiplied with a 32-bit integer without precision loss.
		i = ( value.length % 6 ) || 6;
 
		// If possible, take a bigger chunk:
		if ( i + 6 <= 9 ) {
			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 >>> 0, low >>> 0 ];
	}
 
	if ( RE_HEX.test( value ) ) {
		j = max( 2, value.length - 8 );
		i = max( 2, value.length - 16 );
		return [
			Number( '0x0' + value.slice( i, j ) ) >>> 0,
			Number( '0x' + value.slice( j ) ) >>> 0
		];
	}
 
	if ( RE_BIN.test( value ) ) {
		j = max( 2, value.length - 32 );
		i = max( 2, value.length - 64 );
		return [
			Number( '0b0' + value.slice( i, j ) ) >>> 0,
			Number( '0b' + value.slice( j ) ) >>> 0
		];
	}
 
	if ( RE_OCT.test( value ) ) {
		j = max( 2, value.length - 11 );
		i = max( 2, value.length - 22 );
 
		high = Number( '0o0' + value.slice( i, j ) );
		low = Number( '0o' + value.slice( j ) );
 
		/*
		* Since 11 octal digits are equivalent to 33 bits, 1 msb from `low` will be transferred to `high`.
		* And the excess bits in `high` and `low` will be truncated via the `>>> 0` at return.
		*/
		high = ( high << 1 ) | ( low >= TWO_32 );
 
		return [ high >>> 0, low >>> 0 ];
	}
 
	return [ -1, -1 ];
}
 
 
// EXPORTS //
 
module.exports = parseString;