Allow initialization from Substring etc.
This commit is contained in:
@@ -53,11 +53,25 @@ public struct Path: Equatable, Hashable, Comparable {
|
|||||||
- Note: On macOS, removes an initial component of “/private/var/automount”, “/var/automount”, or “/private” from the path, if the result still indicates an existing file or directory (checked by consulting the file system).
|
- Note: On macOS, removes an initial component of “/private/var/automount”, “/var/automount”, or “/private” from the path, if the result still indicates an existing file or directory (checked by consulting the file system).
|
||||||
- Returns: The path or `nil` if fed a relative path or a `~foo` string where there is no user `foo`.
|
- Returns: The path or `nil` if fed a relative path or a `~foo` string where there is no user `foo`.
|
||||||
*/
|
*/
|
||||||
public init?(_ description: String) {
|
public init?<S: StringProtocol>(_ description: S) {
|
||||||
var pathComponents = description.split(separator: "/")
|
var pathComponents = description.split(separator: "/")
|
||||||
switch description.first {
|
switch description.first {
|
||||||
case "/":
|
case "/":
|
||||||
break
|
#if os(macOS)
|
||||||
|
func ifExists(withPrefix prefix: String, removeFirst n: Int) {
|
||||||
|
assert(prefix.split(separator: "/").count == n)
|
||||||
|
|
||||||
|
if description.hasPrefix(prefix), FileManager.default.fileExists(atPath: String(description)) {
|
||||||
|
pathComponents.removeFirst(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ifExists(withPrefix: "/private/var/automount", removeFirst: 3)
|
||||||
|
ifExists(withPrefix: "/var/automount", removeFirst: 2)
|
||||||
|
ifExists(withPrefix: "/private", removeFirst: 1)
|
||||||
|
#endif
|
||||||
|
self.string = join_(prefix: "/", pathComponents: pathComponents)
|
||||||
|
|
||||||
case "~":
|
case "~":
|
||||||
if description == "~" {
|
if description == "~" {
|
||||||
self = Path.home
|
self = Path.home
|
||||||
@@ -82,26 +96,11 @@ public struct Path: Equatable, Hashable, Comparable {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
pathComponents.remove(at: 0)
|
pathComponents.remove(at: 0)
|
||||||
pathComponents.insert(contentsOf: tilded.split(separator: "/"), at: 0)
|
self.string = join_(prefix: tilded, pathComponents: pathComponents)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(macOS)
|
|
||||||
func ifExists(withPrefix prefix: String, removeFirst n: Int) {
|
|
||||||
assert(prefix.split(separator: "/").count == n)
|
|
||||||
|
|
||||||
if description.hasPrefix(prefix), FileManager.default.fileExists(atPath: description) {
|
|
||||||
pathComponents.removeFirst(n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ifExists(withPrefix: "/private/var/automount", removeFirst: 3)
|
|
||||||
ifExists(withPrefix: "/var/automount", removeFirst: 2)
|
|
||||||
ifExists(withPrefix: "/private", removeFirst: 1)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
self.string = join_(prefix: "/", pathComponents: pathComponents)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -594,4 +594,14 @@ class PathTests: XCTestCase {
|
|||||||
XCTAssertEqual(Path.root.foo.bar.components, ["/", "foo", "bar"])
|
XCTAssertEqual(Path.root.foo.bar.components, ["/", "foo", "bar"])
|
||||||
XCTAssertEqual(Path.root.components, ["/"])
|
XCTAssertEqual(Path.root.components, ["/"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testFlatMap() throws {
|
||||||
|
// testing compile works
|
||||||
|
let foo: String? = "/a"
|
||||||
|
_ = foo.flatMap(Path.init)
|
||||||
|
let bar: Substring? = "/a"
|
||||||
|
_ = bar.flatMap(Path.init)
|
||||||
|
let baz: String.SubSequence? = "/a/b:1".split(separator: ":").first
|
||||||
|
_ = baz.flatMap(Path.init)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
|
#if !canImport(ObjectiveC)
|
||||||
import XCTest
|
import XCTest
|
||||||
|
|
||||||
extension PathTests {
|
extension PathTests {
|
||||||
static let __allTests = [
|
// DO NOT MODIFY: This is autogenerated, use:
|
||||||
|
// `swift test --generate-linuxmain`
|
||||||
|
// to regenerate.
|
||||||
|
static let __allTests__PathTests = [
|
||||||
("testBasename", testBasename),
|
("testBasename", testBasename),
|
||||||
("testBundleExtensions", testBundleExtensions),
|
("testBundleExtensions", testBundleExtensions),
|
||||||
("testCodable", testCodable),
|
("testCodable", testCodable),
|
||||||
@@ -19,6 +23,7 @@ extension PathTests {
|
|||||||
("testFileHandleExtensions", testFileHandleExtensions),
|
("testFileHandleExtensions", testFileHandleExtensions),
|
||||||
("testFileReference", testFileReference),
|
("testFileReference", testFileReference),
|
||||||
("testFilesystemAttributes", testFilesystemAttributes),
|
("testFilesystemAttributes", testFilesystemAttributes),
|
||||||
|
("testFlatMap", testFlatMap),
|
||||||
("testInitializerForRelativePath", testInitializerForRelativePath),
|
("testInitializerForRelativePath", testInitializerForRelativePath),
|
||||||
("testIsDirectory", testIsDirectory),
|
("testIsDirectory", testIsDirectory),
|
||||||
("testJoin", testJoin),
|
("testJoin", testJoin),
|
||||||
@@ -48,10 +53,9 @@ extension PathTests {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !os(macOS)
|
|
||||||
public func __allTests() -> [XCTestCaseEntry] {
|
public func __allTests() -> [XCTestCaseEntry] {
|
||||||
return [
|
return [
|
||||||
testCase(PathTests.__allTests),
|
testCase(PathTests.__allTests__PathTests),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user