Skip to content

Commit 5bea53a

Browse files
authored
Merge pull request #14 from milseman/route_morphology
FilePath syntactic operations
2 parents 0627d28 + d24083e commit 5bea53a

32 files changed

+5186
-274
lines changed

Sources/System/FileOperations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension FileDescriptor {
3333
permissions: FilePermissions? = nil,
3434
retryOnInterrupt: Bool = true
3535
) throws -> FileDescriptor {
36-
try path.withCString {
36+
try path.withPlatformString {
3737
try FileDescriptor.open(
3838
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
3939
}
@@ -55,7 +55,7 @@ extension FileDescriptor {
5555
/// The corresponding C function is `open`.
5656
@_alwaysEmitIntoClient
5757
public static func open(
58-
_ path: UnsafePointer<CChar>,
58+
_ path: UnsafePointer<CInterop.PlatformChar>,
5959
_ mode: FileDescriptor.AccessMode,
6060
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
6161
permissions: FilePermissions? = nil,
@@ -68,7 +68,7 @@ extension FileDescriptor {
6868

6969
@usableFromInline
7070
internal static func _open(
71-
_ path: UnsafePointer<CChar>,
71+
_ path: UnsafePointer<CInterop.PlatformChar>,
7272
_ mode: FileDescriptor.AccessMode,
7373
options: FileDescriptor.OpenOptions,
7474
permissions: FilePermissions?,

Sources/System/FilePath.swift

Lines changed: 0 additions & 210 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)