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 | 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 126x 126x 126x 126x 126x 126x 126x 126x 7x 7x 119x 119x 126x 3x 3x 116x 126x 6x 6x 116x 116x 116x 25x 116x 25x 91x 27x 66x 39x 39x 116x 116x 77x 77x 126x 116x 126x 4x 4x 112x 112x 112x 126x 1x 1x 112x 112x 112x 112x 112x 126x 9x 9x 103x 103x 103x 103x 103x 103x 126x 4x 4x 99x 126x 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 isBetween = require( '@stdlib/assert/is-between' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isEmptyString = require( '@stdlib/assert/is-empty-string' ).isPrimitive;
var Uint64 = require( '@stdlib/number/uint64/ctor' );
var trim = require( '@stdlib/string/base/trim' );
var slice = require( '@stdlib/string/base/slice' );
var format = require( '@stdlib/string/format' );
var parse = require( './parse.js' );
// VARIABLES //
var RE_HEX = /^0[Xx]/;
var RE_BIN = /^0[Bb]/;
var RE_OCT = /^0[Oo]/;
var DIGITS = '0123456789abcdefghijklmnopqrstuvwxyz';
var WORKSPACE = [ 0, 0 ];
// MAIN //
/**
* Parses a string as an unsigned 64-bit integer.
*
* @param {string} str - string representation of a nonnegative integer
* @param {PositiveInteger} [radix=10] - radix (base) to use for string conversion (2-36)
* @throws {TypeError} must provide a string
* @throws {RangeError} must provide an integer on the interval [2, 36]
* @throws {Error} must provide a string encoding a nonnegative integer
* @returns {Uint64} unsigned 64-bit integer
*
* @example
* var v = parseUint64( '1234' );
* // returns <Uint64>[ 1234n ]
*
* v = parseUint64( '18446744073709551615' );
* // returns <Uint64>[ 18446744073709551615n ]
*
* v = parseUint64( '0xffffffffffffffff' );
* // returns <Uint64>[ 18446744073709551615n ]
*
* v = parseUint64( '3w5e11264sgsf', 36 );
* // returns <Uint64>[ 18446744073709551615n ]
*/
function parseUint64( str, radix ) {
var validChars;
var reInv;
var rad;
var v;
var i;
if ( !isString( str ) ) {
throw new TypeError( format( 'invalid argument. Must provide a string. Value: `%s`.', str ) );
}
v = trim( str );
if ( v[0] === '-' ) {
throw new Error( format( 'invalid argument. Must provide a string encoding a nonnegative integer. Value: `%s`.', str ) );
}
if ( v[0] === '+' ) {
v = slice( v, 1 );
}
if ( arguments.length < 2 ) {
if ( RE_BIN.test( v ) ) {
rad = 2;
} else if ( RE_OCT.test( v ) ) {
rad = 8;
} else if ( RE_HEX.test( v ) ) {
rad = 16;
} else {
rad = 10;
}
if ( rad !== 10 ) {
v = slice( v, 2 );
}
} else if ( !isInteger( radix ) ) {
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', radix ) );
} else if ( isBetween( radix, 2, 36 ) ) {
rad = radix;
} else {
throw new RangeError( format( 'invalid argument. Must provide an integer on the interval [2, 36]. Value: `%s`.', radix ) );
}
if ( isEmptyString( v ) ) {
throw new Error( format( 'invalid argument. Must provide a non-empty string encoding a nonnegative integer. Value: `%s`.', str ) );
}
// Trim leading zeros
i = 0;
while ( ( i < v.length-1 ) && ( v[i] === '0' ) ) {
i += 1;
}
v = slice( v, i );
// Validate digits with respect to radix
validChars = slice( DIGITS, 0, rad );
reInv = new RegExp( format( '[^%s]', validChars ), 'i' );
if ( reInv.test( v ) ) {
throw new Error( format( 'invalid argument. Cannot parse a string containing invalid characters. Value: `%s`.', str ) );
}
// Reset the workspace:
WORKSPACE[ 0 ] = 0;
WORKSPACE[ 1 ] = 0;
parse( v, rad, WORKSPACE );
if ( WORKSPACE[ 0 ] === -1 ) {
throw new RangeError( format( 'invalid argument. Must provide a string encoding a nonnegative integer smaller than 2^64. Value: `%s`.', str ) );
}
return Uint64.from( WORKSPACE );
}
// EXPORTS //
module.exports = parseUint64;
|