-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
Conversation
@swift-ci please test |
@swift-ci please test source compatibility |
} | ||
} | ||
|
||
extension Range where Bound: BinaryInteger { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 RangeExpression
s of Int
and push the casting onto the caller. I'm kind of ambivalent which.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swift-ci please smoke test and merge |
@swift-ci please smoke test |
* Add initializers to NSRange/Range * Create Ranges unchecked
Allows conversion between
Swift.Range
andNSRange
for integers and strings.