Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions lib/node_modules/@stdlib/number/uint64/base/string2words/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<!--

@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.

-->

# string2words

> Parse a string representation of a 64-bit unsigned integer into high and low 32-bit words.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var string2words = require( '@stdlib/number/uint64/base/string2words' );
```

#### string2words( str, radix )

Parses a string representation of a 64-bit unsigned integer into high and low 32-bit words.

```javascript
var w = string2words( '1234', 10 );
// returns [ 0, 1234 ]
```

The function returns an array containing two elements: a higher order word and a lower order word. The lower order word contains the less significant bits, while the higher order word contains the more significant bits.

#### string2words.assign( str, radix, out, stride, offset )

Parses a string representation of a 64-bit unsigned integer into high and low 32-bit words and assigns results to a provided output array.

```javascript
var Uint32Array = require( '@stdlib/array/uint32' );

var out = new Uint32Array( 2 );
// returns <Uint32Array>[ 0, 0 ]

var w = string2words.assign( 'ffffffffffffffff', 16, out, 1, 0 );
// returns <Uint32Array>[ 4294967295, 4294967295 ]

var bool = ( w === out );
// returns true
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- The input string must be a valid representation of an unsigned 64-bit integer in the specified radix, containing no whitespace, sign symbols, or leading zeros.
- If the provided string represents a value greater than `2^64-1` or the radix is invalid, then `0` is assigned to both high and low words.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

```javascript
var string2words = require( '@stdlib/number/uint64/base/string2words' );

var w = string2words( '1234', 10 );
console.log( w );
// => [ 0, 1234 ]

w = string2words( 'abcd', 16 );
console.log( w );
// => [ 0, 43981 ]

w = string2words( '18446744073709551615', 10 );
console.log( w );
// => [ 4294967295, 4294967295 ]

w = string2words( '3w5e11264sgsf', 36 );
console.log( w );
// => [ 4294967295, 4294967295 ]
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* @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 Uint32Array = require( '@stdlib/array/uint32' );
var isArray = require( '@stdlib/assert/is-array' );
var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
var Uint64 = require( '@stdlib/number/uint64/ctor' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var string2words = require( './../lib' );


// VARIABLES //

var options = {
'dtype': 'uint32'
};


// MAIN //

bench( pkg, function benchmark( b ) {
var radices;
var values;
var rad;
var N;
var a;
var i;
var w;
var x;

N = 100;
x = discreteUniform( N, 0, UINT32_MAX, options );
values = [];
radices = [];
for ( i = 0; i < N; i++ ) {
a = Uint64.of( x[ i ], x[ (i+1)%N ] );
for ( rad = 2; rad <= 36; rad++ ) {
values.push( a.toString( rad ) );
radices.push( rad );
}
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
w = string2words( values[ i % N ], radices[ i % N ] );
if ( typeof w !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArray( w ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:assign', pkg ), function benchmark( b ) {
var radices;
var values;
var out;
var rad;
var N;
var a;
var i;
var w;
var x;

N = 100;
x = discreteUniform( N, 0, UINT32_MAX, options );
values = [];
radices = [];
for ( i = 0; i < N; i++ ) {
a = Uint64.of( x[ i ], x[ (i+1)%N ] );
for ( rad = 2; rad <= 36; rad++ ) {
values.push( a.toString( rad ) );
radices.push( rad );
}
}

out = new Uint32Array( 2 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
w = string2words.assign( values[ i % N ], radices[ i % N ], out, 1, 0 );
if ( typeof w !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( w !== out ) {
b.fail( 'should return the output array' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

{{alias}}( str, radix )
Parses a string representation of a 64-bit unsigned integer into high and
low 32-bit words.

The function returns an array with two elements: a higher order word and a
lower order word, respectively. The lower order word contains the less
significant bits, while the higher order word contains the more significant
bits.

Parameters
----------
str: string
String representation of a 64-bit unsigned integer.

radix: integer
Radix (base) to use for string conversion (2-36).

Returns
-------
out: Array<integer>
High and low words as 32-bit unsigned integers.

Examples
--------
> var w = {{alias}}( '1234', 10 )
[ 0, 1234 ]
> w = {{alias}}( 'abcd', 16 )
[ 0, 43981 ]
> w = {{alias}}( '3w5e11264sgsf', 36 )
[ 4294967295, 4294967295 ]


{{alias}}.assign( str, radix, out, stride, offset )
Parses a string representation of a 64-bit unsigned integer into high and
low 32-bit words and assigns results to a provided output array.

Parameters
----------
str: string
String representation of a 64-bit unsigned integer.

radix: integer
Radix (base) to use for string conversion (2-36).

out: Array|TypedArray|Object
Output array.

stride: integer
Output array stride.

offset: integer
Output array index offset.

Returns
-------
out: Array|TypedArray|Object
Output array.

Examples
--------
> var out = new {{alias:@stdlib/array/uint32}}( 2 );
> var w = {{alias}}.assign( 'ffffffffffffffff', 16, out, 1, 0 )
<Uint32Array>[ 4294967295, 4294967295 ]
> var bool = ( w === out )
true

See Also
--------
Loading