mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
48 lines
1.3 KiB
Swift
48 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
#if os(Linux)
|
|
#if !swift(>=4.0)
|
|
public typealias NSTextCheckingResult = TextCheckingResult
|
|
#endif
|
|
#else
|
|
#if !swift(>=4.0)
|
|
extension NSTextCheckingResult {
|
|
internal func range(at idx: Int) -> NSRange {
|
|
return rangeAt(idx)
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
private var regexCache = [RegexCacheKey: NSRegularExpression]()
|
|
private let regexCacheLock = NSLock()
|
|
|
|
private struct RegexCacheKey: Hashable {
|
|
let pattern: String
|
|
let options: NSRegularExpression.Options
|
|
|
|
var hashValue: Int {
|
|
return pattern.hashValue ^ options.rawValue.hashValue
|
|
}
|
|
|
|
static func == (lhs: RegexCacheKey, rhs: RegexCacheKey) -> Bool {
|
|
return lhs.options == rhs.options && lhs.pattern == rhs.pattern
|
|
}
|
|
}
|
|
|
|
extension NSRegularExpression {
|
|
internal static func cached(pattern: String, options: Options? = nil) throws -> NSRegularExpression {
|
|
let options = options ?? [.anchorsMatchLines, .dotMatchesLineSeparators]
|
|
let key = RegexCacheKey(pattern: pattern, options: options)
|
|
regexCacheLock.lock()
|
|
defer { regexCacheLock.unlock() }
|
|
if let result = regexCache[key] {
|
|
return result
|
|
}
|
|
|
|
let result = try NSRegularExpression(pattern: pattern, options: options)
|
|
regexCache[key] = result
|
|
return result
|
|
}
|
|
}
|