Use basic NSRegularExpression type with caching (#4683)

This commit is contained in:
Danny Mösch 2023-01-11 20:28:16 +01:00 committed by GitHub
parent b66dd13e2f
commit 693e504258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 40 deletions

View File

@ -1,33 +0,0 @@
import Foundation
/// Represents regex from `exclude` option in `NameConfiguration`.
/// Using NSRegularExpression causes failure on equality check between two `NameConfiguration`s.
/// This class compares pattern only when checking its equality
public final class ExcludedRegexExpression: NSObject {
/// NSRegularExpression built from given pattern
public let regex: NSRegularExpression
/// Creates an `ExcludedRegexExpression` with a pattern.
///
/// - parameter pattern: The pattern string to build regex
init?(pattern: String) {
guard let regex = try? NSRegularExpression(pattern: pattern) else { return nil }
self.regex = regex
}
// MARK: - Equality Check
/// Compares regex pattern to check equality
override public func isEqual(_ object: Any?) -> Bool {
if let object = object as? ExcludedRegexExpression {
return regex.pattern == object.regex.pattern
} else {
return false
}
}
/// Uses regex pattern as hash
override public var hash: Int {
return regex.pattern.hashValue
}
}

View File

@ -4,14 +4,14 @@ struct NameConfiguration: RuleConfiguration, Equatable {
var consoleDescription: String {
return "(min_length) \(minLength.shortConsoleDescription), " +
"(max_length) \(maxLength.shortConsoleDescription), " +
"excluded: \(excludedRegularExpressions.map { $0.regex.pattern }.sorted()), " +
"excluded: \(excludedRegularExpressions.map { $0.pattern }.sorted()), " +
"allowed_symbols: \(allowedSymbolsSet.sorted()), " +
"validates_start_with_lowercase: \(validatesStartWithLowercase)"
}
var minLength: SeverityLevelsConfiguration
var maxLength: SeverityLevelsConfiguration
var excludedRegularExpressions: Set<ExcludedRegexExpression>
var excludedRegularExpressions: Set<NSRegularExpression>
private var allowedSymbolsSet: Set<String>
var validatesStartWithLowercase: Bool
@ -36,7 +36,9 @@ struct NameConfiguration: RuleConfiguration, Equatable {
validatesStartWithLowercase: Bool = true) {
minLength = SeverityLevelsConfiguration(warning: minLengthWarning, error: minLengthError)
maxLength = SeverityLevelsConfiguration(warning: maxLengthWarning, error: maxLengthError)
self.excludedRegularExpressions = Set(excluded.compactMap { ExcludedRegexExpression(pattern: "^\($0)$") })
self.excludedRegularExpressions = Set(excluded.compactMap {
try? NSRegularExpression.cached(pattern: "^\($0)$")
})
self.allowedSymbolsSet = Set(allowedSymbols)
self.validatesStartWithLowercase = validatesStartWithLowercase
}
@ -53,7 +55,9 @@ struct NameConfiguration: RuleConfiguration, Equatable {
try maxLength.apply(configuration: maxLengthConfiguration)
}
if let excluded = [String].array(of: configurationDict["excluded"]) {
self.excludedRegularExpressions = Set(excluded.compactMap { ExcludedRegexExpression(pattern: "^\($0)$") })
self.excludedRegularExpressions = Set(excluded.compactMap {
try? NSRegularExpression.cached(pattern: "^\($0)$")
})
}
if let allowedSymbols = [String].array(of: configurationDict["allowed_symbols"]) {
self.allowedSymbolsSet = Set(allowedSymbols)
@ -95,8 +99,8 @@ extension NameConfiguration {
extension NameConfiguration {
func shouldExclude(name: String) -> Bool {
return excludedRegularExpressions.contains(where: {
return !$0.regex.matches(in: name, options: [], range: NSRange(name.startIndex..., in: name)).isEmpty
})
excludedRegularExpressions.contains {
!$0.matches(in: name, options: [], range: NSRange(name.startIndex..., in: name)).isEmpty
}
}
}