1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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
//
//===----------------------------------------------------------------------===//
import CxxStdlibShim
// MARK: Initializing C++ string from a Swift String
extension std.string {
/// Creates a C++ string having the same content as the given Swift string.
///
/// - Complexity: O(*n*), where *n* is the number of UTF-8 code units in the
/// Swift string.
public init(_ string: String) {
self.init()
let utf8 = string.utf8
self.reserve(utf8.count)
for char in utf8 {
self.push_back(value_type(bitPattern: char))
}
}
public init(_ string: UnsafePointer<CChar>?) {
self.init()
guard let str = string else {
return
}
let len = UTF8._nullCodeUnitOffset(in: str)
for i in 0..<len {
let char = UInt8(str[i])
self.push_back(value_type(bitPattern: char))
}
}
}
extension std.u16string {
/// Creates a C++ UTF-16 string having the same content as the given Swift
/// string.
///
/// - Complexity: O(*n*), where *n* is the number of UTF-16 code units in the
/// Swift string.
public init(_ string: String) {
self.init()
for char in string.utf16 {
self.push_back(char)
}
}
}
extension std.u32string {
/// Creates a C++ UTF-32 string having the same content as the given Swift
/// string.
///
/// - Complexity: O(*n*), where *n* is the number of UTF-32 code units in the
/// Swift string.
public init(_ string: String) {
self.init()
for char in string.unicodeScalars {
self.push_back(char)
}
}
}
// MARK: Initializing C++ string from a Swift String literal
extension std.string: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
extension std.u16string: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
extension std.u32string: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
// MARK: Concatenating and comparing C++ strings
extension std.string: Equatable {
public static func ==(lhs: std.string, rhs: std.string) -> Bool {
return lhs.compare(rhs) == 0
}
public static func +=(lhs: inout std.string, rhs: std.string) {
lhs.append(rhs)
}
@inlinable
public mutating func append(_ other: std.string) {
__appendUnsafe(other) // ignore the returned pointer
}
public static func +(lhs: std.string, rhs: std.string) -> std.string {
var copy = lhs
copy += rhs
return copy
}
}
extension std.u16string: Equatable {
public static func ==(lhs: std.u16string, rhs: std.u16string) -> Bool {
return lhs.compare(rhs) == 0
}
public static func +=(lhs: inout std.u16string, rhs: std.u16string) {
lhs.append(rhs)
}
@inlinable
public mutating func append(_ other: std.u16string) {
__appendUnsafe(other) // ignore the returned pointer
}
public static func +(lhs: std.u16string, rhs: std.u16string) -> std.u16string {
var copy = lhs
copy += rhs
return copy
}
}
extension std.u32string: Equatable {
public static func ==(lhs: std.u32string, rhs: std.u32string) -> Bool {
return lhs.compare(rhs) == 0
}
public static func +=(lhs: inout std.u32string, rhs: std.u32string) {
lhs.append(rhs)
}
@inlinable
public mutating func append(_ other: std.u32string) {
__appendUnsafe(other) // ignore the returned pointer
}
public static func +(lhs: std.u32string, rhs: std.u32string) -> std.u32string {
var copy = lhs
copy += rhs
return copy
}
}
// MARK: Hashing C++ strings
extension std.string: Hashable {
public func hash(into hasher: inout Hasher) {
// Call std::hash<std::string>::operator()
let cxxHash = __swift_interopHashOfString().callAsFunction(self)
hasher.combine(cxxHash)
}
}
extension std.u16string: Hashable {
public func hash(into hasher: inout Hasher) {
// Call std::hash<std::u16string>::operator()
let cxxHash = __swift_interopHashOfU16String().callAsFunction(self)
hasher.combine(cxxHash)
}
}
extension std.u32string: Hashable {
public func hash(into hasher: inout Hasher) {
// Call std::hash<std::u32string>::operator()
let cxxHash = __swift_interopHashOfU32String().callAsFunction(self)
hasher.combine(cxxHash)
}
}
// MARK: Getting a Swift description of a C++ string
extension std.string: CustomDebugStringConvertible {
public var debugDescription: String {
return "std.string(\(String(self)))"
}
}
extension std.u16string: CustomDebugStringConvertible {
public var debugDescription: String {
return "std.u16string(\(String(self)))"
}
}
extension std.u32string: CustomDebugStringConvertible {
public var debugDescription: String {
return "std.u32string(\(String(self)))"
}
}
extension std.string: CustomStringConvertible {
public var description: String {
return String(self)
}
}
extension std.u16string: CustomStringConvertible {
public var description: String {
return String(self)
}
}
extension std.u32string: CustomStringConvertible {
public var description: String {
return String(self)
}
}
// MARK: Initializing Swift String from a C++ string
extension String {
/// Creates a String having the same content as the given C++ string.
///
/// If `cxxString` contains ill-formed UTF-8 code unit sequences, this
/// initializer replaces them with the Unicode replacement character
/// (`"\u{FFFD}"`).
///
/// - Complexity: O(*n*), where *n* is the number of bytes in the C++ string.
public init(_ cxxString: std.string) {
let buffer = UnsafeBufferPointer<CChar>(
start: cxxString.__c_strUnsafe(),
count: cxxString.size())
self = buffer.withMemoryRebound(to: UInt8.self) {
String(decoding: $0, as: UTF8.self)
}
withExtendedLifetime(cxxString) {}
}
/// Creates a String having the same content as the given C++ UTF-16 string.
///
/// If `cxxString` contains ill-formed UTF-16 code unit sequences, this
/// initializer replaces them with the Unicode replacement character
/// (`"\u{FFFD}"`).
///
/// - Complexity: O(*n*), where *n* is the number of bytes in the C++ UTF-16
/// string.
public init(_ cxxU16String: std.u16string) {
let buffer = UnsafeBufferPointer<UInt16>(
start: cxxU16String.__dataUnsafe(),
count: cxxU16String.size())
self = String(decoding: buffer, as: UTF16.self)
withExtendedLifetime(cxxU16String) {}
}
/// Creates a String having the same content as the given C++ UTF-32 string.
///
/// If `cxxString` contains ill-formed UTF-32 code unit sequences, this
/// initializer replaces them with the Unicode replacement character
/// (`"\u{FFFD}"`).
///
/// - Complexity: O(*n*), where *n* is the number of bytes in the C++ UTF-32
/// string.
public init(_ cxxU32String: std.u32string) {
let buffer = UnsafeBufferPointer<Unicode.Scalar>(
start: cxxU32String.__dataUnsafe(),
count: cxxU32String.size())
self = buffer.withMemoryRebound(to: UInt32.self) {
String(decoding: $0, as: UTF32.self)
}
withExtendedLifetime(cxxU32String) {}
}
}
|