Skip to content

Commit 965c6af

Browse files
committed
Revert "UnsafePointer conversion fixes."
This reverts commit fbf8f36. A unit test failed: AutolinkExtract.import_archive.swift But only on ubuntu 15. Speculatively revert everything because I can't spot any particular change that would affect functionality.
1 parent 007089a commit 965c6af

16 files changed

+80
-96
lines changed

Foundation/Data.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
130130

131131
This type provides "copy-on-write" behavior, and is also bridged to the Objective-C `NSData` class. You can wrap an instance of a custom subclass of `NSData` in `struct Data` by converting it using `myData as Data`.
132132

133-
`Data` can be initialized with an `UnsafeRawPointer` and count, an array of `UInt8` (the primitive byte type), an `UnsafeBufferPointer`,the contents of a file, or base-64 encoded data or strings. The buffer-oriented functions provide an extra measure of safety by automatically performing the size calculation, as the type is known at compile time.
133+
`Data` can be initialized with an `UnsafePointer` and count, an array of `UInt8` (the primitive byte type), or an `UnsafeBufferPointer`. The buffer-oriented functions provide an extra measure of safety by automatically performing the size calculation, as the type is known at compile time.
134134
*/
135135
public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, Hashable, RandomAccessCollection, MutableCollection, _MutablePairBoxing {
136136
/// The Objective-C bridged type of `Data`.
@@ -161,7 +161,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
161161
case none
162162

163163
/// A custom deallocator.
164-
case custom((UnsafeMutableRawPointer, Int) -> Void)
164+
case custom((UnsafeMutablePointer<UInt8>, Int) -> Void)
165165

166166
fileprivate var _deallocator : ((UnsafeMutableRawPointer, Int) -> Void)? {
167167
switch self {
@@ -173,7 +173,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
173173
return nil
174174
case .custom(let b):
175175
return { (ptr, len) in
176-
b(ptr, len)
176+
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: len)
177+
b(bytePtr, len)
177178
}
178179
}
179180
}
@@ -237,7 +238,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
237238
/// - parameter bytes: A pointer to the bytes.
238239
/// - parameter count: The size of the bytes.
239240
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer, or `.none`.
240-
public init(bytesNoCopy bytes: UnsafeMutableRawPointer, count: Int, deallocator: Deallocator) {
241+
public init(bytesNoCopy bytes: UnsafeMutablePointer<UInt8>, count: Int, deallocator: Deallocator) {
241242
let whichDeallocator = deallocator._deallocator
242243
_wrapped = _SwiftNSData(immutableObject: NSData(bytesNoCopy: bytes, length: count, deallocator: whichDeallocator))
243244
}
@@ -347,7 +348,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
347348
_mapUnmanaged { $0.getBytes(pointer, length: count) }
348349
}
349350

350-
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: NSRange) {
351+
private func _copyBytesHelper(to pointer: UnsafeMutablePointer<UInt8>, from range: NSRange) {
351352
_mapUnmanaged { $0.getBytes(pointer, range: range) }
352353
}
353354

@@ -388,7 +389,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
388389
guard !copyRange.isEmpty else { return 0 }
389390

390391
let nsRange = NSMakeRange(copyRange.lowerBound, copyRange.upperBound - copyRange.lowerBound)
391-
let pointer = UnsafeMutableRawPointer(buffer.baseAddress!)
392+
let pointer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(buffer.baseAddress!)
392393
_copyBytesHelper(to: pointer, from: nsRange)
393394
return copyRange.count
394395
}

Foundation/NSFileManager.swift

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ public class FileManager: NSObject {
252252
}
253253

254254
while let entry = readdir(dir!) {
255-
let entryName = withUnsafePointer(to: &entry.pointee.d_name) {
256-
String(cString: UnsafeRawPointer($0).assumingMemoryBound(to: CChar.self))
257-
}
258-
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
259-
if entryName != "." && entryName != ".." {
260-
contents.append(entryName)
255+
if let entryName = withUnsafePointer(to: &entry.pointee.d_name, { (ptr) -> String? in
256+
return String(cString: UnsafePointer<Int8>(ptr))
257+
}) {
258+
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
259+
if entryName != "." && entryName != ".." {
260+
contents.append(entryName)
261+
}
261262
}
262263
}
263264

@@ -293,27 +294,31 @@ public class FileManager: NSObject {
293294
var entry = readdir(dir!)
294295

295296
while entry != nil {
296-
let entryName = withUnsafePointer(to: &entry!.pointee.d_name) {
297-
String(cString: UnsafeRawPointer($0).assumingMemoryBound(to: CChar.self))
298-
}
299-
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
300-
if entryName != "." && entryName != ".." {
301-
contents.append(entryName)
297+
if let entryName = withUnsafePointer(to: &entry!.pointee.d_name, { (ptr) -> String? in
298+
let int8Ptr = unsafeBitCast(ptr, to: UnsafePointer<Int8>.self)
299+
return String(cString: int8Ptr)
300+
}) {
301+
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
302+
if entryName != "." && entryName != ".." {
303+
contents.append(entryName)
302304

303-
let entryType = withUnsafePointer(to: &entry!.pointee.d_type) { (ptr) -> Int32 in
304-
return Int32(ptr.pointee)
305-
}
306-
#if os(OSX) || os(iOS)
307-
let tempEntryType = entryType
308-
#elseif os(Linux)
309-
let tempEntryType = Int(entryType)
310-
#endif
305+
if let entryType = withUnsafePointer(to: &entry!.pointee.d_type, { (ptr) -> Int32? in
306+
let int32Ptr = unsafeBitCast(ptr, to: UnsafePointer<UInt8>.self)
307+
return Int32(int32Ptr.pointee)
308+
}) {
309+
#if os(OSX) || os(iOS)
310+
let tempEntryType = entryType
311+
#elseif os(Linux)
312+
let tempEntryType = Int(entryType)
313+
#endif
311314

312-
if tempEntryType == DT_DIR {
313-
let subPath: String = path + "/" + entryName
315+
if tempEntryType == DT_DIR {
316+
let subPath: String = path + "/" + entryName
314317

315-
let entries = try subpathsOfDirectory(atPath: subPath)
316-
contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
318+
let entries = try subpathsOfDirectory(atPath: subPath)
319+
contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
320+
}
321+
}
317322
}
318323
}
319324

@@ -463,7 +468,7 @@ public class FileManager: NSObject {
463468

464469
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
465470
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
466-
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
471+
ps.initialize(to: UnsafeMutablePointer(fsRep))
467472
ps.advanced(by: 1).initialize(to: nil)
468473
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
469474
ps.deinitialize(count: 2)
@@ -909,7 +914,7 @@ extension FileManager {
909914
if FileManager.default().fileExists(atPath: path) {
910915
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
911916
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
912-
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
917+
ps.initialize(to: UnsafeMutablePointer(fsRep))
913918
ps.advanced(by: 1).initialize(to: nil)
914919
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
915920
ps.deinitialize(count: 2)

Foundation/NSJSONSerialization.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ public class JSONSerialization : NSObject {
111111
pretty: opt.contains(.prettyPrinted),
112112
writer: { (str: String?) in
113113
if let str = str {
114-
let count = str.lengthOfBytes(using: .utf8)
115-
result.append(UnsafeRawPointer(str.cString(using: .utf8)!).bindMemory(to: UInt8.self, capacity: count), count: count)
114+
result.append(UnsafePointer<UInt8>(str.cString(using: .utf8)!), count: str.lengthOfBytes(using: .utf8))
116115
}
117116
}
118117
)
@@ -636,11 +635,11 @@ private struct JSONReader {
636635
func parseNumber(_ input: Index) throws -> (Any, Index)? {
637636
func parseTypedNumber(_ address: UnsafePointer<UInt8>, count: Int) -> (Any, IndexDistance)? {
638637
let temp_buffer_size = 64
639-
var temp_buffer = [Int8](repeating: 0, count: temp_buffer_size)
640-
return temp_buffer.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<Int8>) -> (Any, IndexDistance)? in
638+
var temp_buffer = [UInt8](repeating: 0, count: temp_buffer_size)
639+
return temp_buffer.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<UInt8>) -> (Any, IndexDistance)? in
641640
memcpy(buffer.baseAddress!, address, min(count, temp_buffer_size - 1)) // ensure null termination
642641

643-
let startPointer = buffer.baseAddress!
642+
let startPointer = UnsafePointer<Int8>(buffer.baseAddress!)
644643
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
645644
defer { intEndPointer.deallocate(capacity: 1) }
646645
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)

Foundation/NSRegularExpression.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ internal func _NSRegularExpressionMatch(_ context: UnsafeMutableRawPointer?, ran
130130
#endif
131131
matcher.block(nil, NSMatchingFlags(rawValue: opts), UnsafeMutablePointer<ObjCBool>(stop))
132132
} else {
133-
let result = ranges!.withMemoryRebound(to: NSRange.self, capacity: count) { rangePtr in
134-
TextCheckingResult.regularExpressionCheckingResultWithRanges(rangePtr, count: count, regularExpression: matcher.regex)
135-
}
133+
let result = TextCheckingResult.regularExpressionCheckingResultWithRanges(NSRangePointer(ranges!), count: count, regularExpression: matcher.regex)
136134
#if os(OSX) || os(iOS)
137135
let flags = NSMatchingFlags(rawValue: options.rawValue)
138136
#else

Foundation/NSString.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ extension NSString {
469469
var numCharsBuffered = 0
470470
var arrayBuffer = [unichar](repeating: 0, count: 100)
471471
let other = str._nsObject
472-
return arrayBuffer.withUnsafeMutablePointerOrAllocation(selfLen, fastpath: UnsafeMutablePointer<unichar>(mutating: _fastContents)) { (selfChars: UnsafeMutablePointer<unichar>) -> String in
472+
return arrayBuffer.withUnsafeMutablePointerOrAllocation(selfLen, fastpath: UnsafeMutablePointer<unichar>(_fastContents)) { (selfChars: UnsafeMutablePointer<unichar>) -> String in
473473
// Now do the binary search. Note that the probe value determines the length of the substring to check.
474474
while true {
475475
let range = NSMakeRange(0, isLiteral ? probe + 1 : NSMaxRange(rangeOfComposedCharacterSequence(at: probe))) // Extend the end of the composed char sequence
@@ -1182,12 +1182,8 @@ extension NSString {
11821182
}
11831183

11841184
public convenience init?(UTF8String nullTerminatedCString: UnsafePointer<Int8>) {
1185-
let count = Int(strlen(nullTerminatedCString))
1186-
if let str = nullTerminatedCString.withMemoryRebound(to: UInt8.self, capacity: count, {
1187-
let buffer = UnsafeBufferPointer<UInt8>(start: $0, count: count)
1188-
return String._fromCodeUnitSequence(UTF8.self, input: buffer)
1189-
}) as String?
1190-
{
1185+
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(nullTerminatedCString), count: Int(strlen(nullTerminatedCString)))
1186+
if let str = String._fromCodeUnitSequence(UTF8.self, input: buffer) {
11911187
self.init(str)
11921188
} else {
11931189
return nil

Foundation/NSURL.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,7 @@ public class NSURL: NSObject, NSSecureCoding, NSCopying {
488488
/* Returns the URL's path in file system representation. File system representation is a null-terminated C string with canonical UTF-8 encoding.
489489
*/
490490
public func getFileSystemRepresentation(_ buffer: UnsafeMutablePointer<Int8>, maxLength maxBufferLength: Int) -> Bool {
491-
return buffer.withMemoryRebound(to: UInt8.self, capacity: maxBufferLength) {
492-
CFURLGetFileSystemRepresentation(_cfObject, true, $0, maxBufferLength)
493-
}
491+
return CFURLGetFileSystemRepresentation(_cfObject, true, UnsafeMutablePointer<UInt8>(buffer), maxBufferLength)
494492
}
495493

496494
/* Returns the URL's path in file system representation. File system representation is a null-terminated C string with canonical UTF-8 encoding. The returned C string will be automatically freed just as a returned object would be released; your code should copy the representation or use getFileSystemRepresentation:maxLength: if it needs to store the representation outside of the autorelease context in which the representation is created.

Foundation/NSXMLElement.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,8 @@ public class XMLElement: XMLNode {
7474
@abstract Adds an attribute. Attributes with duplicate names are not added.
7575
*/
7676
public func addAttribute(_ attribute: XMLNode) {
77-
let name = _CFXMLNodeGetName(attribute._xmlNode)!
78-
let len = strlen(name)
79-
name.withMemoryRebound(to: UInt8.self, capacity: Int(len)) {
80-
guard _CFXMLNodeHasProp(_xmlNode, $0) == nil else { return }
81-
addChild(attribute)
82-
}
77+
guard _CFXMLNodeHasProp(_xmlNode, UnsafePointer<UInt8>(_CFXMLNodeGetName(attribute._xmlNode)!)) == nil else { return }
78+
addChild(attribute)
8379
} //primitive
8480

8581
/*!

0 commit comments

Comments
 (0)