Skip to content

Commit 198059e

Browse files
committed
Keep UNC host as part of the path
1 parent 0c902cb commit 198059e

File tree

1 file changed

+5
-33
lines changed

1 file changed

+5
-33
lines changed

Sources/FoundationEssentials/URL/URL.swift

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,9 +2033,7 @@ extension URL {
20332033
var filePathArray = Array(utf8)
20342034
filePathArray[1] = UInt8(ascii: ":")
20352035
filePathArray.insert(UInt8(ascii: "\\"), at: 0)
2036-
filePath = String(unsafeUninitializedCapacity: filePathArray.count) { buffer in
2037-
buffer.initialize(fromContentsOf: filePathArray)
2038-
}
2036+
filePath = String(decoding: filePathArray, as: UTF8.self)
20392037
} else {
20402038
filePath = "\\" + filePath
20412039
}
@@ -2082,14 +2080,7 @@ extension URL {
20822080

20832081
#if os(Windows)
20842082
let slash = UInt8(ascii: "\\")
2085-
var hostName: String?
2086-
var hostEnd = path.startIndex
2087-
if path.utf8.starts(with: [slash, slash]) {
2088-
let hostStart = path.utf8.index(path.utf8.startIndex, offsetBy: 2)
2089-
hostEnd = path[hostStart...].utf8.firstIndex { $0 == slash || $0 == UInt8(ascii: "/") } ?? path.endIndex
2090-
hostName = String(path[hostStart..<hostEnd])
2091-
}
2092-
var filePath = path[hostEnd...].replacing(UInt8(ascii: "/"), with: slash)
2083+
var filePath = path.replacing(UInt8(ascii: "/"), with: slash)
20932084
#else
20942085
let slash = UInt8(ascii: "/")
20952086
var filePath = path
@@ -2104,21 +2095,12 @@ extension URL {
21042095
#endif
21052096

21062097
func absoluteFilePath() -> String {
2107-
#if os(Windows)
2108-
if let hostName {
2109-
return #"\\"# + hostName + filePath
2110-
}
2111-
#endif
21122098
guard !isAbsolute, let baseURL else {
21132099
return filePath
21142100
}
21152101
#if os(Windows)
21162102
let urlPath = filePath.replacing(UInt8(ascii: "\\"), with: UInt8(ascii: "/"))
2117-
var mergedPath = baseURL.mergedPath(for: urlPath).replacing(UInt8(ascii: "/"), with: UInt8(ascii: "\\"))
2118-
if let baseHost = baseURL.host(percentEncoded: false), !baseHost.isEmpty {
2119-
return #"\\"# + baseHost + mergedPath
2120-
}
2121-
return mergedPath
2103+
return baseURL.mergedPath(for: urlPath).replacing(UInt8(ascii: "/"), with: UInt8(ascii: "\\"))
21222104
#else
21232105
return baseURL.mergedPath(for: filePath)
21242106
#endif
@@ -2159,15 +2141,7 @@ extension URL {
21592141
var components = URLComponents()
21602142
if isAbsolute {
21612143
components.scheme = "file"
2162-
#if os(Windows)
2163-
if let hostName {
2164-
components.host = hostName
2165-
} else {
2166-
components.encodedHost = ""
2167-
}
2168-
#else
21692144
components.encodedHost = ""
2170-
#endif
21712145
}
21722146
components.path = filePath
21732147

@@ -2181,17 +2155,15 @@ extension URL {
21812155

21822156
private func appending<S: StringProtocol>(path: S, directoryHint: DirectoryHint, encodingSlashes: Bool) -> URL {
21832157
#if os(Windows)
2184-
var path = path.replacing(UInt8(ascii: "\\"), with: UInt8(ascii: "/"))
2158+
let path = path.replacing(UInt8(ascii: "\\"), with: UInt8(ascii: "/"))
21852159
#endif
21862160
guard var pathToAppend = Parser.percentEncode(path, component: .path) else {
21872161
return self
21882162
}
21892163
if encodingSlashes {
21902164
var utf8 = Array(pathToAppend.utf8)
21912165
utf8.replace([UInt8(ascii: "/")], with: [UInt8(ascii: "%"), UInt8(ascii: "2"), UInt8(ascii: "F")])
2192-
pathToAppend = String(unsafeUninitializedCapacity: utf8.count) { buffer in
2193-
buffer.initialize(fromContentsOf: utf8)
2194-
}
2166+
pathToAppend = String(decoding: utf8, as: UTF8.self)
21952167
}
21962168

21972169
let slash = UInt8(ascii: "/")

0 commit comments

Comments
 (0)