Skip to content

Commit 9809ced

Browse files
committed
Path: use isAbsolutePath instead of re-implenting
This is needed to properly handle Windows paths which are not Unix-like. Absolute paths are spelt with a prefix of `\\` or `.:`. Rely on Foundation to provide sufficient abstraction for determining if the path is absolute.
1 parent 368719c commit 9809ced

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Sources/Basic/Path.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ public struct AbsolutePath: Hashable {
101101
/// Convenience initializer that verifies that the path is absolute.
102102
public init(validating path: String) throws {
103103
switch path.first {
104-
case "/":
105-
self.init(path)
106104
case "~":
107105
throw PathValidationError.startsWithTilde(path)
108106
default:
109-
throw PathValidationError.invalidAbsolutePath(path)
107+
guard (path as NSString).isAbsolutePath else {
108+
throw PathValidationError.invalidAbsolutePath(path)
109+
}
110+
self.init(path)
110111
}
111112
}
112113

@@ -561,10 +562,7 @@ private func mayNeedNormalization(absolute string: String) -> Bool {
561562
///
562563
/// The normalization rules are as described for the AbsolutePath struct.
563564
private func normalize(absolute string: String) -> String {
564-
precondition(string.first == "/", "Failure normalizing \(string), absolute paths should start with '/'")
565-
566-
// At this point we expect to have a path separator as first character.
567-
assert(string.first == "/")
565+
precondition((string as NSString).isAbsolutePath, "Failure normalizing \(string)")
568566

569567
// Fast path.
570568
if !mayNeedNormalization(absolute: string) {

0 commit comments

Comments
 (0)