feat: add number/uint64/base/string2words#13356
Conversation
---
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
---
Coverage Report
The above coverage report was generated for the changes in this PR. |
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>
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>
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>
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>
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>
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>
|
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. |
---
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
---
|
@kgryte Added additional tests for 100% coverage. |
| out[ offset ] = 0; | ||
| out[ offset + stride ] = 0; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
So that's my pitch, pushing all the validation burden up to the call site and only keep the too big check.
There was a problem hiding this comment.
Wouldn't you need to check for all variations of 0? 0, 00, 000, etc?
There was a problem hiding this comment.
No because the string will be stripped of extra leading zeros before passing to string2words.assign.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 );
}
Resolves none.
Description
This pull request:
Related Issues
No.
Questions
assignfunction or be removed for performance?Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
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