Skip to content

Handle infinity and NaN for string.format()#172

Merged
zombiezen merged 2 commits into256lights:mainfrom
HigherOrderLogic:fix/string-format
Nov 17, 2025
Merged

Handle infinity and NaN for string.format()#172
zombiezen merged 2 commits into256lights:mainfrom
HigherOrderLogic:fix/string-format

Conversation

@HigherOrderLogic
Copy link
Contributor

Fixes (partially) #78.

Copy link
Member

@zombiezen zombiezen left a comment

Choose a reason for hiding this comment

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

Generally LGTM, thank you! Would you be willing to add some unit tests for this behavior in stringlib_test.go?

spec = spec[:len(spec)-1] + string(c+('X'-'A'))

isUpper := (c >= 'A' && c <= 'Z')
if math.IsNaN(n) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: mind changing this if/else-if into a switch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would I turn this if else into a switch? From what I can see, switch only check one value at a time.

This is my first time writing Go code, so any advice is welcome.

Copy link
Member

Choose a reason for hiding this comment

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

Example here: https://go.dev/tour/flowcontrol/11

Switch without a condition is the same as switch true.

So each of the if conditions would become a case in a switch.

} else {
s = "inf"
}
if math.IsInf(n, -1) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's make this a little more direct: we already know that the number is one of the infinities:

Suggested change
if math.IsInf(n, -1) {
if n < 0 {

@HigherOrderLogic HigherOrderLogic force-pushed the fix/string-format branch 2 times, most recently from 28f0305 to fdb25cc Compare November 11, 2025 05:31
@HigherOrderLogic
Copy link
Contributor Author

add some unit tests for this behavior in stringlib_test.go?

I have updated according to the review, and there's only this left.

The issue is I dont find any test case for stringFormat in stringlib_test.go, and while there is test for string.format in the testsuite folder, there doesnt seems to be one for formatting number. So the question is, where should I place the new test cases?

Copy link
Member

@zombiezen zombiezen left a comment

Choose a reason for hiding this comment

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

Apologies, I had incorrectly remembered there being unit tests for string.format. I went ahead and added a table-driven test to check some other functionality as well as this change. LGTM, thank you!

@zombiezen zombiezen merged commit bd577c5 into 256lights:main Nov 17, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants