Skip to content

Commit a2ec5fb

Browse files
committed
computeDefaultName: drop trailing slashes
1 parent 5deaac3 commit a2ec5fb

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Sources/PackageModel/Package.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +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-
// Get the last path component of the URL.
141-
142140
#if os(Windows)
143141
let isSeparator : (Character) -> Bool = { $0 == "/" || $0 == "\\" }
144142
#else
145143
let isSeparator : (Character) -> Bool = { $0 == "/" }
146144
#endif
147-
let separatorIndex = url.lastIndex(where: isSeparator)
148-
let startIndex = separatorIndex.map { url.index(after: $0) } ?? url.startIndex
145+
146+
// Get the last path component of the 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+
}
149152

150-
var lastComponent = url[startIndex...]
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]
151156

152157
// Strip `.git` suffix if present.
153158
if lastComponent.hasSuffix(".git") {

0 commit comments

Comments
 (0)