mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-07-31 07:30:40 +00:00

git-subtree-dir: submodules/TelegramCore git-subtree-mainline: 971273e8f8f49a47f14b251d2f35e3445a61fc3f git-subtree-split: 9561227540acef69894e6546395ab223a6233600
34 lines
945 B
Swift
34 lines
945 B
Swift
import Foundation
|
|
|
|
public struct Regex {
|
|
let pattern: String
|
|
let options: NSRegularExpression.Options!
|
|
|
|
private var matcher: NSRegularExpression {
|
|
return try! NSRegularExpression(pattern: self.pattern, options: self.options)
|
|
}
|
|
|
|
public init(_ pattern: String) {
|
|
self.pattern = pattern
|
|
self.options = []
|
|
}
|
|
|
|
public func match(_ string: String, options: NSRegularExpression.MatchingOptions = []) -> Bool {
|
|
return self.matcher.numberOfMatches(in: string, options: options, range: NSMakeRange(0, string.utf16.count)) != 0
|
|
}
|
|
}
|
|
|
|
public protocol RegularExpressionMatchable {
|
|
func match(_ regex: Regex) -> Bool
|
|
}
|
|
|
|
extension String: RegularExpressionMatchable {
|
|
public func match(_ regex: Regex) -> Bool {
|
|
return regex.match(self)
|
|
}
|
|
}
|
|
|
|
public func ~=<T: RegularExpressionMatchable>(pattern: Regex, matchable: T) -> Bool {
|
|
return matchable.match(pattern)
|
|
}
|