Skip to content

Supported Scanner API for Swift #1914

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2019
Merged

Supported Scanner API for Swift #1914

merged 3 commits into from
Feb 15, 2019

Conversation

millenomi
Copy link
Contributor

@millenomi millenomi commented Feb 14, 2019

Add API on Scanner that's more idiomatic for use in Swift. This promotes some of the existing experimental API and deprecates the rest. The new API is intended to operate on graphemes, not single UTF-16 code units. This API is not experimental and will be supported going forward.

@millenomi
Copy link
Contributor Author

@swift-ci please test

@millenomi
Copy link
Contributor Author

@swift-ci Please test source compatibility

@millenomi
Copy link
Contributor Author

… worth a try.

@millenomi
Copy link
Contributor Author

@shahmishal is it possible to set up 'please test source compatibility' for s-c-f?

}

public func scanInt(representation: NumberRepresentation = .decimal) -> Int? {
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to write this as if Int.bitWidth == Int64.bitWidth to avoid having to maintain a list of archs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use this in Data as well just to save the runtime check.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldnt it be a constant expression and optimised away by the compiler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There were issues with that IIRC, and it may or may not be.

@millenomi
Copy link
Contributor Author

I bet the issue with the test is that one of the emoji isn't recognized as an emoji by ICU 61.

var value = Int32.max
switch representation {
case .decimal: guard self.scanInt32(&value) else { return nil }
default:
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be case .hexadecimal instead of .default ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, yep.

@spevans
Copy link
Contributor

spevans commented Feb 14, 2019

Are scanUInt(representation:) and scanUInt32(representation:) deliberately missing? Seems a bit lopsided not to have those.

@spevans
Copy link
Contributor

spevans commented Feb 14, 2019

@millenomi because of https://bugs.swift.org/browse/SR-9699 the CI tests arent even building ICU so might be worth getting it fixed now ;)


// Overflow:
withScanner(for: "\(Int64.max)0") {
expectEqual($0.scanInt64(), Int64.max, "Overflow")
Copy link
Contributor

Choose a reason for hiding this comment

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

So is there anyway to detect if a number being scanned is >Int64.max ? Or have a flag to say dont clip to Int64.max ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can't not clip to a Int64 value, since it returns a Int64. This is consistent with the behavior of the older methods.

@millenomi
Copy link
Contributor Author

@spevans it is indeed https://bugs.swift.org/browse/SR-9699 — I pinged @shahmishal about it.


// On overflow, the below methods will return success and clamp
@discardableResult
open func scanInt32(_ result: UnsafeMutablePointer<Int32>) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I misreading the Scanner documentation or should all of these scanXXX(_ result: UnsafeMutablePointer<XXX>) -> Bool methods actually take an optional pointer?
https://developer.apple.com/documentation/foundation/scanner/1410914-scanint32

Looks like they may have always been wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good catch. I'll follow up to correct the signatures.

@shahmishal
Copy link
Member

@shahmishal is it possible to set up 'please test source compatibility' for s-c-f?

Yes, can you file a Jira bug?

@spevans
Copy link
Contributor

spevans commented Feb 15, 2019

@swift-ci test

@millenomi millenomi merged commit 8a87bdd into swiftlang:master Feb 15, 2019
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.

3 participants