Skip to content

[swift-3-indexing-model] porting Foundation to the new indexing model #339

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 1 commit into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Foundation/NSCFString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ internal func _CFSwiftStringGetBytes(_ str: AnyObject, encoding: CFStringEncodin
let start = encodingView.startIndex
if let buffer = buffer {
for idx in 0..<range.length {
let character = encodingView[start.advanced(by: idx + range.location)]
let characterIndex = encodingView.index(start, offsetBy: idx + range.location)
let character = encodingView[characterIndex]
buffer.advanced(by: idx).initialize(with: character)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,9 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
comps.minute = m
comps.second = s
var options: NSCalendarOptions = .MatchNextTime
options.unionInPlace(opts.contains(.MatchLast) ? .MatchLast : .MatchFirst)
options.formUnion(opts.contains(.MatchLast) ? .MatchLast : .MatchFirst)
if opts.contains(.MatchStrictly) {
options.unionInPlace(.MatchStrictly)
options.formUnion(.MatchStrictly)
}
if let result = nextDateAfterDate(range.start.addingTimeInterval(-0.5), matchingComponents: comps, options: options) {
if result.compare(range.start) == .OrderedAscending {
Expand All @@ -1062,7 +1062,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
var unitFlags: NSCalendarUnit = []
for unit in units {
if components.valueForComponent(unit) != NSDateComponentUndefined {
unitFlags.unionInPlace(unit)
unitFlags.formUnion(unit)
}
}
if unitFlags == [] {
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ extension NSData {
}
return location.map {NSRange(location: $0, length: search.count)} ?? NSRange(location: NSNotFound, length: 0)
}
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? {
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? {
for index in seq.indices {
if seq.suffix(from: index).starts(with: subSequence) {
return index
Expand Down Expand Up @@ -684,10 +684,10 @@ extension NSData {
var decodedStart: UInt8 = 0
for range in base64ByteMappings {
if range.contains(byte) {
let result = decodedStart + (byte - range.startIndex)
let result = decodedStart + (byte - range.lowerBound)
return .Valid(result)
}
decodedStart += range.endIndex - range.startIndex
decodedStart += range.upperBound - range.lowerBound
}
return .Invalid
}
Expand All @@ -706,11 +706,11 @@ extension NSData {
assert(byte < 64)
var decodedStart: UInt8 = 0
for range in base64ByteMappings {
let decodedRange = decodedStart ..< decodedStart + (range.endIndex - range.startIndex)
let decodedRange = decodedStart ..< decodedStart + (range.upperBound - range.lowerBound)
if decodedRange.contains(byte) {
return range.startIndex + (byte - decodedStart)
return range.lowerBound + (byte - decodedStart)
}
decodedStart += range.endIndex - range.startIndex
decodedStart += range.upperBound - range.lowerBound
}
return 0
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSHTTPCookie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ public class NSHTTPCookie : NSObject {
}
//Remove the final trailing semicolon and whitespace
if ( cookieString.length > 0 ) {
cookieString.remove(at: cookieString.endIndex.predecessor())
cookieString.remove(at: cookieString.endIndex.predecessor())
cookieString.characters.removeLast()
cookieString.characters.removeLast()
}
return ["Cookie": cookieString]
}
Expand Down
5 changes: 3 additions & 2 deletions Foundation/NSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ private struct JSONReader {
}

typealias Index = Int
typealias IndexDistance = Int

struct UnicodeSource {
let buffer: UnsafeBufferPointer<UInt8>
Expand Down Expand Up @@ -467,7 +468,7 @@ private struct JSONReader {
return input + step <= buffer.endIndex
}

func distanceFromStart(_ index: Index) -> Index.Distance {
func distanceFromStart(_ index: Index) -> IndexDistance {
return buffer.startIndex.distance(to: index) / step
}
}
Expand Down Expand Up @@ -625,7 +626,7 @@ private struct JSONReader {
0x2E, 0x2D, 0x2B, 0x45, 0x65, // . - + E e
]
func parseNumber(_ input: Index) throws -> (Double, Index)? {
func parseDouble(_ address: UnsafePointer<UInt8>) -> (Double, Index.Distance)? {
func parseDouble(_ address: UnsafePointer<UInt8>) -> (Double, IndexDistance)? {
let startPointer = UnsafePointer<Int8>(address)
let endPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 1)
defer { endPointer.deallocateCapacity(1) }
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSKeyedUnarchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ public class NSKeyedUnarchiver : NSCoder {
Helper for NSArray/NSDictionary to dereference and decode an array of objects
*/
internal func _decodeArrayOfObjectsForKey(_ key: String,
@noescape withBlock block: (Any) -> Void) throws {
withBlock block: @noescape (Any) -> Void) throws {
let objectRefs : Array<Any>? = _decodeValue(forKey: key)

guard let unwrappedObjectRefs = objectRefs else {
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class NSLock : NSObject, NSLocking {
}

extension NSLock {
internal func synchronized<T>(@noescape _ closure: () -> T) -> T {
internal func synchronized<T>(_ closure: @noescape () -> T) -> T {
self.lock()
defer { self.unlock() }
return closure()
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal struct _OperationList {
return all.count
}

func map<T>(@noescape _ transform: (NSOperation) throws -> T) rethrows -> [T] {
func map<T>(_ transform: @noescape (NSOperation) throws -> T) rethrows -> [T] {
return try all.map(transform)
}
}
Expand Down
33 changes: 17 additions & 16 deletions Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal extension String {

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

// Find the beginning of the extension
while curPos > lastCompStartPos {
let prevPos = curPos.predecessor()
let prevPos = characterView.index(before: curPos)
let char = characterView[prevPos]
if char == "/" {
return nil
Expand Down Expand Up @@ -111,21 +111,21 @@ internal extension String {
if characterView[curPos] == "/" {
var afterLastSlashPos = curPos
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
afterLastSlashPos = afterLastSlashPos.successor()
afterLastSlashPos = characterView.index(after: afterLastSlashPos)
}
if afterLastSlashPos != curPos.successor() {
if afterLastSlashPos != characterView.index(after: curPos) {
characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
endPos = characterView.endIndex
}
curPos = afterLastSlashPos
} else {
curPos = curPos.successor()
curPos = characterView.index(after: curPos)
}
}
}
}
if stripTrailing && result.length > 1 && result.hasSuffix("/") {
result.remove(at: result.characters.endIndex.predecessor())
result.remove(at: result.characters.index(before: result.characters.endIndex))
}
return result
}
Expand Down Expand Up @@ -185,14 +185,14 @@ public extension NSString {

while curPos < endPos {
while curPos < endPos && characterView[curPos] == "/" {
curPos = curPos.successor()
curPos = characterView.index(after: curPos)
}
if curPos == endPos {
break
}
var curEnd = curPos
while curEnd < endPos && characterView[curEnd] != "/" {
curEnd = curEnd.successor()
curEnd = characterView.index(after: curEnd)
}
result.append(String(characterView[curPos ..< curEnd]))
curPos = curEnd
Expand Down Expand Up @@ -226,12 +226,12 @@ public extension NSString {
return ""

// absolute path, single component
case fixedSelf.startIndex.successor():
case fixedSelf.index(after: fixedSelf.startIndex):
return "/"

// all common cases
case let startOfLast:
return String(fixedSelf.characters.prefix(upTo: startOfLast.predecessor()))
return String(fixedSelf.characters.prefix(upTo: fixedSelf.index(before: startOfLast)))
}
}

Expand All @@ -251,21 +251,21 @@ public extension NSString {
if characterView[curPos] == "/" {
var afterLastSlashPos = curPos
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
afterLastSlashPos = afterLastSlashPos.successor()
afterLastSlashPos = characterView.index(after: afterLastSlashPos)
}
if afterLastSlashPos != curPos.successor() {
if afterLastSlashPos != characterView.index(after: curPos) {
characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
endPos = characterView.endIndex
}
curPos = afterLastSlashPos
} else {
curPos = curPos.successor()
curPos = characterView.index(after: curPos)
}
}
}
}
if stripTrailing && result.hasSuffix("/") {
result.remove(at: result.characters.endIndex.predecessor())
result.remove(at: result.characters.index(before: result.characters.endIndex))
}
return result
}
Expand Down Expand Up @@ -306,7 +306,7 @@ public extension NSString {
return fixedSelf
}
if let extensionPos = (fixedSelf._startOfPathExtension) {
return String(fixedSelf.characters.prefix(upTo: extensionPos.predecessor()))
return String(fixedSelf.characters.prefix(upTo: fixedSelf.characters.index(before: extensionPos)))
} else {
return fixedSelf
}
Expand All @@ -327,7 +327,8 @@ public extension NSString {
}

let endOfUserName = _swiftObject.characters.index(of: "/") ?? _swiftObject.endIndex
let userName = String(_swiftObject.characters[_swiftObject.startIndex.successor()..<endOfUserName])
let startOfUserName = _swiftObject.characters.index(after: _swiftObject.characters.startIndex)
let userName = String(_swiftObject.characters[startOfUserName..<endOfUserName])
let optUserName: String? = userName.isEmpty ? nil : userName

guard let homeDir = NSHomeDirectoryForUser(optUserName) else {
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ extension CFRange {
public typealias NSRange = _NSRange

extension NSRange {
public init(_ x: Range<Int>) {
public init(_ x: CountableRange<Int>) {
location = x.startIndex
length = x.count
}

@warn_unused_result
public func toRange() -> Range<Int>? {
public func toRange() -> CountableRange<Int>? {
if location == NSNotFound { return nil }
let min = location
let max = location + length
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSRegularExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ extension NSRegularExpression {

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

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

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

public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSRange {
var firstRange = NSMakeRange(NSNotFound, 0)
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
if let match = result {
firstRange = match.range
} else {
Expand Down Expand Up @@ -214,7 +214,7 @@ extension NSRegularExpression {
var str: String = ""
let length = string.length
var previousRange = NSMakeRange(0, 0)
let results = matches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range)
let results = matches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range)
let start = string.utf16.startIndex

for result in results {
Expand All @@ -239,7 +239,7 @@ extension NSRegularExpression {
}

public func replaceMatches(in string: NSMutableString, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> Int {
let results = matches(in: string._swiftObject, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range)
let results = matches(in: string._swiftObject, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range)
var count = 0
var offset = 0
for result in results {
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ extension NSSet {
public func setByAddingObjectsFromSet(_ other: Set<NSObject>) -> Set<NSObject> {
var result = Set<NSObject>(minimumCapacity: Swift.max(count, other.count))
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
result.unionInPlace(_storage)
result.formUnion(_storage)
} else {
for case let obj as NSObject in self {
result.insert(obj)
Expand All @@ -283,7 +283,7 @@ extension NSSet {
public func setByAddingObjectsFromArray(_ other: [AnyObject]) -> Set<NSObject> {
var result = Set<NSObject>(minimumCapacity: count)
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
result.unionInPlace(_storage)
result.formUnion(_storage)
} else {
for case let obj as NSObject in self {
result.insert(obj)
Expand Down Expand Up @@ -400,7 +400,7 @@ public class NSMutableSet : NSSet {

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

public func minusSet(_ otherSet: Set<NSObject>) {
if self.dynamicType === NSMutableSet.self {
_storage.subtractInPlace(otherSet)
_storage.subtract(otherSet)
} else {
otherSet.forEach(removeObject)
}
Expand All @@ -426,7 +426,7 @@ public class NSMutableSet : NSSet {

public func unionSet(_ otherSet: Set<NSObject>) {
if self.dynamicType === NSMutableSet.self {
_storage.unionInPlace(otherSet)
_storage.formUnion(otherSet)
} else {
otherSet.forEach(addObject)
}
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ internal func _bytesInEncoding(_ str: NSString, _ encoding: NSStringEncoding, _
var used = 0
var options: NSStringEncodingConversionOptions = []
if externalRep {
options.unionInPlace(.ExternalRepresentation)
options.formUnion(.ExternalRepresentation)
}
if lossy {
options.unionInPlace(.AllowLossy)
options.formUnion(.AllowLossy)
}
if !str.getBytes(nil, maxLength: Int.max - 1, usedLength: &cLength, encoding: encoding, options: options, range: theRange, remainingRange: nil) {
if fatalOnError {
Expand Down Expand Up @@ -1294,8 +1294,8 @@ public class NSMutableString : NSString {
if self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self {
// this is incorrectly calculated for grapheme clusters that have a size greater than a single unichar
let start = _storage.startIndex
let min = start.advanced(by: range.location)
let max = start.advanced(by: range.location + range.length)
let min = _storage.index(start, offsetBy: range.location)
let max = _storage.index(start, offsetBy: range.location + range.length)
_storage.replaceSubrange(min..<max, with: aString)
} else {
NSRequiresConcreteImplementation()
Expand Down
Loading