Skip to content

FoundationEssentials: simplify path normalization #603

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

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Sources/FoundationEssentials/CodableUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ extension UInt8 {
guard _asciiNumbers.contains(self) else { return nil }
return Int(self &- UInt8(ascii: "0"))
}

internal var isLetter: Bool? {
return (0x41 ... 0x5a) ~= self || (0x61 ... 0x7a) ~= self
}
}


Expand Down
50 changes: 11 additions & 39 deletions Sources/FoundationEssentials/String/String+Internals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,23 @@ extension String {
throw CocoaError.errorWithFilePath(.fileReadInvalidFileName, "")
}

// 1. Normalize the path first.

var path = self
var iter = self.utf8.makeIterator()
let bLeadingSlash = if iter.next() == ._slash, iter.next()?.isLetter ?? false, iter.next() == ._colon { true } else { false }

// Strip the leading `/` on a RFC8089 path (`/[drive-letter]:/...` ). A
// leading slash indicates a rooted path on the drive for the current
// working directory.
var iter = path.makeIterator()
if iter.next() == "/", iter.next()?.isLetter ?? false, iter.next() == ":" {
path.removeFirst()
}

// Win32 APIs can support `/` for the arc separator. However,
// symlinks created with `/` do not resolve properly, so normalize
// the path.
path.replace("/", with: "\\")

// Drop trailing slashes unless it follows a drive specification. The
// trailing arc separator after a drive specifier indicates the root as
// opposed to a drive relative path.
while path.count > 1, path.last == "\\" {
let first = path.startIndex
let second = path.index(after: path.startIndex)
if path.count == 3, path[first].isLetter, path[second] == ":" {
break;
}
path.removeLast()
}

// 2. Perform the operation on the normalized path.

return try path.withCString(encodedAs: UTF16.self) { pwszPath in
guard !path.hasPrefix(#"\\"#) else { return try body(pwszPath) }

let dwLength = GetFullPathNameW(pwszPath, 0, nil, nil)
let path = try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
guard GetFullPathNameW(pwszPath, DWORD($0.count), $0.baseAddress, nil) == dwLength - 1 else {
throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
return try Substring(self.utf8.dropFirst(bLeadingSlash ? 1 : 0)).withCString(encodedAs: UTF16.self) { pwszPath in
// 1. Normalize the path first.
let dwLength: DWORD = GetFullPathNameW(pwszPath, 0, nil, nil)
return try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
guard GetFullPathNameW(pwszPath, DWORD($0.count), $0.baseAddress, nil) > 0 else {
throw CocoaError.errorWithFilePath(self, win32: GetLastError(), reading: true)
}
return String(decodingCString: $0.baseAddress!, as: UTF16.self)
}
guard !path.hasPrefix(#"\\"#) else {
return try path.withCString(encodedAs: UTF16.self, body)

// 2. Perform the operation on the normalized path.
return try body($0.baseAddress!)
}
return try #"\\?\\#(path)"#.withCString(encodedAs: UTF16.self, body)
}
}
}
Expand Down