Files
SwiftLint/Source/SwiftLintFramework/Helpers/Glob.swift
T
Keith Smiley 2515c0f936 flatMap it
2018-07-26 10:21:13 -07:00

36 lines
817 B
Swift

#if os(Linux)
import Glibc
let globFunction = Glibc.glob
#else
import Darwin
let globFunction = Darwin.glob
#endif
struct Glob {
static func resolveGlob(_ pattern: String) -> [String] {
guard pattern.contains("*") else {
return [pattern]
}
var globResult = glob_t()
defer { globfree(&globResult) }
let flags = GLOB_TILDE | GLOB_BRACE | GLOB_MARK
guard globFunction(pattern.cString(using: .utf8)!, flags, nil, &globResult) == 0 else {
return []
}
#if os(Linux)
let matchCount = globResult.gl_pathc
#else
let matchCount = globResult.gl_matchc
#endif
return (0..<Int(matchCount)).compactMap { index in
return globResult.gl_pathv[index].flatMap { String(validatingUTF8: $0) }
}
}
}