no longer need swiftregex
This commit is contained in:
parent
437c0880e8
commit
7e23597351
@ -101,11 +101,6 @@
|
|||||||
74171EC51C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
|
74171EC51C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
|
||||||
74171EC71C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
|
74171EC71C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
|
||||||
74171EC81C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
|
74171EC81C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
|
||||||
74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
|
|
||||||
74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
|
|
||||||
74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
|
|
||||||
74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
|
|
||||||
74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
|
|
||||||
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
|
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
|
||||||
74171ED01C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
|
74171ED01C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
|
||||||
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
|
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
|
||||||
@ -190,7 +185,6 @@
|
|||||||
74171E5E1C10CD240062D398 /* SocketParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketParsable.swift; path = Source/SocketParsable.swift; sourceTree = "<group>"; };
|
74171E5E1C10CD240062D398 /* SocketParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketParsable.swift; path = Source/SocketParsable.swift; sourceTree = "<group>"; };
|
||||||
74171E5F1C10CD240062D398 /* SocketStringReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketStringReader.swift; path = Source/SocketStringReader.swift; sourceTree = "<group>"; };
|
74171E5F1C10CD240062D398 /* SocketStringReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketStringReader.swift; path = Source/SocketStringReader.swift; sourceTree = "<group>"; };
|
||||||
74171E601C10CD240062D398 /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = Source/SocketTypes.swift; sourceTree = "<group>"; };
|
74171E601C10CD240062D398 /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = Source/SocketTypes.swift; sourceTree = "<group>"; };
|
||||||
74171E611C10CD240062D398 /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = Source/SwiftRegex.swift; sourceTree = "<group>"; };
|
|
||||||
74171E621C10CD240062D398 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = "<group>"; };
|
74171E621C10CD240062D398 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = "<group>"; };
|
||||||
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
|
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
|
||||||
7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketEnginePollable.swift; path = Source/SocketEnginePollable.swift; sourceTree = "<group>"; };
|
7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketEnginePollable.swift; path = Source/SocketEnginePollable.swift; sourceTree = "<group>"; };
|
||||||
@ -375,7 +369,6 @@
|
|||||||
74171E5E1C10CD240062D398 /* SocketParsable.swift */,
|
74171E5E1C10CD240062D398 /* SocketParsable.swift */,
|
||||||
74171E5F1C10CD240062D398 /* SocketStringReader.swift */,
|
74171E5F1C10CD240062D398 /* SocketStringReader.swift */,
|
||||||
74171E601C10CD240062D398 /* SocketTypes.swift */,
|
74171E601C10CD240062D398 /* SocketTypes.swift */,
|
||||||
74171E611C10CD240062D398 /* SwiftRegex.swift */,
|
|
||||||
74171E621C10CD240062D398 /* WebSocket.swift */,
|
74171E621C10CD240062D398 /* WebSocket.swift */,
|
||||||
);
|
);
|
||||||
name = Source;
|
name = Source;
|
||||||
@ -638,7 +631,6 @@
|
|||||||
74171EAB1C10CD240062D398 /* SocketLogger.swift in Sources */,
|
74171EAB1C10CD240062D398 /* SocketLogger.swift in Sources */,
|
||||||
74171E991C10CD240062D398 /* SocketIOClient.swift in Sources */,
|
74171E991C10CD240062D398 /* SocketIOClient.swift in Sources */,
|
||||||
74171E8D1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
74171E8D1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
||||||
74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */,
|
|
||||||
74171E7B1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
74171E7B1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
||||||
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */,
|
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */,
|
||||||
74171EB11C10CD240062D398 /* SocketPacket.swift in Sources */,
|
74171EB11C10CD240062D398 /* SocketPacket.swift in Sources */,
|
||||||
@ -663,7 +655,6 @@
|
|||||||
74171E8E1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
74171E8E1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
||||||
74171E7C1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
74171E7C1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
||||||
74171E821C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
|
74171E821C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
|
||||||
74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */,
|
|
||||||
74171EB21C10CD240062D398 /* SocketPacket.swift in Sources */,
|
74171EB21C10CD240062D398 /* SocketPacket.swift in Sources */,
|
||||||
741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */,
|
741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */,
|
||||||
74171EBE1C10CD240062D398 /* SocketStringReader.swift in Sources */,
|
74171EBE1C10CD240062D398 /* SocketStringReader.swift in Sources */,
|
||||||
@ -697,7 +688,6 @@
|
|||||||
74171EAD1C10CD240062D398 /* SocketLogger.swift in Sources */,
|
74171EAD1C10CD240062D398 /* SocketLogger.swift in Sources */,
|
||||||
74171E9B1C10CD240062D398 /* SocketIOClient.swift in Sources */,
|
74171E9B1C10CD240062D398 /* SocketIOClient.swift in Sources */,
|
||||||
74171E8F1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
74171E8F1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
||||||
74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */,
|
|
||||||
74171E7D1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
74171E7D1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
||||||
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */,
|
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */,
|
||||||
74171EB31C10CD240062D398 /* SocketPacket.swift in Sources */,
|
74171EB31C10CD240062D398 /* SocketPacket.swift in Sources */,
|
||||||
@ -742,7 +732,6 @@
|
|||||||
74171EAF1C10CD240062D398 /* SocketLogger.swift in Sources */,
|
74171EAF1C10CD240062D398 /* SocketLogger.swift in Sources */,
|
||||||
74171E9D1C10CD240062D398 /* SocketIOClient.swift in Sources */,
|
74171E9D1C10CD240062D398 /* SocketIOClient.swift in Sources */,
|
||||||
74171E911C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
74171E911C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
||||||
74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */,
|
|
||||||
74171E7F1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
74171E7F1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
||||||
74171ED31C10CD240062D398 /* WebSocket.swift in Sources */,
|
74171ED31C10CD240062D398 /* WebSocket.swift in Sources */,
|
||||||
74171EB51C10CD240062D398 /* SocketPacket.swift in Sources */,
|
74171EB51C10CD240062D398 /* SocketPacket.swift in Sources */,
|
||||||
@ -767,7 +756,6 @@
|
|||||||
74171E921C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
74171E921C10CD240062D398 /* SocketEventHandler.swift in Sources */,
|
||||||
74171E801C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
74171E801C10CD240062D398 /* SocketEngineClient.swift in Sources */,
|
||||||
74171E861C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
|
74171E861C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
|
||||||
74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */,
|
|
||||||
74171EB61C10CD240062D398 /* SocketPacket.swift in Sources */,
|
74171EB61C10CD240062D398 /* SocketPacket.swift in Sources */,
|
||||||
57634A2A1BD9B46D00E19CD7 /* SocketEngineTest.swift in Sources */,
|
57634A2A1BD9B46D00E19CD7 /* SocketEngineTest.swift in Sources */,
|
||||||
74171EC21C10CD240062D398 /* SocketStringReader.swift in Sources */,
|
74171EC21C10CD240062D398 /* SocketStringReader.swift in Sources */,
|
||||||
|
|||||||
@ -1,195 +0,0 @@
|
|||||||
//
|
|
||||||
// SwiftRegex.swift
|
|
||||||
// SwiftRegex
|
|
||||||
//
|
|
||||||
// Created by John Holdsworth on 26/06/2014.
|
|
||||||
// Copyright (c) 2014 John Holdsworth.
|
|
||||||
//
|
|
||||||
// $Id: //depot/SwiftRegex/SwiftRegex.swift#37 $
|
|
||||||
//
|
|
||||||
// This code is in the public domain from:
|
|
||||||
// https://github.com/johnno1962/SwiftRegex
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
infix operator <~ { associativity none precedence 130 }
|
|
||||||
|
|
||||||
private let lock = dispatch_semaphore_create(1)
|
|
||||||
private var swiftRegexCache = [String: NSRegularExpression]()
|
|
||||||
|
|
||||||
internal final class SwiftRegex : NSObject, BooleanType {
|
|
||||||
var target: String
|
|
||||||
var regex: NSRegularExpression
|
|
||||||
|
|
||||||
init(target:String, pattern:String, options:NSRegularExpressionOptions?) {
|
|
||||||
self.target = target
|
|
||||||
|
|
||||||
if dispatch_semaphore_wait(lock, dispatch_time(DISPATCH_TIME_NOW, Int64(10 * NSEC_PER_MSEC))) != 0 {
|
|
||||||
do {
|
|
||||||
let regex = try NSRegularExpression(pattern: pattern, options:
|
|
||||||
NSRegularExpressionOptions.DotMatchesLineSeparators)
|
|
||||||
self.regex = regex
|
|
||||||
} catch let error as NSError {
|
|
||||||
SwiftRegex.failure("Error in pattern: \(pattern) - \(error)")
|
|
||||||
self.regex = NSRegularExpression()
|
|
||||||
}
|
|
||||||
|
|
||||||
super.init()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if let regex = swiftRegexCache[pattern] {
|
|
||||||
self.regex = regex
|
|
||||||
} else {
|
|
||||||
do {
|
|
||||||
let regex = try NSRegularExpression(pattern: pattern, options:
|
|
||||||
NSRegularExpressionOptions.DotMatchesLineSeparators)
|
|
||||||
swiftRegexCache[pattern] = regex
|
|
||||||
self.regex = regex
|
|
||||||
} catch let error as NSError {
|
|
||||||
SwiftRegex.failure("Error in pattern: \(pattern) - \(error)")
|
|
||||||
self.regex = NSRegularExpression()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dispatch_semaphore_signal(lock)
|
|
||||||
super.init()
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func failure(message: String) {
|
|
||||||
fatalError("SwiftRegex: \(message)")
|
|
||||||
}
|
|
||||||
|
|
||||||
private var targetRange: NSRange {
|
|
||||||
return NSRange(location: 0,length: target.utf16.count)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func substring(range: NSRange) -> String? {
|
|
||||||
if range.location != NSNotFound {
|
|
||||||
return (target as NSString).substringWithRange(range)
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func doesMatch(options: NSMatchingOptions!) -> Bool {
|
|
||||||
return range(options).location != NSNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
func range(options: NSMatchingOptions) -> NSRange {
|
|
||||||
return regex.rangeOfFirstMatchInString(target as String, options: [], range: targetRange)
|
|
||||||
}
|
|
||||||
|
|
||||||
func match(options: NSMatchingOptions) -> String? {
|
|
||||||
return substring(range(options))
|
|
||||||
}
|
|
||||||
|
|
||||||
func groups() -> [String]? {
|
|
||||||
return groupsForMatch(regex.firstMatchInString(target as String, options:
|
|
||||||
NSMatchingOptions.WithoutAnchoringBounds, range: targetRange))
|
|
||||||
}
|
|
||||||
|
|
||||||
private func groupsForMatch(match: NSTextCheckingResult?) -> [String]? {
|
|
||||||
guard let match = match else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var groups = [String]()
|
|
||||||
for groupno in 0...regex.numberOfCaptureGroups {
|
|
||||||
if let group = substring(match.rangeAtIndex(groupno)) {
|
|
||||||
groups += [group]
|
|
||||||
} else {
|
|
||||||
groups += ["_"] // avoids bridging problems
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return groups
|
|
||||||
}
|
|
||||||
|
|
||||||
subscript(groupno: Int) -> String? {
|
|
||||||
get {
|
|
||||||
return groups()?[groupno]
|
|
||||||
}
|
|
||||||
|
|
||||||
set(newValue) {
|
|
||||||
if newValue == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for match in Array(matchResults().reverse()) {
|
|
||||||
let replacement = regex.replacementStringForResult(match,
|
|
||||||
inString: target as String, offset: 0, template: newValue!)
|
|
||||||
let mut = NSMutableString(string: target)
|
|
||||||
mut.replaceCharactersInRange(match.rangeAtIndex(groupno), withString: replacement)
|
|
||||||
|
|
||||||
target = mut as String
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func matchResults() -> [NSTextCheckingResult] {
|
|
||||||
let matches = regex.matchesInString(target as String, options:
|
|
||||||
NSMatchingOptions.WithoutAnchoringBounds, range: targetRange)
|
|
||||||
as [NSTextCheckingResult]
|
|
||||||
|
|
||||||
return matches
|
|
||||||
}
|
|
||||||
|
|
||||||
func ranges() -> [NSRange] {
|
|
||||||
return matchResults().map { $0.range }
|
|
||||||
}
|
|
||||||
|
|
||||||
func matches() -> [String] {
|
|
||||||
return matchResults().map( { self.substring($0.range)!})
|
|
||||||
}
|
|
||||||
|
|
||||||
func allGroups() -> [[String]?] {
|
|
||||||
return matchResults().map { self.groupsForMatch($0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
func dictionary(options: NSMatchingOptions!) -> Dictionary<String,String> {
|
|
||||||
var out = Dictionary<String,String>()
|
|
||||||
for match in matchResults() {
|
|
||||||
out[substring(match.rangeAtIndex(1))!] = substring(match.rangeAtIndex(2))!
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func substituteMatches(substitution: ((NSTextCheckingResult, UnsafeMutablePointer<ObjCBool>) -> String),
|
|
||||||
options:NSMatchingOptions) -> String {
|
|
||||||
let out = NSMutableString()
|
|
||||||
var pos = 0
|
|
||||||
|
|
||||||
regex.enumerateMatchesInString(target as String, options: options, range: targetRange ) {match, flags, stop in
|
|
||||||
let matchRange = match!.range
|
|
||||||
out.appendString( self.substring(NSRange(location:pos, length:matchRange.location-pos))!)
|
|
||||||
out.appendString( substitution(match!, stop) )
|
|
||||||
pos = matchRange.location + matchRange.length
|
|
||||||
}
|
|
||||||
|
|
||||||
out.appendString(substring(NSRange(location:pos, length:targetRange.length-pos))!)
|
|
||||||
|
|
||||||
return out as String
|
|
||||||
}
|
|
||||||
|
|
||||||
var boolValue: Bool {
|
|
||||||
return doesMatch(nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension String {
|
|
||||||
subscript(pattern: String, options: NSRegularExpressionOptions) -> SwiftRegex {
|
|
||||||
return SwiftRegex(target: self, pattern: pattern, options: options)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension String {
|
|
||||||
subscript(pattern: String) -> SwiftRegex {
|
|
||||||
return SwiftRegex(target: self, pattern: pattern, options: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func <~ (left: SwiftRegex, right: String) -> String {
|
|
||||||
return left.substituteMatches({match, stop in
|
|
||||||
return left.regex.replacementStringForResult( match,
|
|
||||||
inString: left.target as String, offset: 0, template: right )
|
|
||||||
}, options: [])
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user