[xcodegen] Clean up file extension handling

Factor out `FileExtension.matches` and add some tests.
This commit is contained in:
Hamish Knight
2025-05-06 10:18:17 +01:00
parent 3022270150
commit 555bc1b54f
3 changed files with 39 additions and 13 deletions

View File

@@ -68,16 +68,11 @@ public extension PathProtocol {
return RelativePath(result)
}
func hasExtension(_ ext: FileExtension) -> Bool {
storage.extension == ext.rawValue
}
func hasExtension(_ exts: FileExtension...) -> Bool {
// Note that querying `.extension` involves re-parsing, so only do it
// once here.
guard let ext = storage.extension else { return false }
return exts.contains(where: {
ext.compare($0.rawValue, options: .caseInsensitive) == .orderedSame
})
guard let pathExt = storage.extension else { return false }
return exts.contains(where: { $0.matches(pathExt) })
}
func starts(with other: Self) -> Bool {
@@ -158,3 +153,16 @@ extension Collection where Element: PathProtocol {
return result == first ? result.parentDir : result
}
}
extension StringProtocol {
func hasExtension(_ exts: FileExtension...) -> Bool {
guard let pathExt = FilePath(String(self)).extension else { return false }
return exts.contains(where: { $0.matches(pathExt) })
}
}
extension FileExtension {
func matches(_ extStr: String) -> Bool {
rawValue.compare(extStr, options: .caseInsensitive) == .orderedSame
}
}