in Sources/TSCBasic/Path.swift [735:768]
func suffix(withDot: Bool) -> String? {
#if os(Windows)
return self.string.withCString(encodedAs: UTF16.self) {
if let pointer = PathFindExtensionW($0) {
let substring = String(decodingCString: pointer, as: UTF16.self)
guard substring.length > 0 else { return nil }
return withDot ? substring : String(substring.dropFirst(1))
}
return nil
}
#else
// FIXME: This method seems too complicated; it should be simplified,
// if possible, and certainly optimized (using UTF8View).
// Find the last path separator, if any.
let sIdx = string.lastIndex(of: "/")
// Find the start of the basename.
let bIdx = (sIdx != nil) ? string.index(after: sIdx!) : string.startIndex
// Find the last `.` (if any), starting from the second character of
// the basename (a leading `.` does not make the whole path component
// a suffix).
let fIdx = string.index(bIdx, offsetBy: 1, limitedBy: string.endIndex) ?? string.startIndex
if let idx = string[fIdx...].lastIndex(of: ".") {
// Unless it's just a `.` at the end, we have found a suffix.
if string.distance(from: idx, to: string.endIndex) > 1 {
let fromIndex = withDot ? idx : string.index(idx, offsetBy: 1)
return String(string.suffix(from: fromIndex))
} else {
return nil
}
}
// If we get this far, there is no suffix.
return nil
#endif
}