Skip to content

Path: use isAbsolutePath instead of re-implenting #2294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Sources/Basic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ public struct AbsolutePath: Hashable {
/// Convenience initializer that verifies that the path is absolute.
public init(validating path: String) throws {
switch path.first {
case "/":
self.init(path)
case "~":
throw PathValidationError.startsWithTilde(path)
default:
throw PathValidationError.invalidAbsolutePath(path)
guard (path as NSString).isAbsolutePath else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried about the performance impact of this. Can we measure the difference or otherwise reimplement it Windows?

https://github.com/apple/swift-corelibs-foundation/blob/c5357f39dd15ae858dc72d15bbabe5435376db70/Foundation/NSPathUtilities.swift#L120-L126

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an easy/automated way to test the performance impact?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can generate Xcode project with SwiftPM perf tests enabled using: Utilities/bootstrap --generate-xcodeproj --enable-perf-tests

Then add a perf test here and run using the PerformanceTest:
You can write a test here: https://github.com/apple/swift-package-manager/tree/master/Tests/BasicPerformanceTests

See: https://github.com/apple/swift-package-manager/blob/master/Documentation/Development.md#running-the-performance-tests

throw PathValidationError.invalidAbsolutePath(path)
}
self.init(path)
}
}

Expand Down Expand Up @@ -561,10 +562,7 @@ private func mayNeedNormalization(absolute string: String) -> Bool {
///
/// The normalization rules are as described for the AbsolutePath struct.
private func normalize(absolute string: String) -> String {
precondition(string.first == "/", "Failure normalizing \(string), absolute paths should start with '/'")

// At this point we expect to have a path separator as first character.
assert(string.first == "/")
precondition((string as NSString).isAbsolutePath, "Failure normalizing \(string)")

// Fast path.
if !mayNeedNormalization(absolute: string) {
Expand Down