mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 14:20:20 +00:00
no message
This commit is contained in:
33
TelegramCore/Regex.swift
Normal file
33
TelegramCore/Regex.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
import Foundation
|
||||
|
||||
struct Regex {
|
||||
let pattern: String
|
||||
let options: NSRegularExpression.Options!
|
||||
|
||||
private var matcher: NSRegularExpression {
|
||||
return try! NSRegularExpression(pattern: self.pattern, options: self.options)
|
||||
}
|
||||
|
||||
init(_ pattern: String) {
|
||||
self.pattern = pattern
|
||||
self.options = []
|
||||
}
|
||||
|
||||
func match(_ string: String, options: NSRegularExpression.MatchingOptions = []) -> Bool {
|
||||
return self.matcher.numberOfMatches(in: string, options: options, range: NSMakeRange(0, string.utf16.count)) != 0
|
||||
}
|
||||
}
|
||||
|
||||
protocol RegularExpressionMatchable {
|
||||
func match(_ regex: Regex) -> Bool
|
||||
}
|
||||
|
||||
extension String: RegularExpressionMatchable {
|
||||
func match(_ regex: Regex) -> Bool {
|
||||
return regex.match(self)
|
||||
}
|
||||
}
|
||||
|
||||
func ~=<T: RegularExpressionMatchable>(pattern: Regex, matchable: T) -> Bool {
|
||||
return matchable.match(pattern)
|
||||
}
|
||||
Reference in New Issue
Block a user