Created
December 29, 2014 22:35
-
-
Save mattt/3f12f56d72b8d2ebbe62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Regex { | |
let pattern: String | |
let options: NSRegularExpressionOptions! | |
private var matcher: NSRegularExpression { | |
return NSRegularExpression(pattern: self.pattern, options: self.options, error: nil) | |
} | |
init(pattern: String, options: NSRegularExpressionOptions = nil) { | |
self.pattern = pattern | |
self.options = options | |
} | |
func match(string: String, options: NSMatchingOptions = nil) -> Bool { | |
return self.matcher.numberOfMatchesInString(string, options: options, range: NSMakeRange(0, string.utf16Count)) != 0 | |
} | |
} | |
extension Regex: StringLiteralConvertible { | |
typealias ExtendedGraphemeClusterLiteralType = StringLiteralType | |
init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { | |
self.pattern = "\(value)" | |
} | |
init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { | |
self.pattern = value | |
} | |
init(stringLiteral value: StringLiteralType) { | |
self.pattern = value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made redundant with the introduction of the following for Strings
Use the following instead