Repository navigation
interpreter: convert strings to numbers by their leading numeric prefix - #142
jayhemnani9910 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
GNU awk testsuite comparison: |
| /// are skipped and the longest prefix that is a decimal number counts, so | ||
| /// `" 12 "` is 12, `"3x"` is 3 and `"x"` is 0. Like gawk, `inf` and `nan` | ||
| /// are only recognized with a sign and nothing else but blanks around them. | ||
| fn str_to_num(s: &[u8]) -> f64 { |
There was a problem hiding this comment.
strtonum in builtins.rs still falls back to s.parse(), so strtonum("3x") gives 0 (gawk gives 3)
could it use this function too?
There was a problem hiding this comment.
Yes, in ae2eb12. strtonum now falls back to str_to_num, and the hex and octal branches also read just the leading digits, as gawk does: strtonum("0x1Fz") is 31, strtonum("017x") is 15, and an 8, 9, . or exponent after the leading 0 makes it decimal ("0778" is 778, "01.5" is 1.5). Like gawk, the prefix only counts at the very start, so " 0x10" and "+0x10" are 0. The new test is checked against gawk 5.3.2.
| .ok() | ||
| .and_then(|s| s.parse().ok()) | ||
| .unwrap_or(0.), | ||
| Self::String(s) => str_to_num(s), |
There was a problem hiding this comment.
what about Str and StrNum? they still hit _ => 0.
There was a problem hiding this comment.
Added in ae2eb12: Str goes through str_to_num, and StrNum returns its stored num. Nothing builds a StrNum yet, so only Str has a test.
|
sorry, needs to be rebased! |
952c091 to
9ee59e1
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
GNU awk testsuite comparison: |
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
GNU awk testsuite comparison: |
ae2eb12 to
52b26e2
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
GNU awk testsuite comparison: |
|
I don't think it's necessary to re-implement the whole numeric parser, but let me take a look at the GNU docs and then review the code. |
52b26e2 to
d4a2c32
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
GNU awk testsuite comparison: |
A string turns into a number only when the whole string is a valid Rust float, so blanks around a number or anything after it give 0. gawk 5.3.2 vs main:
to_numfor a string now skips leading blanks and reads the longest decimal prefix (sign, digits,., exponent only when digits follow it), like strtod without hex.infandnancount only with a sign and nothing but blanks after them, which is what gawk does; before, Rust's parser turned a bareinf,nanorinfinityinto a number.New tests
string_to_number_uses_leading_numeric_prefixandfields_with_blanks_around_numbers_sumfail without the change.cargo test --workspace: 212 passed (210 on main plus the two), clippy and fmt clean. 38 strings (blanks incl.\v\f\r,5.,.5,1e,1e+,1.2.3,0x1A,+INF,+infinity,1e400, ...) give the same value as gawk; inf and nan still print differently (infvs+inf), which is output formatting, not this change.