@@ -2040,41 +2040,47 @@ extension URL {
2040
2040
/// Checks if a file path is absolute and standardizes the inputted file path on Windows
2041
2041
/// Assumes the path only contains `/` as the path separator
2042
2042
internal static func isAbsolute( standardizing filePath: inout String ) -> Bool {
2043
+ if filePath. utf8. first == . _slash {
2044
+ return true
2045
+ }
2043
2046
#if os(Windows)
2044
- var isAbsolute = false
2045
2047
let utf8 = filePath. utf8
2046
- if utf8. first == . _slash {
2047
- // Either an absolute path or a UNC path
2048
- isAbsolute = true
2049
- } else if utf8. count >= 3 {
2050
- // Check if this is a drive letter
2051
- let first = utf8. first!
2052
- let secondIndex = utf8. index ( after: utf8. startIndex)
2053
- let second = utf8 [ secondIndex]
2054
- let thirdIndex = utf8. index ( after: secondIndex)
2055
- let third = utf8 [ thirdIndex]
2056
- isAbsolute = (
2057
- first. isAlpha
2058
- && ( second == . _colon || second == . _pipe)
2059
- && third == . _slash
2060
- )
2061
-
2062
- if isAbsolute {
2063
- // Standardize to "/[drive-letter]:/..."
2064
- if second == . _pipe {
2065
- var filePathArray = Array ( utf8)
2066
- filePathArray [ 1 ] = . _colon
2067
- filePathArray. insert ( . _slash, at: 0 )
2068
- filePath = String ( decoding: filePathArray, as: UTF8 . self)
2069
- } else {
2070
- filePath = " / " + filePath
2071
- }
2048
+ guard utf8. count >= 3 else {
2049
+ return false
2050
+ }
2051
+ // Check if this is a drive letter
2052
+ let first = utf8. first!
2053
+ let secondIndex = utf8. index ( after: utf8. startIndex)
2054
+ let second = utf8 [ secondIndex]
2055
+ let thirdIndex = utf8. index ( after: secondIndex)
2056
+ let third = utf8 [ thirdIndex]
2057
+ let isAbsolute = (
2058
+ first. isAlpha
2059
+ && ( second == . _colon || second == . _pipe)
2060
+ && third == . _slash
2061
+ )
2062
+ if isAbsolute {
2063
+ // Standardize to "/[drive-letter]:/..."
2064
+ if second == . _pipe {
2065
+ var filePathArray = Array ( utf8)
2066
+ filePathArray [ 1 ] = . _colon
2067
+ filePathArray. insert ( . _slash, at: 0 )
2068
+ filePath = String ( decoding: filePathArray, as: UTF8 . self)
2069
+ } else {
2070
+ filePath = " / " + filePath
2072
2071
}
2073
2072
}
2074
- #else
2075
- let isAbsolute = filePath. utf8. first == UInt8 ( ascii: " / " ) || filePath. utf8. first == UInt8 ( ascii: " ~ " )
2076
- #endif
2077
2073
return isAbsolute
2074
+ #else // os(Windows)
2075
+ #if !NO_FILESYSTEM
2076
+ // Expand the tilde if present
2077
+ if filePath. utf8. first == UInt8 ( ascii: " ~ " ) {
2078
+ filePath = filePath. expandingTildeInPath
2079
+ }
2080
+ #endif
2081
+ // Make sure the expanded path is absolute
2082
+ return filePath. utf8. first == . _slash
2083
+ #endif // os(Windows)
2078
2084
}
2079
2085
2080
2086
/// Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.
0 commit comments