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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
// MARK: - Platform string
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath {
/// Creates a file path by copying bytes from a null-terminated platform
/// string.
///
/// - Parameter platformString: A pointer to a null-terminated platform
/// string.
public init(platformString: UnsafePointer<CInterop.PlatformChar>) {
self.init(_platformString: platformString)
}
/// Creates a file path by copying bytes from a null-terminated platform
/// string.
///
/// - Note It is a precondition that `platformString` must be null-terminated.
/// The absence of a null byte will trigger a runtime error.
///
/// - Parameter platformString: A null-terminated platform string.
@inlinable
@_alwaysEmitIntoClient
public init(platformString: [CInterop.PlatformChar]) {
guard let _ = platformString.firstIndex(of: 0) else {
fatalError(
"input of FilePath.init(platformString:) must be null-terminated"
)
}
self = platformString.withUnsafeBufferPointer {
FilePath(platformString: $0.baseAddress!)
}
}
@inlinable
@_alwaysEmitIntoClient
@available(*, deprecated, message: "Use FilePath.init(_ scalar: Unicode.Scalar)")
public init(platformString: inout CInterop.PlatformChar) {
guard platformString == 0 else {
fatalError(
"input of FilePath.init(platformString:) must be null-terminated"
)
}
self = FilePath()
}
@inlinable
@_alwaysEmitIntoClient
@available(*, deprecated, message: "Use FilePath(_: String) to create a path from a String")
public init(platformString: String) {
if let nullLoc = platformString.firstIndex(of: "\0") {
self = FilePath(String(platformString[..<nullLoc]))
} else {
self = FilePath(platformString)
}
}
/// Calls the given closure with a pointer to the contents of the file path,
/// represented as a null-terminated platform string.
///
/// - Parameter body: A closure with a pointer parameter
/// that points to a null-terminated platform string.
/// If `body` has a return value,
/// that value is also used as the return value for this method.
/// - Returns: The return value, if any, of the `body` closure parameter.
///
/// The pointer passed as an argument to `body` is valid
/// only during the execution of this method.
/// Don't try to store the pointer for later use.
@_alwaysEmitIntoClient
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result {
// For backwards deployment, call withCString if available.
#if !os(Windows)
return try withCString(body)
#else
return try _withPlatformString(body)
#endif
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Component {
/// Creates a file path component by copying bytes from a null-terminated
/// platform string.
///
/// Returns `nil` if `platformString` is empty, is a root, or has more than
/// one component in it.
///
/// - Parameter platformString: A pointer to a null-terminated platform
/// string.
///
public init?(platformString: UnsafePointer<CInterop.PlatformChar>) {
self.init(_platformString: platformString)
}
/// Creates a file path component by copying bytes from a null-terminated
/// platform string. It is a precondition that a null byte indicates the end of
/// the string. The absence of a null byte will trigger a runtime error.
///
/// Returns `nil` if `platformString` is empty, is a root, or has more than
/// one component in it.
///
/// - Note It is a precondition that `platformString` must be null-terminated.
/// The absence of a null byte will trigger a runtime error.
///
/// - Parameter platformString: A null-terminated platform string.
@inlinable
@_alwaysEmitIntoClient
public init?(platformString: [CInterop.PlatformChar]) {
guard let _ = platformString.firstIndex(of: 0) else {
fatalError(
"input of FilePath.Component.init?(platformString:) must be null-terminated"
)
}
guard let component = platformString.withUnsafeBufferPointer({
FilePath.Component(platformString: $0.baseAddress!)
}) else {
return nil
}
self = component
}
@inlinable
@_alwaysEmitIntoClient
@available(*, deprecated, message: "Use FilePath.Component.init(_ scalar: Unicode.Scalar)")
public init?(platformString: inout CInterop.PlatformChar) {
guard platformString == 0 else {
fatalError(
"input of FilePath.Component.init?(platformString:) must be null-terminated"
)
}
return nil
}
@inlinable
@_alwaysEmitIntoClient
@available(*, deprecated, message: "Use FilePath.Component.init(_: String)")
public init?(platformString: String) {
let string: String
if let nullLoc = platformString.firstIndex(of: "\0") {
string = String(platformString[..<nullLoc])
} else {
string = platformString
}
guard let component = FilePath.Component(string) else { return nil }
self = component
}
/// Calls the given closure with a pointer to the contents of the file path
/// component, represented as a null-terminated platform string.
///
/// If this is not the last component of a path, an allocation will occur in
/// order to add the null terminator.
///
/// - Parameter body: A closure with a pointer parameter
/// that points to a null-terminated platform string.
/// If `body` has a return value,
/// that value is also used as the return value for this method.
/// - Returns: The return value, if any, of the `body` closure parameter.
///
/// The pointer passed as an argument to `body` is valid
/// only during the execution of this method.
/// Don't try to store the pointer for later use.
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result {
try _withPlatformString(body)
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Root {
/// Creates a file path root by copying bytes from a null-terminated platform
/// string.
///
/// Returns `nil` if `platformString` is empty or is not a root.
///
/// - Parameter platformString: A pointer to a null-terminated platform
/// string.
///
public init?(platformString: UnsafePointer<CInterop.PlatformChar>) {
self.init(_platformString: platformString)
}
/// Creates a file path root by copying bytes from a null-terminated platform
/// string. It is a precondition that a null byte indicates the end of
/// the string. The absence of a null byte will trigger a runtime error.
///
/// Returns `nil` if `platformString` is empty or is not a root.
///
/// - Note It is a precondition that `platformString` must be null-terminated.
/// The absence of a null byte will trigger a runtime error.
///
/// - Parameter platformString: A null-terminated platform string.
@inlinable
@_alwaysEmitIntoClient
public init?(platformString: [CInterop.PlatformChar]) {
guard let _ = platformString.firstIndex(of: 0) else {
fatalError(
"input of FilePath.Root.init?(platformString:) must be null-terminated"
)
}
guard let component = platformString.withUnsafeBufferPointer({
FilePath.Root(platformString: $0.baseAddress!)
}) else {
return nil
}
self = component
}
@inlinable
@_alwaysEmitIntoClient
@available(*, deprecated, message: "Use FilePath.Root.init(_ scalar: Unicode.Scalar)")
public init?(platformString: inout CInterop.PlatformChar) {
guard platformString == 0 else {
fatalError(
"input of FilePath.Root.init?(platformString:) must be null-terminated"
)
}
return nil
}
@inlinable
@_alwaysEmitIntoClient
@available(*, deprecated, message: "Use FilePath.Root.init(_: String)")
public init?(platformString: String) {
let string: String
if let nullLoc = platformString.firstIndex(of: "\0") {
string = String(platformString[..<nullLoc])
} else {
string = platformString
}
guard let root = FilePath.Root(string) else { return nil }
self = root
}
/// Calls the given closure with a pointer to the contents of the file path
/// root, represented as a null-terminated platform string.
///
/// If the path has a relative portion, an allocation will occur in order to
/// add the null terminator.
///
/// - Parameter body: A closure with a pointer parameter
/// that points to a null-terminated platform string.
/// If `body` has a return value,
/// that value is also used as the return value for this method.
/// - Returns: The return value, if any, of the `body` closure parameter.
///
/// The pointer passed as an argument to `body` is valid
/// only during the execution of this method.
/// Don't try to store the pointer for later use.
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result {
try _withPlatformString(body)
}
}
// MARK: - String literals
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension FilePath: ExpressibleByStringLiteral {
/// Creates a file path from a string literal.
///
/// - Parameter stringLiteral: A string literal
/// whose Unicode encoded contents to use as the contents of the path.
public init(stringLiteral: String) {
self.init(stringLiteral)
}
/// Creates a file path from a string.
///
/// - Parameter string: A string
/// whose Unicode encoded contents to use as the contents of the path.
public init(_ string: String) {
self.init(SystemString(string))
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Component: ExpressibleByStringLiteral {
/// Create a file path component from a string literal.
///
/// Precondition: `stringLiteral` is non-empty, is not a root,
/// and has only one component in it.
public init(stringLiteral: String) {
guard let s = FilePath.Component(stringLiteral) else {
// TODO: static assert
fatalError("""
FilePath.Component must be created from exactly one non-root component
""")
}
self = s
}
/// Create a file path component from a string.
///
/// Returns `nil` if `string` is empty, a root, or has more than one component
/// in it.
public init?(_ string: String) {
self.init(SystemString(string))
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Root: ExpressibleByStringLiteral {
/// Create a file path root from a string literal.
///
/// Precondition: `stringLiteral` is non-empty and is a root.
public init(stringLiteral: String) {
guard let s = FilePath.Root(stringLiteral) else {
// TODO: static assert
fatalError("""
FilePath.Root must be created from a root
""")
}
self = s
}
/// Create a file path root from a string.
///
/// Returns `nil` if `string` is empty or is not a root.
public init?(_ string: String) {
self.init(SystemString(string))
}
}
// MARK: - Printing and dumping
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension FilePath: CustomStringConvertible, CustomDebugStringConvertible {
/// A textual representation of the file path.
///
/// If the content of the path isn't a well-formed Unicode string,
/// this replaces invalid bytes with U+FFFD. See `String.init(decoding:)`
@inline(never)
public var description: String { String(decoding: self) }
/// A textual representation of the file path, suitable for debugging.
///
/// If the content of the path isn't a well-formed Unicode string,
/// this replaces invalid bytes with U+FFFD. See `String.init(decoding:)`
public var debugDescription: String { description.debugDescription }
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Component: CustomStringConvertible, CustomDebugStringConvertible {
/// A textual representation of the path component.
///
/// If the content of the path component isn't a well-formed Unicode string,
/// this replaces invalid bytes with U+FFFD. See `String.init(decoding:)`.
@inline(never)
public var description: String { String(decoding: self) }
/// A textual representation of the path component, suitable for debugging.
///
/// If the content of the path component isn't a well-formed Unicode string,
/// this replaces invalid bytes with U+FFFD. See `String.init(decoding:)`.
public var debugDescription: String { description.debugDescription }
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Root: CustomStringConvertible, CustomDebugStringConvertible {
/// A textual representation of the path root.
///
/// If the content of the path root isn't a well-formed Unicode string,
/// this replaces invalid bytes with U+FFFD. See `String.init(decoding:)`.
@inline(never)
public var description: String { String(decoding: self) }
/// A textual representation of the path root, suitable for debugging.
///
/// If the content of the path root isn't a well-formed Unicode string,
/// this replaces invalid bytes with U+FFFD. See `String.init(decoding:)`.
public var debugDescription: String { description.debugDescription }
}
// MARK: - Convenience helpers
// Convenience helpers
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath {
/// Creates a string by interpreting the path’s content as UTF-8 on Unix
/// and UTF-16 on Windows.
///
/// This property is equivalent to calling `String(decoding: path)`
public var string: String {
String(decoding: self)
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Component {
/// Creates a string by interpreting the component’s content as UTF-8 on Unix
/// and UTF-16 on Windows.
///
/// This property is equivalent to calling `String(decoding: component)`.
public var string: String {
String(decoding: self)
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension FilePath.Root {
/// On Unix, this returns `"/"`.
///
/// On Windows, interprets the root's content as UTF-16 on Windows.
///
/// This property is equivalent to calling `String(decoding: root)`.
public var string: String {
String(decoding: self)
}
}
// MARK: - Decoding and validating
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension String {
/// Creates a string by interpreting the file path's content as UTF-8 on Unix
/// and UTF-16 on Windows.
///
/// - Parameter path: The file path to be interpreted as
/// `CInterop.PlatformUnicodeEncoding`.
///
/// If the content of the file path isn't a well-formed Unicode string,
/// this initializer replaces invalid bytes with U+FFFD.
/// This means that, depending on the semantics of the specific file system,
/// conversion to a string and back to a path
/// might result in a value that's different from the original path.
public init(decoding path: FilePath) {
self.init(_decoding: path)
}
/// Creates a string from a file path, validating its contents as UTF-8 on
/// Unix and UTF-16 on Windows.
///
/// - Parameter path: The file path to be interpreted as
/// `CInterop.PlatformUnicodeEncoding`.
///
/// If the contents of the file path isn't a well-formed Unicode string,
/// this initializer returns `nil`.
public init?(validating path: FilePath) {
self.init(_validating: path)
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension String {
/// Creates a string by interpreting the path component's content as UTF-8 on
/// Unix and UTF-16 on Windows.
///
/// - Parameter component: The path component to be interpreted as
/// `CInterop.PlatformUnicodeEncoding`.
///
/// If the content of the path component isn't a well-formed Unicode string,
/// this initializer replaces invalid bytes with U+FFFD.
/// This means that, depending on the semantics of the specific file system,
/// conversion to a string and back to a path component
/// might result in a value that's different from the original path component.
public init(decoding component: FilePath.Component) {
self.init(_decoding: component)
}
/// Creates a string from a path component, validating its contents as UTF-8
/// on Unix and UTF-16 on Windows.
///
/// - Parameter component: The path component to be interpreted as
/// `CInterop.PlatformUnicodeEncoding`.
///
/// If the contents of the path component isn't a well-formed Unicode string,
/// this initializer returns `nil`.
public init?(validating component: FilePath.Component) {
self.init(_validating: component)
}
}
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
extension String {
/// On Unix, creates the string `"/"`
///
/// On Windows, creates a string by interpreting the path root's content as
/// UTF-16.
///
/// - Parameter root: The path root to be interpreted as
/// `CInterop.PlatformUnicodeEncoding`.
///
/// If the content of the path root isn't a well-formed Unicode string,
/// this initializer replaces invalid bytes with U+FFFD.
/// This means that on Windows,
/// conversion to a string and back to a path root
/// might result in a value that's different from the original path root.
public init(decoding root: FilePath.Root) {
self.init(_decoding: root)
}
/// On Unix, creates the string `"/"`
///
/// On Windows, creates a string from a path root, validating its contents as
/// UTF-16 on Windows.
///
/// - Parameter root: The path root to be interpreted as
/// `CInterop.PlatformUnicodeEncoding`.
///
/// On Windows, if the contents of the path root isn't a well-formed Unicode
/// string, this initializer returns `nil`.
public init?(validating root: FilePath.Root) {
self.init(_validating: root)
}
}
// MARK: - Internal helpers
extension String {
fileprivate init<PS: _PlatformStringable>(_decoding ps: PS) {
self = ps._withPlatformString { String(platformString: $0) }
}
fileprivate init?<PS: _PlatformStringable>(_validating ps: PS) {
guard let str = ps._withPlatformString(
String.init(validatingPlatformString:)
) else {
return nil
}
self = str
}
}
// MARK: - Deprecations
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension String {
@available(*, deprecated, renamed: "init(decoding:)")
public init(_ path: FilePath) { self.init(decoding: path) }
@available(*, deprecated, renamed: "init(validating:)")
public init?(validatingUTF8 path: FilePath) { self.init(validating: path) }
}
#if !os(Windows)
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension FilePath {
/// For backwards compatibility only. This initializer is equivalent to
/// the preferred `FilePath(platformString:)`.
@available(*, deprecated, renamed: "init(platformString:)")
public init(cString: UnsafePointer<CChar>) {
self.init(platformString: cString)
}
@available(*, deprecated, renamed: "init(platformString:)")
public init(cString: [CChar]) {
self.init(platformString: cString)
}
@available(*, deprecated, renamed: "init(platformString:)")
public init(cString: inout CChar) {
self.init(platformString: &cString)
}
@available(*, deprecated, renamed: "init(platformString:)")
public init(cString: String) {
self.init(platformString: cString)
}
/// For backwards compatibility only. This function is equivalent to
/// the preferred `withPlatformString`.
public func withCString<Result>(
_ body: (UnsafePointer<CChar>) throws -> Result
) rethrows -> Result {
return try _withPlatformString(body)
}
}
#endif
|