Skip to content

UnsafePointer conversion fixes. #487

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
Jul 29, 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
13 changes: 6 additions & 7 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {

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`.

`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.
`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.
*/
public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, Hashable, RandomAccessCollection, MutableCollection, _MutablePairBoxing {
/// The Objective-C bridged type of `Data`.
Expand Down Expand Up @@ -161,7 +161,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
case none

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

fileprivate var _deallocator : ((UnsafeMutableRawPointer, Int) -> Void)? {
switch self {
Expand All @@ -173,8 +173,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
return nil
case .custom(let b):
return { (ptr, len) in
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: len)
b(bytePtr, len)
b(ptr, len)
}
}
}
Expand Down Expand Up @@ -238,7 +237,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
/// - parameter bytes: A pointer to the bytes.
/// - parameter count: The size of the bytes.
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer, or `.none`.
public init(bytesNoCopy bytes: UnsafeMutablePointer<UInt8>, count: Int, deallocator: Deallocator) {
public init(bytesNoCopy bytes: UnsafeMutableRawPointer, count: Int, deallocator: Deallocator) {
let whichDeallocator = deallocator._deallocator
_wrapped = _SwiftNSData(immutableObject: NSData(bytesNoCopy: bytes, length: count, deallocator: whichDeallocator))
}
Expand Down Expand Up @@ -348,7 +347,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
_mapUnmanaged { $0.getBytes(pointer, length: count) }
}

private func _copyBytesHelper(to pointer: UnsafeMutablePointer<UInt8>, from range: NSRange) {
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: NSRange) {
_mapUnmanaged { $0.getBytes(pointer, range: range) }
}

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

let nsRange = NSMakeRange(copyRange.lowerBound, copyRange.upperBound - copyRange.lowerBound)
let pointer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(buffer.baseAddress!)
let pointer = UnsafeMutableRawPointer(buffer.baseAddress!)
_copyBytesHelper(to: pointer, from: nsRange)
return copyRange.count
}
Expand Down
57 changes: 26 additions & 31 deletions Foundation/NSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,12 @@ public class FileManager: NSObject {
}

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

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

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

if let entryType = withUnsafePointer(to: &entry!.pointee.d_type, { (ptr) -> Int32? in
let int32Ptr = unsafeBitCast(ptr, to: UnsafePointer<UInt8>.self)
return Int32(int32Ptr.pointee)
}) {
#if os(OSX) || os(iOS)
let tempEntryType = entryType
#elseif os(Linux)
let tempEntryType = Int(entryType)
#endif
let entryType = withUnsafePointer(to: &entry!.pointee.d_type) { (ptr) -> Int32 in
return Int32(ptr.pointee)
}
#if os(OSX) || os(iOS)
let tempEntryType = entryType
#elseif os(Linux)
let tempEntryType = Int(entryType)
#endif

if tempEntryType == DT_DIR {
let subPath: String = path + "/" + entryName
if tempEntryType == DT_DIR {
let subPath: String = path + "/" + entryName

let entries = try subpathsOfDirectory(atPath: subPath)
contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
}
}
let entries = try subpathsOfDirectory(atPath: subPath)
contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
}
}

Expand Down Expand Up @@ -468,7 +463,7 @@ public class FileManager: NSObject {

let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(fsRep))
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
ps.advanced(by: 1).initialize(to: nil)
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
ps.deinitialize(count: 2)
Expand Down Expand Up @@ -914,7 +909,7 @@ extension FileManager {
if FileManager.default().fileExists(atPath: path) {
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(fsRep))
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
ps.advanced(by: 1).initialize(to: nil)
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
ps.deinitialize(count: 2)
Expand Down
9 changes: 5 additions & 4 deletions Foundation/NSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public class JSONSerialization : NSObject {
pretty: opt.contains(.prettyPrinted),
writer: { (str: String?) in
if let str = str {
result.append(UnsafePointer<UInt8>(str.cString(using: .utf8)!), count: str.lengthOfBytes(using: .utf8))
let count = str.lengthOfBytes(using: .utf8)
result.append(UnsafeRawPointer(str.cString(using: .utf8)!).bindMemory(to: UInt8.self, capacity: count), count: count)
}
}
)
Expand Down Expand Up @@ -635,11 +636,11 @@ private struct JSONReader {
func parseNumber(_ input: Index) throws -> (Any, Index)? {
func parseTypedNumber(_ address: UnsafePointer<UInt8>, count: Int) -> (Any, IndexDistance)? {
let temp_buffer_size = 64
var temp_buffer = [UInt8](repeating: 0, count: temp_buffer_size)
return temp_buffer.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<UInt8>) -> (Any, IndexDistance)? in
var temp_buffer = [Int8](repeating: 0, count: temp_buffer_size)
return temp_buffer.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<Int8>) -> (Any, IndexDistance)? in
memcpy(buffer.baseAddress!, address, min(count, temp_buffer_size - 1)) // ensure null termination

let startPointer = UnsafePointer<Int8>(buffer.baseAddress!)
let startPointer = buffer.baseAddress!
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
defer { intEndPointer.deallocate(capacity: 1) }
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
Expand Down
4 changes: 3 additions & 1 deletion Foundation/NSRegularExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ internal func _NSRegularExpressionMatch(_ context: UnsafeMutableRawPointer?, ran
#endif
matcher.block(nil, NSMatchingFlags(rawValue: opts), UnsafeMutablePointer<ObjCBool>(stop))
} else {
let result = TextCheckingResult.regularExpressionCheckingResultWithRanges(NSRangePointer(ranges!), count: count, regularExpression: matcher.regex)
let result = ranges!.withMemoryRebound(to: NSRange.self, capacity: count) { rangePtr in
TextCheckingResult.regularExpressionCheckingResultWithRanges(rangePtr, count: count, regularExpression: matcher.regex)
}
#if os(OSX) || os(iOS)
let flags = NSMatchingFlags(rawValue: options.rawValue)
#else
Expand Down
10 changes: 7 additions & 3 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ extension NSString {
var numCharsBuffered = 0
var arrayBuffer = [unichar](repeating: 0, count: 100)
let other = str._nsObject
return arrayBuffer.withUnsafeMutablePointerOrAllocation(selfLen, fastpath: UnsafeMutablePointer<unichar>(_fastContents)) { (selfChars: UnsafeMutablePointer<unichar>) -> String in
return arrayBuffer.withUnsafeMutablePointerOrAllocation(selfLen, fastpath: UnsafeMutablePointer<unichar>(mutating: _fastContents)) { (selfChars: UnsafeMutablePointer<unichar>) -> String in
// Now do the binary search. Note that the probe value determines the length of the substring to check.
while true {
let range = NSMakeRange(0, isLiteral ? probe + 1 : NSMaxRange(rangeOfComposedCharacterSequence(at: probe))) // Extend the end of the composed char sequence
Expand Down Expand Up @@ -1182,8 +1182,12 @@ extension NSString {
}

public convenience init?(UTF8String nullTerminatedCString: UnsafePointer<Int8>) {
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(nullTerminatedCString), count: Int(strlen(nullTerminatedCString)))
if let str = String._fromCodeUnitSequence(UTF8.self, input: buffer) {
let count = Int(strlen(nullTerminatedCString))
if let str = nullTerminatedCString.withMemoryRebound(to: UInt8.self, capacity: count, {
let buffer = UnsafeBufferPointer<UInt8>(start: $0, count: count)
return String._fromCodeUnitSequence(UTF8.self, input: buffer)
}) as String?
{
self.init(str)
} else {
return nil
Expand Down
4 changes: 3 additions & 1 deletion Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ public class NSURL: NSObject, NSSecureCoding, NSCopying {
/* Returns the URL's path in file system representation. File system representation is a null-terminated C string with canonical UTF-8 encoding.
*/
public func getFileSystemRepresentation(_ buffer: UnsafeMutablePointer<Int8>, maxLength maxBufferLength: Int) -> Bool {
return CFURLGetFileSystemRepresentation(_cfObject, true, UnsafeMutablePointer<UInt8>(buffer), maxBufferLength)
return buffer.withMemoryRebound(to: UInt8.self, capacity: maxBufferLength) {
CFURLGetFileSystemRepresentation(_cfObject, true, $0, maxBufferLength)
}
}

/* 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.
Expand Down
8 changes: 6 additions & 2 deletions Foundation/NSXMLElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ public class XMLElement: XMLNode {
@abstract Adds an attribute. Attributes with duplicate names are not added.
*/
public func addAttribute(_ attribute: XMLNode) {
guard _CFXMLNodeHasProp(_xmlNode, UnsafePointer<UInt8>(_CFXMLNodeGetName(attribute._xmlNode)!)) == nil else { return }
addChild(attribute)
let name = _CFXMLNodeGetName(attribute._xmlNode)!
let len = strlen(name)
name.withMemoryRebound(to: UInt8.self, capacity: Int(len)) {
guard _CFXMLNodeHasProp(_xmlNode, $0) == nil else { return }
addChild(attribute)
}
} //primitive

/*!
Expand Down
Loading