Skip to content

[Foundation] Add initializers for NSRange<-->Range #9709

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 2 commits into from
May 18, 2017

Conversation

airspeedswift
Copy link
Member

Allows conversion between Swift.Range and NSRange for integers and strings.

@airspeedswift airspeedswift requested a review from phausler May 17, 2017 20:46
@airspeedswift
Copy link
Member Author

@swift-ci please test

@airspeedswift
Copy link
Member Author

@swift-ci please test source compatibility

}
}

extension Range where Bound: BinaryInteger {
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we also do the other range types here too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, probably not. We don't generally encourage people to create the other range types without using the operators. Range is the currency type.

public init<R: RangeExpression>(_ rangeExpression: R)
where R.Bound: FixedWidthInteger, R.Bound.Stride : SignedInteger {
let range = rangeExpression.relative(to: 0..<R.Bound.max)
let start: Int = numericCast(range.lowerBound)
Copy link
Contributor

Choose a reason for hiding this comment

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

so what happens here when you have a 32 bit host and the passed in range is a UInt64 that exceeds Int32.max?

Copy link
Member Author

Choose a reason for hiding this comment

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

It would trap – expectedly. To construct an NSRange you need an Int. The alternative is we only allow construction from RangeExpressions of Int and push the casting onto the caller. I'm kind of ambivalent which.

Copy link
Contributor

Choose a reason for hiding this comment

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

eh as is seems fine; I just wanted to make sure it wasn't some funky bit truncation. Trap seems quite reasonable.

extension Range where Bound == Int {
public init?(_ range: NSRange) {
guard range.location != NSNotFound else { return nil }
self = range.lowerBound..<range.upperBound
Copy link
Member Author

Choose a reason for hiding this comment

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

@xwu's comment on #9654 reminded me this should be unchecked.

@airspeedswift
Copy link
Member Author

@swift-ci please smoke test and merge

@airspeedswift
Copy link
Member Author

@swift-ci please smoke test

@airspeedswift airspeedswift merged commit 89515f8 into swiftlang:master May 18, 2017
airspeedswift added a commit to airspeedswift/swift that referenced this pull request May 18, 2017
* Add initializers to NSRange/Range

* Create Ranges unchecked
airspeedswift added a commit that referenced this pull request May 18, 2017
* Add initializers to NSRange/Range

* Create Ranges unchecked
@airspeedswift airspeedswift deleted the nsrange-init branch May 19, 2017 17:24
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