diff --git a/lib/node_modules/@stdlib/number/uint64/parse/README.md b/lib/node_modules/@stdlib/number/uint64/parse/README.md new file mode 100644 index 000000000000..5c178777b6ef --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/README.md @@ -0,0 +1,132 @@ + + +# parseUint64 + +> Parse a string as an unsigned 64-bit integer. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var parseUint64 = require( '@stdlib/number/uint64/parse' ); +``` + +#### parseUint64( str ) + +Parses a string as an unsigned 64-bit integer. + +```javascript +var str = '1234'; +var x = parseUint64( str ); +// returns + +str = '0x1234567890abcdef'; +x = parseUint64( str ); +// returns +``` + +
+ + + +* * * + + + +
+ +## Notes + +- An unsigned 64-bit integer has a range of \[`0`, `2^64-1`\]. +- If the provided string represents a value greater than `2^64-1` it is truncated to 64 least significant bits. + +
+ + + +* * * + + + +
+ +## Examples + + + +```javascript +var parseUint64 = require( '@stdlib/number/uint64/parse' ); + +var x = parseUint64( '1234' ); + +console.log( 'type: %s', typeof x ); +// => 'type: object' + +console.log( 'str: %s', x ); +// => 'str: 1234' + +console.log( 'JSON: %s', JSON.stringify( x ) ); +// => 'JSON: {"type":"Uint64","words":[0,1234]}' +``` + +
+ + + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/number/uint64/parse/benchmark/benchmark.js b/lib/node_modules/@stdlib/number/uint64/parse/benchmark/benchmark.js new file mode 100644 index 000000000000..bdec8fb91917 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var pkg = require( './../package.json' ).name; +var parseUint64 = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var radix; + var N; + var M; + var a; + var i; + var j; + var x; + + N = 100; + x = discreteUniform( N, 0, MAX_SAFE_INTEGER ); + values = []; + for ( radix = 2; radix <= 36; radix++ ) { + for ( i = 0; i < N; i++ ) { + values.push( x[i].toString( radix ) ); + } + } + M = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % M; + radix = 2 + floor( j / N ); + a = parseUint64( values[ j ], radix ); + if ( typeof a !== 'object' ) { + b.fail( 'should return a Uint64 instance' ); + } + } + b.toc(); + if ( !( a instanceof Uint64 ) ) { + b.fail( 'should return a Uint64 instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/number/uint64/parse/docs/repl.txt b/lib/node_modules/@stdlib/number/uint64/parse/docs/repl.txt new file mode 100644 index 000000000000..13dd7c2ee3bf --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( str ) + Parses a string as an unsigned 64-bit integer. + + Parameters + ---------- + str: string + String representation of a nonnegative integer. + + Returns + ------- + v: Uint64 + Unsigned 64-bit integer. + + Examples + -------- + > var x = {{alias}}( '1234' ) + + > x.toString() + '1234' + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/number/uint64/parse/docs/types/index.d.ts b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/index.d.ts new file mode 100644 index 000000000000..a045c72f4679 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +import Uint64 = require( '@stdlib/number/uint64/ctor' ); + + +/** +* Parses a string as an unsigned 64-bit integer. +* +* @param str - string representation of a nonnegative integer +* @throws must provide a string encoding a nonnegative integer +* @returns unsigned 64-bit integer +* +* @example +* var v = parseUint64( '1234' ); +* // returns [ 1234n ] +* +* v = parseUint64( '18446744073709551615' ); +* // returns [ 18446744073709551615n ] +* +* v = parseUint64( '0xffffffffffffffff' ); +* // returns [ 18446744073709551615n ] +* +* v = parseUint64( '3w5e11264sgsf', 36 ); +* // returns [ 18446744073709551615n ] +*/ +declare function parseUint64( str: string ): Uint64; + + +// EXPORTS // + +export = parseUint64; diff --git a/lib/node_modules/@stdlib/number/uint64/parse/docs/types/test.ts b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/test.ts new file mode 100644 index 000000000000..284616de7e87 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/test.ts @@ -0,0 +1,48 @@ +/* +* @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. +*/ + +/* eslint-disable @typescript-eslint/no-unused-expressions */ + +import parseUint64 = require( './index' ); + + +// TESTS // + +// The function returns an unsigned 64-bit integer... +{ + parseUint64( '1234' ); // $ExpectType Uint64 + parseUint64( '0xabcd' ); // $ExpectType Uint64 + parseUint64( '0b1010' ); // $ExpectType Uint64 + parseUint64( '0o1234' ); // $ExpectType Uint64 +} + +// The compiler throws an error if the function is provided an argument which is not a string... +{ + parseUint64( true ); // $ExpectError + parseUint64( false ); // $ExpectError + parseUint64( null ); // $ExpectError + parseUint64( 123 ); // $ExpectError + parseUint64( {} ); // $ExpectError + parseUint64( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + parseUint64(); // $ExpectError + parseUint64( '5', '3' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/number/uint64/parse/examples/index.js b/lib/node_modules/@stdlib/number/uint64/parse/examples/index.js new file mode 100644 index 000000000000..9646d69f7388 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/examples/index.js @@ -0,0 +1,32 @@ +/** +* @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'; + +var parseUint64 = require( './../lib' ); + +var x = parseUint64( '1234' ); + +console.log( 'type: %s', typeof x ); +// => 'type: object' + +console.log( 'str: %s', x ); +// => 'str: 1234' + +console.log( 'JSON: %s', JSON.stringify( x ) ); +// => 'JSON: {"type":"Uint64","words":[0,1234]}' diff --git a/lib/node_modules/@stdlib/number/uint64/parse/lib/index.js b/lib/node_modules/@stdlib/number/uint64/parse/lib/index.js new file mode 100644 index 000000000000..d9adee21df44 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/lib/index.js @@ -0,0 +1,49 @@ +/** +* @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'; + +/** +* Parse a string as an unsigned 64-bit integer. +* +* @module @stdlib/number/uint64/parse +* +* @example +* var parseUint64 = require( '@stdlib/number/uint64/parse' ); +* +* var v = parseUint64( '1234' ); +* // returns [ 1234n ] +* +* v = parseUint64( '18446744073709551615' ); +* // returns [ 18446744073709551615n ] +* +* v = parseUint64( '0xffffffffffffffff' ); +* // returns [ 18446744073709551615n ] +* +* v = parseUint64( '3w5e11264sgsf', 36 ); +* // returns [ 18446744073709551615n ] +*/ + +// MAIN // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/number/uint64/parse/lib/main.js b/lib/node_modules/@stdlib/number/uint64/parse/lib/main.js new file mode 100644 index 000000000000..4ddcb2e82984 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/lib/main.js @@ -0,0 +1,142 @@ +/** +* @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 [ 1234n ] +* +* v = parseUint64( '18446744073709551615' ); +* // returns [ 18446744073709551615n ] +* +* v = parseUint64( '0xffffffffffffffff' ); +* // returns [ 18446744073709551615n ] +* +* v = parseUint64( '3w5e11264sgsf', 36 ); +* // returns [ 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; diff --git a/lib/node_modules/@stdlib/number/uint64/parse/lib/parse.js b/lib/node_modules/@stdlib/number/uint64/parse/lib/parse.js new file mode 100644 index 000000000000..61b80dbb94d9 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/lib/parse.js @@ -0,0 +1,187 @@ +/** +* @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 = [ + // , , // 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; diff --git a/lib/node_modules/@stdlib/number/uint64/parse/package.json b/lib/node_modules/@stdlib/number/uint64/parse/package.json new file mode 100644 index 000000000000..42586090cf0e --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/number/uint64/parse", + "version": "0.0.0", + "description": "Parse a string as an unsigned 64-bit integer.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "uint64", + "unsigned", + "64-bit", + "integer", + "int" + ] +} diff --git a/lib/node_modules/@stdlib/number/uint64/parse/test/test.js b/lib/node_modules/@stdlib/number/uint64/parse/test/test.js new file mode 100644 index 000000000000..e001fafa7f11 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/test/test.js @@ -0,0 +1,387 @@ +/** +* @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 tape = require( 'tape' ); +var trim = require( '@stdlib/string/base/trim' ); +var slice = require( '@stdlib/string/base/slice' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var parseUint64 = require( './../lib' ); + + +// VARIABLES // + +var TWO_32 = 0x100000000; // 2^32 + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof parseUint64, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an unsigned 64-bit integer', function test( t ) { + var x = parseUint64( '1234' ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a string', function test( t ) { + var values; + var i; + + values = [ + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var x = parseUint64( value ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function throws an error if not provided a string encoding a nonnegative integer', function test( t ) { + var values; + var i; + + values = [ + '', + '-1', + '0.5', + '++', + '--', + '++1', + '--1', + 'foo', + 'foo1234', + '1234abc', + '0xyab', + '0b123', + '0o789', + '0x', + '0b', + '0o' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), Error, 'throws an error when provided '+values[i] ); + } + + t.end(); + + function badValue( value ) { + return function badValue() { + var x = parseUint64( value ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function correctly handles leading and trailing spaces', function test( t ) { + var values; + var i; + var x; + var y; + + values = [ + ' 1234', + ' 1234', + '1234 ', + '1234 ', + ' 1234 ', + ' 1234 ', + ' 0x1234 ', + ' 0b1010 ', + ' 0o1234 ' + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( values[i] ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + y = parseUint64( trim( values[i] ) ); + t.strictEqual( x.toString(), y.toString(), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function correctly handles optional plus sign', function test( t ) { + var values; + var i; + var x; + var y; + + values = [ + '+1234', + '+0x1234', + '+0b1010', + '+0o1234' + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( values[i] ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + y = parseUint64( slice( values[i], 1 ) ); + t.strictEqual( x.toString(), y.toString(), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function correctly parses integers in decimal notation', function test( t ) { + var values; + var i; + var x; + var y; + + values = [ + 0, + 1, + 123456, + 123456789, + 1234567890, + 987654321, + 9876543210, + 1122334455667788, + 8877665544332211, + TWO_32 - 1, + TWO_32, + TWO_32 + 1, + TWO_32 * 2, + MAX_SAFE_INTEGER - 1, + MAX_SAFE_INTEGER + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( values[i].toString( 10 ) ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + y = new Uint64( values[i] ); + t.strictEqual( x.toString(), y.toString(), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function correctly parses integers in hexadecimal notation', function test( t ) { + var values; + var i; + var x; + var y; + + values = [ + 0, + 1, + 123456, + 123456789, + 1234567890, + 987654321, + 9876543210, + 1122334455667788, + 8877665544332211, + TWO_32 - 1, + TWO_32, + TWO_32 + 1, + TWO_32 * 2, + MAX_SAFE_INTEGER - 1, + MAX_SAFE_INTEGER + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( '0x'+values[i].toString( 16 ) ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + y = new Uint64( values[i] ); + t.strictEqual( x.toString(), y.toString(), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function correctly parses integers in binary notation', function test( t ) { + var values; + var i; + var x; + var y; + + values = [ + 0, + 1, + 123456, + 123456789, + 1234567890, + 987654321, + 9876543210, + 1122334455667788, + 8877665544332211, + TWO_32 - 1, + TWO_32, + TWO_32 + 1, + TWO_32 * 2, + MAX_SAFE_INTEGER - 1, + MAX_SAFE_INTEGER + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( '0b'+values[i].toString( 2 ) ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + y = new Uint64( values[i] ); + t.strictEqual( x.toString(), y.toString(), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function correctly parses integers in octal notation', function test( t ) { + var values; + var i; + var x; + var y; + + values = [ + 0, + 1, + 123456, + 123456789, + 1234567890, + 987654321, + 9876543210, + 1122334455667788, + 8877665544332211, + TWO_32 - 1, + TWO_32, + TWO_32 + 1, + TWO_32 * 2, + MAX_SAFE_INTEGER - 1, + MAX_SAFE_INTEGER + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( '0o'+values[i].toString( 8 ) ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + y = new Uint64( values[i] ); + t.strictEqual( x.toString(), y.toString(), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function supports lowercase alphabetic digits and base prefix', function test( t ) { + var values; + var i; + var x; + + values = [ + '0x1234', + '0xabcdef', + '0b1010', + '0o1234' + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( values[i] ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function supports uppercase alphabetic digits and base prefix', function test( t ) { + var values; + var i; + var x; + + values = [ + '0X1234', + '0XABCDEF', + '0B1010', + '0O1234' + ]; + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( values[i] ); + t.strictEqual( x instanceof Uint64, true, 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function correctly handles integer values upto 2^64-1', function test( t ) { + var values; + var i; + var x; + var y; + + values = [ + // 2^64 - 1 + '18446744073709551615', + '0xffffffffffffffff', + '0b1111111111111111111111111111111111111111111111111111111111111111', + '0o1777777777777777777777' + ]; + + y = Uint64.of( UINT32_MAX, UINT32_MAX ); + + for ( i = 0; i < values.length; i++ ) { + x = parseUint64( values[i] ); + t.strictEqual( x.toString(), y.toString(), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'the function throws an error for integer values greater than 2^64-1', function test( t ) { + var values; + var i; + + values = [ + // 2^64 + '18446744073709551616', + '0x10000000000000000', + '0b10000000000000000000000000000000000000000000000000000000000000000', + '0o2000000000000000000000' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), Error, 'throws an error when provided '+values[i] ); + } + + t.end(); + + function badValue( value ) { + return function badValue() { + var x = parseUint64( value ); // eslint-disable-line no-unused-vars + }; + } +}); + +// FIXME: Add additional tests