Skip to content

Commit 6422633

Browse files
authored
Merge pull request #2628 from troughton/patch-1
Package.computeDefaultName: support the Windows path separator
2 parents 94d66ce + a2ec5fb commit 6422633

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Sources/PackageModel/Package.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,22 @@ public struct PackageReference: JSONMappable, JSONSerializable, Codable, CustomS
137137

138138
/// Compute the default name of a package given its URL.
139139
public static func computeDefaultName(fromURL url: String) -> String {
140+
#if os(Windows)
141+
let isSeparator : (Character) -> Bool = { $0 == "/" || $0 == "\\" }
142+
#else
143+
let isSeparator : (Character) -> Bool = { $0 == "/" }
144+
#endif
145+
140146
// Get the last path component of the URL.
141-
var lastComponent = url.split(separator: "/", omittingEmptySubsequences: true).last ?? url[...]
147+
// Drop the last character in case it's a trailing slash.
148+
var endIndex = url.endIndex
149+
if let lastCharacter = url.last, isSeparator(lastCharacter) {
150+
endIndex = url.index(before: endIndex)
151+
}
152+
153+
let separatorIndex = url[..<endIndex].lastIndex(where: isSeparator)
154+
let startIndex = separatorIndex.map { url.index(after: $0) } ?? url.startIndex
155+
var lastComponent = url[startIndex..<endIndex]
142156

143157
// Strip `.git` suffix if present.
144158
if lastComponent.hasSuffix(".git") {

0 commit comments

Comments
 (0)