Skip to content

Commit 9485d18

Browse files
committed
Merge pull request #272 from ddunbar/onesep-fast-path
[Utility] Add a fast path for Path.onesep.
2 parents 42b9a20 + 8373e52 commit 9485d18

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Sources/Utility/Path.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,32 @@ extension String {
277277
return Path.join(self, "..").normpath
278278
}
279279

280-
/// - Returns: Ensures single path separators in a path string
280+
/// - Returns: Ensures single path separators in a path string, and removes trailing slashes.
281281
private var onesep: String {
282+
// Fast path, for already clean strings.
283+
//
284+
// It would be more efficient to avoid scrubbing every string that
285+
// passes through join(), but this retains the pre-existing semantics.
286+
func isClean(_ str: String) -> Bool {
287+
// Check if the string contains any occurrence of "//" or ends with "/".
288+
let utf8 = str.utf8
289+
var idx = utf8.startIndex
290+
let end = utf8.endIndex
291+
while idx != end {
292+
if utf8[idx] == "/".utf8.first! {
293+
idx = idx.successor()
294+
if idx == end || utf8[idx] == "/".utf8.first! {
295+
return false
296+
}
297+
}
298+
idx = idx.successor()
299+
}
300+
return true
301+
}
302+
if isClean(self) {
303+
return self
304+
}
305+
282306
let abs = isAbsolute
283307
let cleaned = characters.split(separator: "/").map(String.init).joined(separator: "/")
284308
return abs ? "/\(cleaned)" : cleaned

0 commit comments

Comments
 (0)