expand: Support integer bases in arithmetic#1328
Conversation
|
What shell syntax does this follow? I don't see any mention of |
|
Hello @mdvan. The
... which I guess looks like this: bash-3.2$ echo $((0xff+1))
256
bash-3.2$ echo $((0Xff+1))
256
bash-3.2$ echo $((0377+1))
256
bash-3.2$ echo $((10#1+1))
2
bash-3.2$ echo $((16#ff+1))
256
bash-3.2$ echo $((2#11111111+1))
256Happy to update the commit to match that syntax. |
|
Yep, mimicking bash is good. Just add some tests near existing arithmetic ones. |
e167cd2 to
d96931f
Compare
|
@mvdan, I updated the commit to use bash's syntax for integer base handling. I am not a huge fan of the code I wrote, but I think it's a good starting point. I'm not sure if the test format is what you are looking for. But, the tests pass and the bash examples I posted earlier work now. |
mvdan
left a comment
There was a problem hiding this comment.
Thanks! SGTM, just two thoughts.
| // letters may be used interchangeably to represent numbers between | ||
| // 10 and 35. | ||
| // | ||
| // - https://www.man7.org/linux/man-pages/man1/bash.1.html |
There was a problem hiding this comment.
no need to quote the whole section of the bash man page; simply mention that this mimics the documented bash behavior.
| } | ||
| } | ||
|
|
||
| func TestArith(t *testing.T) { |
There was a problem hiding this comment.
no need to add an entirely new test; you can add test cases in interp/interp_test.go after the existing arithmetic ones with echo $((...)). An advantage of that route is that those get run against bash as well, so we catch any discrepancy.
There was a problem hiding this comment.
I moved the tests around as requested.
|
Also, please add "Fixes #339" to the commit message. |
e7af52d to
7e66d77
Compare
|
Hey @mvdan, I have updated the commit with the requested changes and ran the |
|
Hm, the tests failed on 32-bit. |
|
Awesome. I can probably make the test pass by just using a smaller number... It does make me wonder about sh's handling of integer bit width and what the "correct" behavior would look like though. |
|
All the arithmetic code should be on 64-bit integers; there might be a spot that isn't the case. This might be a latent bug and not introduced by your fix, but perhaps just surfaced by your test. |
|
Ah, I see that |
7e66d77 to
6e66536
Compare
The existing arithmetic logic is hard coded to always treat strings as base 10 integers. This commit updates that logic to support what bash supports based on its manual page. [1] References 1. https://www.man7.org/linux/man-pages/man1/bash.1.html Fixes mvdan#339.
6e66536 to
e124d5f
Compare
|
I commented out that test and added another with a smaller number. |
The existing arithmetic logic is hard coded to always treat strings as base 10 integers. This commit updates that logic to support what bash supports based on its manual page. [1]
References