Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copy-pasta typo in regex utility for int parsing #363

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gwynne
Copy link
Contributor

@gwynne gwynne commented Jan 1, 2024

Title kind've says it all 馃槄

@compnerd
Copy link
Collaborator

compnerd commented Jan 1, 2024

@swift-ci please test

@@ -58,7 +58,7 @@ extension IntegerParseStrategy: ParseStrategy {
if let value = parser.parseAsInt(substr, upperBound: &upperBound) {
let upperBoundInSubstr = String.Index(utf16Offset: upperBound, in: substr)
return (upperBoundInSubstr, Format.FormatInput(value))
} else if let value = parser.parseAsInt(substr, upperBound: &upperBound) {
} else if let value = parser.parseAsDouble(substr, upperBound: &upperBound) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this happen first? Otherwise, it will consume until . as an integer right?

Copy link
Contributor

Choose a reason for hiding this comment

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

In the implementation at the above function (line 33) we also parse it as Int first, then try Double second. I believe the idea was that since it's a IntegerParseStrategy we'd try to parse it as Int if possible. From this perspective I think this fix is alright. It fixes the obvious copy pasta error, and the behavior is at least consistent with this other function.

That being said, in my playground it seems that both parseAsInt and parseAsDouble consumes the entire floating-point-like string. parseAsInt doesn't just stop at . either; rather, it just truncates the floating point string into an Int. That makes me wonder if that'd always succeed, and that this else if let value = parser.parseAsDouble clause actually never gets hit.

@gwynne did you see any test failures on your end that prompted this fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Truthfully, I didn't even run the code in question; I was just looking at it in terms of the obvious copy-paste error 馃槄. I agree that it seems redundant to have both code paths in practice.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with merging it as is and revisit if we can remove this altogether separately.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking again at this, I think we should just remove this code path. This has been in the wild since it was introduced and there hasn't been casualties (yet). We'll just come back to add back the logic to parse as Double if we ever come across a valid case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I concur, updating the PR 馃憤

itingliu
itingliu previously approved these changes Jan 4, 2024
@@ -58,7 +58,7 @@ extension IntegerParseStrategy: ParseStrategy {
if let value = parser.parseAsInt(substr, upperBound: &upperBound) {
let upperBoundInSubstr = String.Index(utf16Offset: upperBound, in: substr)
return (upperBoundInSubstr, Format.FormatInput(value))
} else if let value = parser.parseAsInt(substr, upperBound: &upperBound) {
} else if let value = parser.parseAsDouble(substr, upperBound: &upperBound) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with merging it as is and revisit if we can remove this altogether separately.

@itingliu itingliu dismissed their stale review January 9, 2024 19:56

Dismissing the review so @gwynne sees my latest comment

@@ -32,8 +32,6 @@ extension IntegerParseStrategy: ParseStrategy {
let trimmedString = value._trimmingWhitespace()
if let v = parser.parseAsInt(trimmedString) {
return Format.FormatInput(v)
} else if let v = parser.parseAsDouble(trimmedString) {
return Format.FormatInput(clamping: Int64(v))
Copy link
Contributor

Choose a reason for hiding this comment

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

This one I'm not so sure. We don't know if this code path is not currently being used even though I can't personally hit it. Should we just leave it as is for now?

@compnerd
Copy link
Collaborator

@swift-ci please test

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.

None yet

3 participants