Files
SwiftLint/Source/SwiftLintFramework/Extensions/NSRegularExpression+SwiftLint.swift
T
Norio Nomura 97392bdc94 Add NSRegularExpression.cached(pattern:)
All rules enabled linting Carthage 0.11 with this commit:
- Persistent Bytes on termination is reduced from 584.67MB to 556.62MB.
- Duration is reduced from 22sec to 21sec.

Related: #394
2016-01-27 14:33:19 +09:00

29 lines
831 B
Swift

//
// NSRegularExpression+SwiftLint.swift
// SwiftLint
//
// Created by Scott Hoyt on 1/21/16.
// Copyright © 2016 Realm. All rights reserved.
//
import Foundation
private var regexCache = [String: NSRegularExpression]()
public extension NSRegularExpression {
public static func cached(pattern pattern: String) throws -> NSRegularExpression {
if let result = regexCache[pattern] {
return result
}
let result = try NSRegularExpression(pattern: pattern,
options: [.AnchorsMatchLines, .DotMatchesLineSeparators])
regexCache[pattern] = result
return result
}
public static func forcePattern(pattern: String) -> NSRegularExpression {
// swiftlint:disable:next force_try
return try! NSRegularExpression.cached(pattern: pattern)
}
}