|
| 1 | +/* |
| 2 | + This source file is part of the Swift System open source project |
| 3 | + |
| 4 | + Copyright (c) 2020 Apple Inc. and the Swift System project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | +*/ |
| 9 | + |
| 10 | +/// Represents a location in the file system. |
| 11 | +/// |
| 12 | +/// This structure recognizes directory separators (e.g. `/`), roots, and |
| 13 | +/// requires that the content terminates in a NUL (`0x0`). Beyond that, it |
| 14 | +/// does not give any meaning to the bytes that it contains. The file system |
| 15 | +/// defines how the content is interpreted; for example, by its choice of string |
| 16 | +/// encoding. |
| 17 | +/// |
| 18 | +/// On construction, `FilePath` will normalize separators by removing |
| 19 | +/// reduncant intermediary separators and stripping any trailing separators. |
| 20 | +/// On Windows, `FilePath` will also normalize forward slashes `/` into |
| 21 | +/// backslashes `\`, as preferred by the platform. |
| 22 | +/// |
| 23 | +/// The code below creates a file path from a string literal, |
| 24 | +/// and then uses it to open and append to a log file: |
| 25 | +/// |
| 26 | +/// let message: String = "This is a log message." |
| 27 | +/// let path: FilePath = "/tmp/log" |
| 28 | +/// let fd = try FileDescriptor.open(path, .writeOnly, options: .append) |
| 29 | +/// try fd.closeAfter { try fd.writeAll(message.utf8) } |
| 30 | +/// |
| 31 | +/// TODO(docs): Section on all the new syntactic operations, lexical normalization, decomposition, |
| 32 | +/// components, etc. |
| 33 | +/// |
| 34 | +/// File paths conform to the |
| 35 | +/// and <doc://com.apple.documentation/documentation/swift/equatable> |
| 36 | +/// and <doc://com.apple.documentation/documentation/swift/hashable> protocols |
| 37 | +/// by performing the protocols' operations on their raw byte contents. |
| 38 | +/// This conformance allows file paths to be used, |
| 39 | +/// for example, as keys in a dictionary. |
| 40 | +/// However, the rules for path equivalence |
| 41 | +/// are file-system–specific and have additional considerations |
| 42 | +/// like case insensitivity, Unicode normalization, and symbolic links. |
| 43 | +// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) |
| 44 | +public struct FilePath { |
| 45 | + internal var _storage: SystemString |
| 46 | + |
| 47 | + /// Creates an empty, null-terminated path. |
| 48 | + public init() { |
| 49 | + self._storage = SystemString() |
| 50 | + _invariantCheck() |
| 51 | + } |
| 52 | + |
| 53 | + // In addition to the empty init, this init will properly normalize |
| 54 | + // separators. All other initializers should be implemented by |
| 55 | + // ultimately deferring to a normalizing init. |
| 56 | + internal init(_ str: SystemString) { |
| 57 | + self._storage = str |
| 58 | + self._normalizeSeparators() |
| 59 | + _invariantCheck() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) |
| 64 | +extension FilePath { |
| 65 | + /// The length of the file path, excluding the null terminator. |
| 66 | + public var length: Int { _storage.length } |
| 67 | +} |
| 68 | + |
| 69 | +// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) |
| 70 | +extension FilePath: Hashable, Codable {} |
| 71 | + |
0 commit comments