Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Sources/FoundationEssentials/String/String+Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,19 @@ extension String {
}

internal var pathExtension: String {
let lastComponent = lastPathComponent.utf8
guard lastComponent.last != ._dot,
!lastComponent.starts(with: [._dot, ._dot]),
let lastDot = lastComponent.lastIndex(of: ._dot),
lastDot != lastComponent.startIndex else {
let utf8Component = lastPathComponent.utf8
guard utf8Component.last != ._dot,
let lastDot = utf8Component.lastIndex(of: ._dot),
// Don't treat a hidden file name as an extension
lastDot != utf8Component.startIndex else {
return ""
}
let result = String(lastPathComponent[lastComponent.index(after: lastDot)...])
let utf8FileName = utf8Component[..<lastDot]
// Guard against "." and ".." file names
if (utf8FileName.count == 1 || utf8FileName.count == 2) && utf8FileName.allSatisfy({ $0 == ._dot }) {
return ""
}
let result = String(lastPathComponent[utf8Component.index(after: lastDot)...])
guard validatePathExtension(result) else {
return ""
}
Expand Down
13 changes: 13 additions & 0 deletions Tests/FoundationEssentialsTests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -854,16 +854,29 @@ private struct StringTests {
#expect("/foo/bar/.zip".deletingPathExtension() == "/foo/bar/.zip")
#expect("..".deletingPathExtension() == "..")
#expect("..zip".deletingPathExtension() == "..zip")
#expect("/..".deletingPathExtension() == "/..")
#expect("/..zip".deletingPathExtension() == "/..zip")
#expect("/foo/bar/..zip".deletingPathExtension() == "/foo/bar/..zip")
#expect("/foo/bar/baz..zip".deletingPathExtension() == "/foo/bar/baz.")
#expect("...".deletingPathExtension() == "...")
#expect("...zip".deletingPathExtension() == "...zip")
#expect("/...".deletingPathExtension() == "/...")
#expect("/...zip".deletingPathExtension() == "/...zip")
#expect("/foo/bar/...zip".deletingPathExtension() == "/foo/bar/...zip")
#expect("/foo/bar/baz...zip".deletingPathExtension() == "/foo/bar/baz..")
#expect("/foo.bar/bar.baz/baz.zip".deletingPathExtension() == "/foo.bar/bar.baz/baz")
#expect("/.././.././a.zip".deletingPathExtension() == "/.././.././a")
#expect("/.././.././.".deletingPathExtension() == "/.././.././.")

// File names starting with "." or ".." are OK
// as long as they aren't exactly "." or ".."
#expect("..name.txt".deletingPathExtension() == "..name")
#expect("/..name.txt".deletingPathExtension() == "/..name")
#expect("....txt".deletingPathExtension() == "...")
#expect("/....txt".deletingPathExtension() == "/...")
#expect(".name.txt".deletingPathExtension() == ".name")
#expect("/.name.txt".deletingPathExtension() == "/.name")

#expect("path.foo".deletingPathExtension() == "path")
#expect("path.foo.zip".deletingPathExtension() == "path.foo")
#expect("/path.foo".deletingPathExtension() == "/path")
Expand Down