mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			102 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			102 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Swift
		
	
| 
											7 years ago
										 | // | ||
|  | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | import Foundation | ||
|  | 
 | ||
|  | @objc | ||
| 
											5 years ago
										 | extension NSRegularExpression { | ||
| 
											7 years ago
										 | 
 | ||
|  |     @objc | ||
|  |     public func hasMatch(input: String) -> Bool { | ||
|  |         return self.firstMatch(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) != nil | ||
|  |     } | ||
| 
											7 years ago
										 | 
 | ||
|  |     @objc | ||
| 
											5 years ago
										 |     public class func parseFirstMatch(pattern: String, text: String, options: NSRegularExpression.Options = []) -> String? { | ||
| 
											7 years ago
										 |         do { | ||
| 
											7 years ago
										 |             let regex = try NSRegularExpression(pattern: pattern, options: options) | ||
| 
											5 years ago
										 |             guard let match = regex.firstMatch(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count)) else { | ||
|  |                 return nil | ||
| 
											7 years ago
										 |             } | ||
|  |             let matchRange = match.range(at: 1) | ||
|  |             guard let textRange = Range(matchRange, in: text) else { | ||
|  |                 return nil | ||
|  |             } | ||
|  |             let substring = String(text[textRange]) | ||
|  |             return substring | ||
|  |         } catch { | ||
|  |             return nil | ||
|  |         } | ||
|  |     } | ||
| 
											7 years ago
										 | 
 | ||
|  |     @objc | ||
| 
											5 years ago
										 |     public func parseFirstMatch(inText text: String, options: NSRegularExpression.Options = []) -> String? { | ||
| 
											7 years ago
										 |         guard let match = self.firstMatch(in: text, | ||
|  |                                           options: [], | ||
|  |                                           range: NSRange(location: 0, length: text.utf16.count)) else { | ||
|  |                                             return nil | ||
|  |         } | ||
|  |         let matchRange = match.range(at: 1) | ||
|  |         guard let textRange = Range(matchRange, in: text) else { | ||
|  |             return nil | ||
|  |         } | ||
|  |         let substring = String(text[textRange]) | ||
|  |         return substring | ||
|  |     } | ||
| 
											5 years ago
										 |      | ||
|  |     @nonobjc | ||
| 
											5 years ago
										 |     public func firstMatchSet(in searchString: String) -> MatchSet? { | ||
| 
											5 years ago
										 |         firstMatch(in: searchString, options: [], range: searchString.completeNSRange)?.createMatchSet(originalSearchString: searchString) | ||
|  |     } | ||
|  | 
 | ||
|  |     @nonobjc | ||
| 
											5 years ago
										 |     public func allMatchSets(in searchString: String) -> [MatchSet] { | ||
| 
											5 years ago
										 |         matches(in: searchString, options: [], range: searchString.completeNSRange).compactMap { $0.createMatchSet(originalSearchString: searchString) } | ||
|  |     } | ||
|  |      | ||
|  | } | ||
|  | 
 | ||
|  | public struct MatchSet { | ||
| 
											5 years ago
										 |     public let fullString: Substring | ||
|  |     public let matchedGroups: [Substring?] | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 |     public func group(idx: Int) -> Substring? { | ||
| 
											5 years ago
										 |         guard idx < matchedGroups.count else { return nil } | ||
|  |         return matchedGroups[idx] | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											5 years ago
										 | extension String { | ||
|  |      | ||
|  |     public subscript(_ nsRange: NSRange) -> Substring? { | ||
| 
											5 years ago
										 |         guard let swiftRange = Range(nsRange, in: self) else { return nil } | ||
|  |         return self[swiftRange] | ||
|  |     } | ||
|  | 
 | ||
| 
											5 years ago
										 |     public var completeRange: Range<String.Index> { | ||
| 
											5 years ago
										 |         startIndex..<endIndex | ||
|  |     } | ||
|  | 
 | ||
| 
											5 years ago
										 |     public var completeNSRange: NSRange { | ||
| 
											5 years ago
										 |         NSRange(completeRange, in: self) | ||
|  |     } | ||
| 
											7 years ago
										 | } | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | extension NSTextCheckingResult { | ||
|  |      | ||
|  |     public func createMatchSet(originalSearchString string: String) -> MatchSet? { | ||
| 
											5 years ago
										 |         guard numberOfRanges > 0 else { return nil } | ||
|  |         let substrings = (0..<numberOfRanges) | ||
|  |             .map { range(at: $0) } | ||
|  |             .map { string[$0] } | ||
|  | 
 | ||
|  |         guard let fullString = substrings[0] else { | ||
|  |             return nil | ||
|  |         } | ||
|  | 
 | ||
|  |         return MatchSet(fullString: fullString, matchedGroups: Array(substrings[1...])) | ||
|  |     } | ||
|  | } | ||
|  | 
 |