From f826ca4b8e8fd3576d5f7c8e9ed915b858132912 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 13 Feb 2015 18:16:09 -0500 Subject: [PATCH] Fix weird optimization bug --- SwiftIO/SwiftRegex.swift | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/SwiftIO/SwiftRegex.swift b/SwiftIO/SwiftRegex.swift index 68396a7..360d051 100644 --- a/SwiftIO/SwiftRegex.swift +++ b/SwiftIO/SwiftRegex.swift @@ -93,7 +93,7 @@ public class SwiftRegex: NSObject, BooleanType { } set(newValue) { if let mutableTarget = target as? NSMutableString { - for match in matchResults().reverse() { + for match in matchResults()!.reverse() { let replacement = regex.replacementStringForResult( match, inString: target as! String, offset: 0, template: newValue ) mutableTarget.replaceCharactersInRange(match.rangeAtIndex(groupno), withString: replacement) @@ -104,25 +104,32 @@ public class SwiftRegex: NSObject, BooleanType { } } - func matchResults(options: NSMatchingOptions = nil) -> [NSTextCheckingResult] { - return regex.matchesInString(target as! String, options: options, range: targetRange) as! [NSTextCheckingResult] + func matchResults(options: NSMatchingOptions = nil) -> [NSTextCheckingResult]? { + let matches = regex.matchesInString(target as! String, options: options, range: targetRange) + as? [NSTextCheckingResult] + + if matches != nil { + return matches! + } else { + return nil + } } public func ranges(options: NSMatchingOptions = nil) -> [NSRange] { - return matchResults(options: options).map { $0.range } + return matchResults(options: options)!.map { $0.range } } public func matches(options: NSMatchingOptions = nil) -> [String] { - return matchResults(options: options).map { self.substring($0.range) } as [NSString] as! [String] + return matchResults(options: options)!.map( { self.substring($0.range) as String } ) } public func allGroups(options: NSMatchingOptions = nil) -> [[String]] { - return matchResults(options: options).map { self.groupsForMatch($0) } + return matchResults(options: options)!.map { self.groupsForMatch($0) } } public func dictionary(options: NSMatchingOptions = nil) -> Dictionary { var out = Dictionary() - for match in matchResults(options: options) { + for match in matchResults(options: options)! { out[substring(match.rangeAtIndex(1)) as! String] = substring(match.rangeAtIndex(2)) as? String }