mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Previously, it was possible to write Unsafe[Mutable]Pointer(x) and have Swift deduce the pointee type based on context. Since reinterpreting memory is a fundamentally type-unsafe operation, it's better to be explicit about conversions from Unsafe[Mutable]Pointer<T> to Unsafe[Mutable]Pointer<U>. This change is consistent with the move from reinterpretCast(x) to unsafeBitCast(x, T.self). Also, we've encoded the operations of explicitly adding or removing mutability as properties, so that adding mutability can be separated from wild reinterpretCast'ing, a much more severe form of unsafety. Swift SVN r21324
77 lines
2.6 KiB
Swift
77 lines
2.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
// String interop with C
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
@asmname("strlen")
|
|
func _strlen(arg : UnsafePointer<CChar>) -> Int
|
|
@asmname("strcpy")
|
|
func _strcpy(dest: UnsafePointer<CChar>, src: UnsafePointer<CChar>)
|
|
-> UnsafePointer<CChar>
|
|
@asmname("strcmp")
|
|
func _strcmp(dest: UnsafePointer<CChar>, src: UnsafePointer<CChar>)
|
|
-> CInt
|
|
|
|
extension String {
|
|
/// Creates a new `String` by copying the nul-terminated UTF-8 data
|
|
/// referenced by a `CString`.
|
|
///
|
|
/// Returns `nil` if the `CString` is `NULL` or if it contains ill-formed
|
|
/// UTF-8 code unit sequences.
|
|
public static func fromCString(cs: UnsafePointer<CChar>) -> String? {
|
|
if cs._isNull {
|
|
return .None
|
|
}
|
|
let len = Int(_strlen(cs))
|
|
return String._fromCodeUnitSequence(
|
|
UTF8.self,
|
|
input: UnsafeBufferPointer(
|
|
start: cs.asPointerTo(UTF8.CodeUnit.self), count: len))
|
|
}
|
|
|
|
/// Creates a new `String` by copying the nul-terminated UTF-8 data
|
|
/// referenced by a `CString`.
|
|
///
|
|
/// Returns `nil` if the `CString` is `NULL`. If `CString` contains
|
|
/// ill-formed UTF-8 code unit sequences, replaces them with replacement
|
|
/// characters (U+FFFD).
|
|
public static func fromCStringRepairingIllFormedUTF8(
|
|
cs: UnsafePointer<CChar>)
|
|
-> (String?, hadError: Bool) {
|
|
if cs._isNull {
|
|
return (.None, hadError: false)
|
|
}
|
|
let len = Int(_strlen(cs))
|
|
let (result, hadError) = String._fromCodeUnitSequenceWithRepair(
|
|
UTF8.self,
|
|
input: UnsafeBufferPointer(
|
|
start: cs.asPointerTo(UTF8.CodeUnit.self), count: len))
|
|
return (result, hadError: hadError)
|
|
}
|
|
}
|
|
|
|
/// From a non-`nil` `UnsafePointer` to a null-terminated string
|
|
/// with possibly-transient lifetime, create a nul-terminated array of 'C' char.
|
|
/// Returns `nil` if passed a null pointer.
|
|
public func _persistCString(s: UnsafePointer<CChar>) -> [CChar]? {
|
|
if s == nil {
|
|
return .None
|
|
}
|
|
var length = _strlen(s)
|
|
var result = [CChar](count: length + 1, repeatedValue: 0)
|
|
for var i = 0; i < length; ++i {
|
|
// FIXME: this will not compile on platforms where 'CChar' is unsigned.
|
|
result[i] = s[i]
|
|
}
|
|
return result
|
|
}
|