mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
97 lines
2.8 KiB
Swift
97 lines
2.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
@_exported import Foundation // Clang module
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// New Strings
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
// Conversion from NSString to Swift's native representation
|
|
//
|
|
|
|
extension String {
|
|
public init(_ cocoaString: NSString) {
|
|
self = String(_cocoaString: cocoaString)
|
|
}
|
|
}
|
|
|
|
extension String : _ObjectiveCBridgeable {
|
|
@_semantics("convertToObjectiveC")
|
|
public func _bridgeToObjectiveC() -> NSString {
|
|
// This method should not do anything extra except calling into the
|
|
// implementation inside core. (These two entry points should be
|
|
// equivalent.)
|
|
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject, to: NSString.self)
|
|
}
|
|
|
|
public static func _forceBridgeFromObjectiveC(
|
|
_ x: NSString,
|
|
result: inout String?
|
|
) {
|
|
result = String(x)
|
|
}
|
|
|
|
public static func _conditionallyBridgeFromObjectiveC(
|
|
_ x: NSString,
|
|
result: inout String?
|
|
) -> Bool {
|
|
self._forceBridgeFromObjectiveC(x, result: &result)
|
|
return result != nil
|
|
}
|
|
|
|
public static func _unconditionallyBridgeFromObjectiveC(
|
|
_ source: NSString?
|
|
) -> String {
|
|
// `nil` has historically been used as a stand-in for an empty
|
|
// string; map it to an empty string.
|
|
if _slowPath(source == nil) { return String() }
|
|
return String(source!)
|
|
}
|
|
}
|
|
|
|
extension Substring : _ObjectiveCBridgeable {
|
|
@_semantics("convertToObjectiveC")
|
|
public func _bridgeToObjectiveC() -> NSString {
|
|
return String(self)._bridgeToObjectiveC()
|
|
}
|
|
|
|
public static func _forceBridgeFromObjectiveC(
|
|
_ x: NSString,
|
|
result: inout Substring?
|
|
) {
|
|
let s = String(x)
|
|
result = Substring(_base: s, s.startIndex ..< s.endIndex)
|
|
}
|
|
|
|
public static func _conditionallyBridgeFromObjectiveC(
|
|
_ x: NSString,
|
|
result: inout Substring?
|
|
) -> Bool {
|
|
self._forceBridgeFromObjectiveC(x, result: &result)
|
|
return result != nil
|
|
}
|
|
|
|
public static func _unconditionallyBridgeFromObjectiveC(
|
|
_ source: NSString?
|
|
) -> Substring {
|
|
// `nil` has historically been used as a stand-in for an empty
|
|
// string; map it to an empty substring.
|
|
if _slowPath(source == nil) { return Substring() }
|
|
let s = String(source!)
|
|
return Substring(_base: s, s.startIndex ..< s.endIndex)
|
|
}
|
|
}
|
|
|
|
extension String: CVarArg {}
|