Skip to content

Conversation

harishbohara11
Copy link
Contributor

fixes #274

@coveralls
Copy link
Collaborator

coveralls commented Feb 11, 2023

Coverage Status

Coverage: 94.843% (+0.01%) from 94.828% when pulling 7e55c50 on harishbohara11:digit_separator into 1e30c27 on gavv:master.

@gavv
Copy link
Owner

gavv commented Feb 13, 2023

Thanks for PR! Will review in upcoming days.

@gavv gavv added the ready for review Pull request can be reviewed label Feb 13, 2023
Copy link
Owner

@gavv gavv left a comment

Choose a reason for hiding this comment

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

Thanks for pr. Here are some comments

formatter.go Outdated
Comment on lines 459 to 516
if intg := extractInt(value); intg != nil {
return f.addDigitGrouping(strconv.Itoa(*intg))
}
Copy link
Owner

Choose a reason for hiding this comment

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

Here we will catch only int, but we should handle other integers types too: uint, int8, int16, etc.

The easiest way to do it is:

  • check if isNumber returns true; it means that it's an integer, because floats are already handled above
  • if yes, format it using fmt.Sprintf with %v - this should be equivalent to how we format integers below
  • apply digit groupping to that string, and return it

formatter.go Outdated
@@ -472,6 +497,68 @@ func (f *DefaultFormatter) formatFloatValue(value float64, bits int) string {
}
}

func (f *DefaultFormatter) addDigitGrouping(numStr string) string {
Copy link
Owner

Choose a reason for hiding this comment

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

I suggest to rename this func to reformatNumber, and groupDigitsInString to insertDigitSeparator.

The first name suggests that we should call it when we're formatting any number, float or int. The caller doesn't bother what exactly it will do.

The second name correlates with the name of the DigitSeparator option.

formatter.go Outdated
numStr = numStr[1:]
}

parts := strings.SplitN(numStr, ".", 2)
Copy link
Owner

Choose a reason for hiding this comment

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

To make this code easier to follow, I suggest to extract a function, say, parseNumber, that returns all parts of the number: integer, fractional, and exponent.

Here we can call it, and apply groupping to integer and fractional parts.

Then we can compose number from parts.

formatter.go Outdated
parts = strings.SplitN(parts[1], "e", 2)
// using Reverse(parts[0]) because digit grouping in
// fractional part is required from Most Significant Bit
fracPart = "." + Reverse(f.groupDigitsInString(Reverse(parts[0])))
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of using Reverse func, I suggest to pass direction flag (-1 or +1) to groupDigitsInString. I think the code will be easier to follow on the caller side

@@ -587,6 +587,120 @@ func TestFormatter_FloatFormat(t *testing.T) {
}
}

func TestFormatter_DigitSeparator(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

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

We also need to cover a few more cases:

  • big integer part, small decimal part, non scientific
  • big integer part, small decimal part, scientific
  • big integer part, big decimal part, non scientific
  • big integer part, big decimal part, scientific

By small decimal part, I mean 1 to 3 digits, by big - more than 3 digits.

}
}

func TestFormatter_DigitSeparatorType(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

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

We also need a separate test that check how separator is applied to different fliatand integer types (float32, float64, int, uint)

@gavv gavv added needs revision Pull request should be revised by its author and removed ready for review Pull request can be reviewed labels Feb 18, 2023
@github-actions
Copy link

☔ The latest upstream change (presumably these) made this pull request unmergeable. Please resolve the merge conflicts.

@github-actions github-actions bot added the needs rebase Pull request has conflicts and should be rebased label Mar 10, 2023
@gavv gavv force-pushed the digit_separator branch from 68658d7 to 7e55c50 Compare March 22, 2023 13:02
@gavv
Copy link
Owner

gavv commented Mar 22, 2023

I've rebased this branch on master and pushed follow-up commit: 7e55c50

The commit addresses issues I mentioned above. I also switched from manual parsing to regex.

Thanks for PR!

@gavv gavv merged commit 836480a into gavv:master Mar 22, 2023
@gavv gavv removed needs revision Pull request should be revised by its author needs rebase Pull request has conflicts and should be rebased labels Mar 22, 2023
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.

Implement DefaultFormatter.DigitSeparator
3 participants