Skip to content

Commit 9f8fa52

Browse files
committed
Merge pull request #339 from apple/swift-3-indexing-model
[swift-3-indexing-model] porting Foundation to the new indexing model
2 parents d2dc9f3 + fdfdd38 commit 9f8fa52

19 files changed

+109
-87
lines changed

Foundation/NSCFString.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ internal func _CFSwiftStringGetBytes(_ str: AnyObject, encoding: CFStringEncodin
133133
let start = encodingView.startIndex
134134
if let buffer = buffer {
135135
for idx in 0..<range.length {
136-
let character = encodingView[start.advanced(by: idx + range.location)]
136+
let characterIndex = encodingView.index(start, offsetBy: idx + range.location)
137+
let character = encodingView[characterIndex]
137138
buffer.advanced(by: idx).initialize(with: character)
138139
}
139140
}

Foundation/NSCalendar.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,9 +1038,9 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
10381038
comps.minute = m
10391039
comps.second = s
10401040
var options: NSCalendarOptions = .MatchNextTime
1041-
options.unionInPlace(opts.contains(.MatchLast) ? .MatchLast : .MatchFirst)
1041+
options.formUnion(opts.contains(.MatchLast) ? .MatchLast : .MatchFirst)
10421042
if opts.contains(.MatchStrictly) {
1043-
options.unionInPlace(.MatchStrictly)
1043+
options.formUnion(.MatchStrictly)
10441044
}
10451045
if let result = nextDateAfterDate(range.start.addingTimeInterval(-0.5), matchingComponents: comps, options: options) {
10461046
if result.compare(range.start) == .OrderedAscending {
@@ -1062,7 +1062,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
10621062
var unitFlags: NSCalendarUnit = []
10631063
for unit in units {
10641064
if components.valueForComponent(unit) != NSDateComponentUndefined {
1065-
unitFlags.unionInPlace(unit)
1065+
unitFlags.formUnion(unit)
10661066
}
10671067
}
10681068
if unitFlags == [] {

Foundation/NSData.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ extension NSData {
539539
}
540540
return location.map {NSRange(location: $0, length: search.count)} ?? NSRange(location: NSNotFound, length: 0)
541541
}
542-
private static func searchSubSequence<T : Collection,T2 : Sequence where T.Iterator.Element : Equatable, T.Iterator.Element == T2.Iterator.Element, T.SubSequence.Iterator.Element == T.Iterator.Element>(_ subSequence : T2, inSequence seq: T,anchored : Bool) -> T.Index? {
542+
private static func searchSubSequence<T : Collection, T2 : Sequence where T.Iterator.Element : Equatable, T.Iterator.Element == T2.Iterator.Element, T.SubSequence.Iterator.Element == T.Iterator.Element, T.Indices.Iterator.Element == T.Index>(_ subSequence : T2, inSequence seq: T,anchored : Bool) -> T.Index? {
543543
for index in seq.indices {
544544
if seq.suffix(from: index).starts(with: subSequence) {
545545
return index
@@ -684,10 +684,10 @@ extension NSData {
684684
var decodedStart: UInt8 = 0
685685
for range in base64ByteMappings {
686686
if range.contains(byte) {
687-
let result = decodedStart + (byte - range.startIndex)
687+
let result = decodedStart + (byte - range.lowerBound)
688688
return .Valid(result)
689689
}
690-
decodedStart += range.endIndex - range.startIndex
690+
decodedStart += range.upperBound - range.lowerBound
691691
}
692692
return .Invalid
693693
}
@@ -706,11 +706,11 @@ extension NSData {
706706
assert(byte < 64)
707707
var decodedStart: UInt8 = 0
708708
for range in base64ByteMappings {
709-
let decodedRange = decodedStart ..< decodedStart + (range.endIndex - range.startIndex)
709+
let decodedRange = decodedStart ..< decodedStart + (range.upperBound - range.lowerBound)
710710
if decodedRange.contains(byte) {
711-
return range.startIndex + (byte - decodedStart)
711+
return range.lowerBound + (byte - decodedStart)
712712
}
713-
decodedStart += range.endIndex - range.startIndex
713+
decodedStart += range.upperBound - range.lowerBound
714714
}
715715
return 0
716716
}

Foundation/NSHTTPCookie.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ public class NSHTTPCookie : NSObject {
323323
}
324324
//Remove the final trailing semicolon and whitespace
325325
if ( cookieString.length > 0 ) {
326-
cookieString.remove(at: cookieString.endIndex.predecessor())
327-
cookieString.remove(at: cookieString.endIndex.predecessor())
326+
cookieString.characters.removeLast()
327+
cookieString.characters.removeLast()
328328
}
329329
return ["Cookie": cookieString]
330330
}

Foundation/NSJSONSerialization.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ private struct JSONReader {
406406
}
407407

408408
typealias Index = Int
409+
typealias IndexDistance = Int
409410

410411
struct UnicodeSource {
411412
let buffer: UnsafeBufferPointer<UInt8>
@@ -467,7 +468,7 @@ private struct JSONReader {
467468
return input + step <= buffer.endIndex
468469
}
469470

470-
func distanceFromStart(_ index: Index) -> Index.Distance {
471+
func distanceFromStart(_ index: Index) -> IndexDistance {
471472
return buffer.startIndex.distance(to: index) / step
472473
}
473474
}
@@ -625,7 +626,7 @@ private struct JSONReader {
625626
0x2E, 0x2D, 0x2B, 0x45, 0x65, // . - + E e
626627
]
627628
func parseNumber(_ input: Index) throws -> (Double, Index)? {
628-
func parseDouble(_ address: UnsafePointer<UInt8>) -> (Double, Index.Distance)? {
629+
func parseDouble(_ address: UnsafePointer<UInt8>) -> (Double, IndexDistance)? {
629630
let startPointer = UnsafePointer<Int8>(address)
630631
let endPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 1)
631632
defer { endPointer.deallocateCapacity(1) }

Foundation/NSKeyedUnarchiver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public class NSKeyedUnarchiver : NSCoder {
527527
Helper for NSArray/NSDictionary to dereference and decode an array of objects
528528
*/
529529
internal func _decodeArrayOfObjectsForKey(_ key: String,
530-
@noescape withBlock block: (Any) -> Void) throws {
530+
withBlock block: @noescape (Any) -> Void) throws {
531531
let objectRefs : Array<Any>? = _decodeValue(forKey: key)
532532

533533
guard let unwrappedObjectRefs = objectRefs else {

Foundation/NSLock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class NSLock : NSObject, NSLocking {
5151
}
5252

5353
extension NSLock {
54-
internal func synchronized<T>(@noescape _ closure: () -> T) -> T {
54+
internal func synchronized<T>(_ closure: @noescape () -> T) -> T {
5555
self.lock()
5656
defer { self.unlock() }
5757
return closure()

Foundation/NSOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ internal struct _OperationList {
279279
return all.count
280280
}
281281

282-
func map<T>(@noescape _ transform: (NSOperation) throws -> T) rethrows -> [T] {
282+
func map<T>(_ transform: @noescape (NSOperation) throws -> T) rethrows -> [T] {
283283
return try all.map(transform)
284284
}
285285
}

Foundation/NSPathUtilities.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal extension String {
4545

4646
// Find the beginning of the component
4747
while curPos > startPos {
48-
let prevPos = curPos.predecessor()
48+
let prevPos = characterView.index(before: curPos)
4949
if characterView[prevPos] == "/" {
5050
break
5151
}
@@ -66,7 +66,7 @@ internal extension String {
6666

6767
// Find the beginning of the extension
6868
while curPos > lastCompStartPos {
69-
let prevPos = curPos.predecessor()
69+
let prevPos = characterView.index(before: curPos)
7070
let char = characterView[prevPos]
7171
if char == "/" {
7272
return nil
@@ -111,21 +111,21 @@ internal extension String {
111111
if characterView[curPos] == "/" {
112112
var afterLastSlashPos = curPos
113113
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
114-
afterLastSlashPos = afterLastSlashPos.successor()
114+
afterLastSlashPos = characterView.index(after: afterLastSlashPos)
115115
}
116-
if afterLastSlashPos != curPos.successor() {
116+
if afterLastSlashPos != characterView.index(after: curPos) {
117117
characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
118118
endPos = characterView.endIndex
119119
}
120120
curPos = afterLastSlashPos
121121
} else {
122-
curPos = curPos.successor()
122+
curPos = characterView.index(after: curPos)
123123
}
124124
}
125125
}
126126
}
127127
if stripTrailing && result.length > 1 && result.hasSuffix("/") {
128-
result.remove(at: result.characters.endIndex.predecessor())
128+
result.remove(at: result.characters.index(before: result.characters.endIndex))
129129
}
130130
return result
131131
}
@@ -185,14 +185,14 @@ public extension NSString {
185185

186186
while curPos < endPos {
187187
while curPos < endPos && characterView[curPos] == "/" {
188-
curPos = curPos.successor()
188+
curPos = characterView.index(after: curPos)
189189
}
190190
if curPos == endPos {
191191
break
192192
}
193193
var curEnd = curPos
194194
while curEnd < endPos && characterView[curEnd] != "/" {
195-
curEnd = curEnd.successor()
195+
curEnd = characterView.index(after: curEnd)
196196
}
197197
result.append(String(characterView[curPos ..< curEnd]))
198198
curPos = curEnd
@@ -226,12 +226,12 @@ public extension NSString {
226226
return ""
227227

228228
// absolute path, single component
229-
case fixedSelf.startIndex.successor():
229+
case fixedSelf.index(after: fixedSelf.startIndex):
230230
return "/"
231231

232232
// all common cases
233233
case let startOfLast:
234-
return String(fixedSelf.characters.prefix(upTo: startOfLast.predecessor()))
234+
return String(fixedSelf.characters.prefix(upTo: fixedSelf.index(before: startOfLast)))
235235
}
236236
}
237237

@@ -251,21 +251,21 @@ public extension NSString {
251251
if characterView[curPos] == "/" {
252252
var afterLastSlashPos = curPos
253253
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
254-
afterLastSlashPos = afterLastSlashPos.successor()
254+
afterLastSlashPos = characterView.index(after: afterLastSlashPos)
255255
}
256-
if afterLastSlashPos != curPos.successor() {
256+
if afterLastSlashPos != characterView.index(after: curPos) {
257257
characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
258258
endPos = characterView.endIndex
259259
}
260260
curPos = afterLastSlashPos
261261
} else {
262-
curPos = curPos.successor()
262+
curPos = characterView.index(after: curPos)
263263
}
264264
}
265265
}
266266
}
267267
if stripTrailing && result.hasSuffix("/") {
268-
result.remove(at: result.characters.endIndex.predecessor())
268+
result.remove(at: result.characters.index(before: result.characters.endIndex))
269269
}
270270
return result
271271
}
@@ -306,7 +306,7 @@ public extension NSString {
306306
return fixedSelf
307307
}
308308
if let extensionPos = (fixedSelf._startOfPathExtension) {
309-
return String(fixedSelf.characters.prefix(upTo: extensionPos.predecessor()))
309+
return String(fixedSelf.characters.prefix(upTo: fixedSelf.characters.index(before: extensionPos)))
310310
} else {
311311
return fixedSelf
312312
}
@@ -327,7 +327,8 @@ public extension NSString {
327327
}
328328

329329
let endOfUserName = _swiftObject.characters.index(of: "/") ?? _swiftObject.endIndex
330-
let userName = String(_swiftObject.characters[_swiftObject.startIndex.successor()..<endOfUserName])
330+
let startOfUserName = _swiftObject.characters.index(after: _swiftObject.characters.startIndex)
331+
let userName = String(_swiftObject.characters[startOfUserName..<endOfUserName])
331332
let optUserName: String? = userName.isEmpty ? nil : userName
332333

333334
guard let homeDir = NSHomeDirectoryForUser(optUserName) else {

Foundation/NSRange.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ extension CFRange {
3838
public typealias NSRange = _NSRange
3939

4040
extension NSRange {
41-
public init(_ x: Range<Int>) {
41+
public init(_ x: CountableRange<Int>) {
4242
location = x.startIndex
4343
length = x.count
4444
}
4545

4646
@warn_unused_result
47-
public func toRange() -> Range<Int>? {
47+
public func toRange() -> CountableRange<Int>? {
4848
if location == NSNotFound { return nil }
4949
let min = location
5050
let max = location + length

Foundation/NSRegularExpression.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ extension NSRegularExpression {
157157

158158
public func matches(in string: String, options: NSMatchingOptions, range: NSRange) -> [NSTextCheckingResult] {
159159
var matches = [NSTextCheckingResult]()
160-
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
160+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
161161
if let match = result {
162162
matches.append(match)
163163
}
@@ -168,15 +168,15 @@ extension NSRegularExpression {
168168

169169
public func numberOfMatches(in string: String, options: NSMatchingOptions, range: NSRange) -> Int {
170170
var count = 0
171-
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion).union(.OmitResult), range: range) {_,_,_ in
171+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion).union(.OmitResult), range: range) {_,_,_ in
172172
count += 1
173173
}
174174
return count
175175
}
176176

177177
public func firstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult? {
178178
var first: NSTextCheckingResult?
179-
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
179+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
180180
first = result
181181
stop.pointee = true
182182
}
@@ -185,7 +185,7 @@ extension NSRegularExpression {
185185

186186
public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSRange {
187187
var firstRange = NSMakeRange(NSNotFound, 0)
188-
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
188+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
189189
if let match = result {
190190
firstRange = match.range
191191
} else {
@@ -214,7 +214,7 @@ extension NSRegularExpression {
214214
var str: String = ""
215215
let length = string.length
216216
var previousRange = NSMakeRange(0, 0)
217-
let results = matches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range)
217+
let results = matches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range)
218218
let start = string.utf16.startIndex
219219

220220
for result in results {
@@ -239,7 +239,7 @@ extension NSRegularExpression {
239239
}
240240

241241
public func replaceMatches(in string: NSMutableString, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> Int {
242-
let results = matches(in: string._swiftObject, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range)
242+
let results = matches(in: string._swiftObject, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range)
243243
var count = 0
244244
var offset = 0
245245
for result in results {

Foundation/NSSet.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ extension NSSet {
271271
public func setByAddingObjectsFromSet(_ other: Set<NSObject>) -> Set<NSObject> {
272272
var result = Set<NSObject>(minimumCapacity: Swift.max(count, other.count))
273273
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
274-
result.unionInPlace(_storage)
274+
result.formUnion(_storage)
275275
} else {
276276
for case let obj as NSObject in self {
277277
result.insert(obj)
@@ -283,7 +283,7 @@ extension NSSet {
283283
public func setByAddingObjectsFromArray(_ other: [AnyObject]) -> Set<NSObject> {
284284
var result = Set<NSObject>(minimumCapacity: count)
285285
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
286-
result.unionInPlace(_storage)
286+
result.formUnion(_storage)
287287
} else {
288288
for case let obj as NSObject in self {
289289
result.insert(obj)
@@ -400,7 +400,7 @@ public class NSMutableSet : NSSet {
400400

401401
public func intersectSet(_ otherSet: Set<NSObject>) {
402402
if self.dynamicType === NSMutableSet.self {
403-
_storage.intersectInPlace(otherSet)
403+
_storage.formIntersection(otherSet)
404404
} else {
405405
for case let obj as NSObject in self where !otherSet.contains(obj) {
406406
removeObject(obj)
@@ -410,7 +410,7 @@ public class NSMutableSet : NSSet {
410410

411411
public func minusSet(_ otherSet: Set<NSObject>) {
412412
if self.dynamicType === NSMutableSet.self {
413-
_storage.subtractInPlace(otherSet)
413+
_storage.subtract(otherSet)
414414
} else {
415415
otherSet.forEach(removeObject)
416416
}
@@ -426,7 +426,7 @@ public class NSMutableSet : NSSet {
426426

427427
public func unionSet(_ otherSet: Set<NSObject>) {
428428
if self.dynamicType === NSMutableSet.self {
429-
_storage.unionInPlace(otherSet)
429+
_storage.formUnion(otherSet)
430430
} else {
431431
otherSet.forEach(addObject)
432432
}

Foundation/NSString.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ internal func _bytesInEncoding(_ str: NSString, _ encoding: NSStringEncoding, _
169169
var used = 0
170170
var options: NSStringEncodingConversionOptions = []
171171
if externalRep {
172-
options.unionInPlace(.ExternalRepresentation)
172+
options.formUnion(.ExternalRepresentation)
173173
}
174174
if lossy {
175-
options.unionInPlace(.AllowLossy)
175+
options.formUnion(.AllowLossy)
176176
}
177177
if !str.getBytes(nil, maxLength: Int.max - 1, usedLength: &cLength, encoding: encoding, options: options, range: theRange, remainingRange: nil) {
178178
if fatalOnError {
@@ -1294,8 +1294,8 @@ public class NSMutableString : NSString {
12941294
if self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self {
12951295
// this is incorrectly calculated for grapheme clusters that have a size greater than a single unichar
12961296
let start = _storage.startIndex
1297-
let min = start.advanced(by: range.location)
1298-
let max = start.advanced(by: range.location + range.length)
1297+
let min = _storage.index(start, offsetBy: range.location)
1298+
let max = _storage.index(start, offsetBy: range.location + range.length)
12991299
_storage.replaceSubrange(min..<max, with: aString)
13001300
} else {
13011301
NSRequiresConcreteImplementation()

0 commit comments

Comments
 (0)