Skip to content

feat: add number/uint64/base/string2words#13356

Open
impawstarlight wants to merge 16 commits into
stdlib-js:developfrom
impawstarlight:feature/uint64-base-string2words
Open

feat: add number/uint64/base/string2words#13356
impawstarlight wants to merge 16 commits into
stdlib-js:developfrom
impawstarlight:feature/uint64-base-string2words

Conversation

@impawstarlight

@impawstarlight impawstarlight commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Resolves none.

Description

What is the purpose of this pull request?

This pull request:

  • Adds a new package for parsing numeric strings into high and low words of an 64-bit unsigned integer.

Related Issues

Does this pull request have any related issues?

No.

Questions

Any questions for reviewers of this pull request?

  • Should the validation blocks for invalid radix and too big number remain in the assign function or be removed for performance?
  • Any refinements necessary in the notes section of the readme file?

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

I used Gemini and GitHub Copilot to generate & refine the JSDoc comment for the CHUNKMAP variable, conduct overall research and code review.


@stdlib-js/reviewers

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: passed
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: passed
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: passed
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: passed
  - task: lint_license_headers
    status: passed
---
@impawstarlight impawstarlight requested a review from a team July 7, 2026 08:15
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Jul 7, 2026
@impawstarlight impawstarlight added the Feature Issue or pull request for adding a new feature. label Jul 7, 2026
@stdlib-bot

stdlib-bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
number/uint64/base/string2words $\\color{green}319/319$
$\\color{green}+3.57\\%$
$\\color{green}20/20$
$\\color{green}+21.43\\%$
$\\color{green}3/3$
$\\color{green}+0.00\\%$
$\\color{green}319/319$
$\\color{green}+3.57\\%$

The above coverage report was generated for the changes in this PR.

Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/main.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/test/test.assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/test/test.main.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/README.md Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
kgryte
kgryte previously approved these changes Jul 7, 2026

@kgryte kgryte left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread lib/node_modules/@stdlib/number/uint64/base/string2words/lib/assign.js Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
kgryte
kgryte previously approved these changes Jul 7, 2026
@kgryte kgryte removed the Needs Review A pull request which needs code review. label Jul 7, 2026
@kgryte

kgryte commented Jul 7, 2026

Copy link
Copy Markdown
Member

Actually, before we merge, @impawstarlight would you mind adding additional tests so that we can achieve 100% coverage? See https://coverage.stdlib.io/pr-13356/number/uint64/base/string2words/index.html.

@kgryte kgryte added the Needs Changes Pull request which needs changes before being merged. label Jul 7, 2026
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@impawstarlight

Copy link
Copy Markdown
Contributor Author

@kgryte Added additional tests for 100% coverage.

@impawstarlight impawstarlight requested a review from kgryte July 7, 2026 11:09
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Jul 7, 2026
@kgryte kgryte removed Needs Changes Pull request which needs changes before being merged. Needs Review A pull request which needs code review. labels Jul 7, 2026
Comment on lines +175 to +176
out[ offset ] = 0;
out[ offset + stride ] = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay...I am backtracking here. I am thinking we should raise an exception here. The reason being that it is impossible currently to disambiguate the string '0' from an error state here.

In other scenarios, we can return sentinel values (e.g., NaN, -1, etc), but here we cannot. As such, I am of opinion that it would be better to throw.

We do raise exceptions in "base" implementations, depending on the context. Typically, when there is the possibility of a runtime error, rather than, let's call it, a static error (e.g., providing a string instead of a number; something which would be caught by a static compiler, if JS had one).

That applies here and when str is too big for uint64. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing we can do to distinguish '0' from a too big str is to check in the call site whether the passed string was actually '0' or not. For example in uint64/parse we can do this:

function parseUint64( str, radix ) {
    // Check str type and throw TypeErrors
    ...

    // Sanitize and validate str: trim whitespaces, sign symbol, leading zeros, detect radix prefix, check invalid chars and throw,
    ...

    // Check radix type and range and throw Type/RangeError
    if ( radix < 2 || radix > 36 )
    ...

    // Call string2words, str will be trimmed of whitespace, sign symbol, leading zeros and contain no invalid chars at this point
    string2words.assign( str, radix, out, 1, 0 );

    // Check for too big value / overflow and throw RangeError
    if ( out[ 0 ] === 0 && out[ 1 ] === 0 && str !== '0' )
    ...
   
    ...
}

So like, the only error that we cannot detect beforehand from the call site is the too big value case, and we can differentiate it after the call with the combined str !== '0' check.

And in all other cases we can detect the error before the call. That's why I think we can remove the radix check from string2words.assign altogether.

And about runtime errors due to an invalid radix (0, 1, or 37), there is no crash in those cases and just gives wrong output ([ 0, 0 ]) even without the radix check at the top.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that's my pitch, pushing all the validation burden up to the call site and only keep the too big check.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't you need to check for all variations of 0? 0, 00, 000, etc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because the string will be stripped of extra leading zeros before passing to string2words.assign.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the contract here is this: the string2words package will only deal with a string that is free of any whitespace, sign symbols or leading zeros. And it's the caller's responsibility to ensure that.

@impawstarlight impawstarlight Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a detailed sample:

function parseUint64( str, radix ) {
	var rad;
	var out;
	var s;
	var i;

	if ( !isString( str ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
	}

	s = trim( str );
	if ( s[0] === '-' ) {
		throw new Error( format( 'invalid argument. First argument must be a string encoding a nonnegative integer. Value: `%s`.', str ) ); // Maybe use RangeError here?
	}

	if ( s[0] === '+' ) {
		s = slice( s, 1 );
	}

	if ( arguments.length < 2 ) {
		if ( RE_BIN.test( s ) ) {
			rad = 2;
		} else if ( RE_OCT.test( s ) ) {
			rad = 8;
		} else if ( RE_HEX.test( s ) ) {
			rad = 16;
		} else {
			rad = 10;
		}

		if ( rad !== 10 ) {
			s = slice( s, 2 ); // Remove the radix prefix
		}
	} else if ( !isInteger( radix ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', radix ) );
	} else if ( isBetween( radix, 2, 36 ) ) {
		rad = radix;
	} else {
		throw new RangeError( format( 'invalid argument. Second argument must be an integer on the interval [2, 36]. Value: `%s`.', radix ) );
	}

	// Handles cases like '', ' ', '0x'
	if ( isEmptyString( s ) ) {
		throw new Error( format( 'invalid argument. First argument must be a string encoding a nonnegative integer. Value: `%s`.', str ) );
	}

	// Trim leading zeros
	i = 0;
	while ( ( i < s.length-1 ) && ( s[i] === '0' ) ) {
		i += 1;
	}
	s = slice( s, i );

	// Validate digits with respect to the radix
	if ( regexp( rad ).test( s ) ) { // If contains any invalid digit
		throw new Error( format( 'invalid argument. First argument must be a string encoding a nonnegative integer. Value: `%s`.', str ) );
	}

	out = assign( s, rad, [ 0, 0 ], 1, 0 );
	if ( out[ 0 ] === 0 && out[ 1 ] === 0 && s !== '0' ) {
		throw new RangeError( format( 'invalid argument. First argument must be a string encoding a nonnegative integer smaller than 2^64. Value: `%s`.', str ) );
	}

	return Uint64.from( out );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature Issue or pull request for adding a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants