mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib] allow construction of UTF16Index from Int
...when Foundation is loaded. Also, doc comments for its RandomAccessIndexType conformance Swift SVN r24637
This commit is contained in:
@@ -14,9 +14,29 @@
|
||||
// imported. Making this API dependent on Foundation decouples the
|
||||
// Swift core from a UTF16 representation.
|
||||
extension String.UTF16View.Index : RandomAccessIndexType {
|
||||
|
||||
/// Construct from an integer offset.
|
||||
public init(_ offset: Int) {
|
||||
_precondition(offset >= 0, "Negative UTF16 index offset not allowed")
|
||||
self.init(_offset: offset)
|
||||
// self._offset = offset
|
||||
}
|
||||
|
||||
/// Return the minimum number of applications of `successor` or
|
||||
/// `predecessor` required to reach `other` from `self`.
|
||||
///
|
||||
/// Complexity: O(1).
|
||||
public func distanceTo(x: String.UTF16View.Index) -> Int {
|
||||
return x._offset - _offset
|
||||
}
|
||||
|
||||
/// Return `self` offset by `n` steps.
|
||||
///
|
||||
/// :returns: If `n > 0`, the result of applying `successor` to
|
||||
/// `self` `n` times. If `n < 0`, the result of applying
|
||||
/// `predecessor` to `self` `-n` times. Otherwise, `self`.
|
||||
///
|
||||
/// Complexity: O(1)
|
||||
public func advancedBy(x: Int) -> String.UTF16View.Index {
|
||||
return String.UTF16View.Index(_offset: _offset + x)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user