diff --git a/Foundation/NSRegularExpression.swift b/Foundation/NSRegularExpression.swift index 50075abd09..5bc263d54d 100644 --- a/Foundation/NSRegularExpression.swift +++ b/Foundation/NSRegularExpression.swift @@ -12,7 +12,7 @@ import CoreFoundation -extension RegularExpression { +extension NSRegularExpression { public struct Options : OptionSet { public let rawValue : UInt public init(rawValue: UInt) { self.rawValue = rawValue } @@ -27,7 +27,7 @@ extension RegularExpression { } } -open class RegularExpression: NSObject, NSCopying, NSCoding { +open class NSRegularExpression: NSObject, NSCopying, NSCoding { internal var _internal: _CFRegularExpression open override func copy() -> Any { @@ -112,9 +112,9 @@ public struct NSMatchingFlags : OptionSet { } internal class _NSRegularExpressionMatcher { - var regex: RegularExpression + var regex: NSRegularExpression var block: (TextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer) -> Void - init(regex: RegularExpression, block: @escaping (TextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer) -> Void) { + init(regex: NSRegularExpression, block: @escaping (TextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer) -> Void) { self.regex = regex self.block = block } @@ -146,7 +146,7 @@ internal func _NSRegularExpressionMatch(_ context: UnsafeMutableRawPointer?, ran } } -extension RegularExpression { +extension NSRegularExpression { /* The fundamental matching method on NSRegularExpression is a block iterator. There are several additional convenience methods, for returning all matches at once, the number of matches, the first match, or the range of the first match. Each match is specified by an instance of NSTextCheckingResult (of type NSTextCheckingTypeRegularExpression) in which the overall match range is given by the range property (equivalent to range at:0) and any capture group ranges are given by range at: for indexes from 1 to numberOfCaptureGroups. {NSNotFound, 0} is used if a particular capture group does not participate in the match. */ @@ -214,7 +214,7 @@ NSMatchingAnchored, NSMatchingWithTransparentBounds, and NSMatchingWithoutAnchor NSRegularExpression is designed to be immutable and threadsafe, so that a single instance can be used in matching operations on multiple threads at once. However, the string on which it is operating should not be mutated during the course of a matching operation (whether from another thread or from within the block used in the iteration). */ -extension RegularExpression { +extension NSRegularExpression { /* NSRegularExpression also provides find-and-replace methods for both immutable and mutable strings. The replacement is treated as a template, with $0 being replaced by the contents of the matched range, $1 by the contents of the first capture group, and so on. Additional digits beyond the maximum required to represent the number of capture groups will be treated as ordinary characters, as will a $ not followed by digits. Backslash will escape both $ and itself. */ diff --git a/Foundation/NSString.swift b/Foundation/NSString.swift index 5d48b64cf8..32455e9185 100644 --- a/Foundation/NSString.swift +++ b/Foundation/NSString.swift @@ -101,10 +101,10 @@ extension NSString { } } -internal func _createRegexForPattern(_ pattern: String, _ options: RegularExpression.Options) -> RegularExpression? { +internal func _createRegexForPattern(_ pattern: String, _ options: NSRegularExpression.Options) -> NSRegularExpression? { struct local { - static let __NSRegularExpressionCache: NSCache = { - let cache = NSCache() + static let __NSRegularExpressionCache: NSCache = { + let cache = NSCache() cache.name = "NSRegularExpressionCache" cache.countLimit = 10 return cache @@ -115,7 +115,7 @@ internal func _createRegexForPattern(_ pattern: String, _ options: RegularExpres return regex } do { - let regex = try RegularExpression(pattern: pattern, options: options) + let regex = try NSRegularExpression(pattern: pattern, options: options) local.__NSRegularExpressionCache.setObject(regex, forKey: key._nsObject) return regex } catch { @@ -487,7 +487,7 @@ extension NSString { internal func _rangeOfRegularExpressionPattern(regex pattern: String, options mask: CompareOptions, range searchRange: NSRange, locale: Locale?) -> NSRange { var matchedRange = NSMakeRange(NSNotFound, 0) - let regexOptions: RegularExpression.Options = mask.contains(.caseInsensitive) ? .caseInsensitive : [] + let regexOptions: NSRegularExpression.Options = mask.contains(.caseInsensitive) ? .caseInsensitive : [] let matchingOptions: NSMatchingOptions = mask.contains(.anchored) ? .anchored : [] if let regex = _createRegexForPattern(pattern, regexOptions) { matchedRange = regex.rangeOfFirstMatch(in: _swiftObject, options: matchingOptions, range: searchRange) @@ -1054,7 +1054,7 @@ extension NSString { } internal func _stringByReplacingOccurrencesOfRegularExpressionPattern(_ pattern: String, withTemplate replacement: String, options: CompareOptions, range: NSRange) -> String { - let regexOptions: RegularExpression.Options = options.contains(.caseInsensitive) ? .caseInsensitive : [] + let regexOptions: NSRegularExpression.Options = options.contains(.caseInsensitive) ? .caseInsensitive : [] let matchingOptions: NSMatchingOptions = options.contains(.anchored) ? .anchored : [] if let regex = _createRegexForPattern(pattern, regexOptions) { return regex.stringByReplacingMatches(in: _swiftObject, options: matchingOptions, range: range, withTemplate: replacement) @@ -1341,7 +1341,7 @@ extension NSMutableString { } internal func _replaceOccurrencesOfRegularExpressionPattern(_ pattern: String, withTemplate replacement: String, options: CompareOptions, range searchRange: NSRange) -> Int { - let regexOptions: RegularExpression.Options = options.contains(.caseInsensitive) ? .caseInsensitive : [] + let regexOptions: NSRegularExpression.Options = options.contains(.caseInsensitive) ? .caseInsensitive : [] let matchingOptions: NSMatchingOptions = options.contains(.anchored) ? .anchored : [] if let regex = _createRegexForPattern(pattern, regexOptions) { return regex.replaceMatches(in: self, options: matchingOptions, range: searchRange, withTemplate: replacement) diff --git a/Foundation/NSTextCheckingResult.swift b/Foundation/NSTextCheckingResult.swift index 16b58f8b91..b8c2c097ba 100644 --- a/Foundation/NSTextCheckingResult.swift +++ b/Foundation/NSTextCheckingResult.swift @@ -25,7 +25,7 @@ open class TextCheckingResult: NSObject, NSCopying, NSCoding { super.init() } - open class func regularExpressionCheckingResultWithRanges(_ ranges: NSRangePointer, count: Int, regularExpression: RegularExpression) -> TextCheckingResult { + open class func regularExpressionCheckingResultWithRanges(_ ranges: NSRangePointer, count: Int, regularExpression: NSRegularExpression) -> TextCheckingResult { return _NSRegularExpressionTextCheckingResultResult(ranges: ranges, count: count, regularExpression: regularExpression) } @@ -50,14 +50,14 @@ open class TextCheckingResult: NSObject, NSCopying, NSCoding { open var range: NSRange { return range(at: 0) } /* A result must have at least one range, but may optionally have more (for example, to represent regular expression capture groups). The range at index 0 always matches the range property. Additional ranges, if any, will have indexes from 1 to numberOfRanges-1. */ open func range(at idx: Int) -> NSRange { NSUnimplemented() } - open var regularExpression: RegularExpression? { return nil } + open var regularExpression: NSRegularExpression? { return nil } open var numberOfRanges: Int { return 1 } } internal class _NSRegularExpressionTextCheckingResultResult : TextCheckingResult { var _ranges = [NSRange]() - let _regularExpression: RegularExpression - init(ranges: NSRangePointer, count: Int, regularExpression: RegularExpression) { + let _regularExpression: NSRegularExpression + init(ranges: NSRangePointer, count: Int, regularExpression: NSRegularExpression) { _regularExpression = regularExpression super.init() let notFound = NSRange(location: NSNotFound,length: 0) @@ -73,7 +73,7 @@ internal class _NSRegularExpressionTextCheckingResultResult : TextCheckingResult override var resultType: CheckingType { return .RegularExpression } override func range(at idx: Int) -> NSRange { return _ranges[idx] } override var numberOfRanges: Int { return _ranges.count } - override var regularExpression: RegularExpression? { return _regularExpression } + override var regularExpression: NSRegularExpression? { return _regularExpression } } extension TextCheckingResult { diff --git a/TestFoundation/TestNSRegularExpression.swift b/TestFoundation/TestNSRegularExpression.swift index 0c0fe11073..9e18a929e4 100644 --- a/TestFoundation/TestNSRegularExpression.swift +++ b/TestFoundation/TestNSRegularExpression.swift @@ -33,7 +33,7 @@ class TestNSRegularExpression : XCTestCase { do { let str = NSString(string: searchString) var range = NSMakeRange(0, str.length) - let regex = try RegularExpression(pattern: patternString, options: []) + let regex = try NSRegularExpression(pattern: patternString, options: []) do { let lookingRange = regex.rangeOfFirstMatch(in: searchString, options: .anchored, range: range) let matchRange = regex.rangeOfFirstMatch(in: searchString, options: [], range: range) @@ -158,13 +158,13 @@ class TestNSRegularExpression : XCTestCase { simpleRegularExpressionTestWithPattern(".*\\Ax", target:"xyz", looking:true, match:false) simpleRegularExpressionTestWithPattern(".*\\Ax", target:" xyz", looking:false, match:false) simpleRegularExpressionTestWithPattern("\\\\\\|\\(\\)\\[\\{\\~\\$\\*\\+\\?\\.", target:"\\|()[{~$*+?.", looking:true, match:true) - simpleRegularExpressionTestWithPattern(RegularExpression.escapedPattern(for: "+\\{}[].^$?#<=!&*()"), target:"+\\{}[].^$?#<=!&*()", looking:true, match:true) - simpleRegularExpressionTestWithPattern(RegularExpression.escapedPattern(for: "+\\{}[].^$?#<=!&*()"), target:"+\\{}[].^$?#<=!&*() abc", looking:true, match:false) + simpleRegularExpressionTestWithPattern(NSRegularExpression.escapedPattern(for: "+\\{}[].^$?#<=!&*()"), target:"+\\{}[].^$?#<=!&*()", looking:true, match:true) + simpleRegularExpressionTestWithPattern(NSRegularExpression.escapedPattern(for: "+\\{}[].^$?#<=!&*()"), target:"+\\{}[].^$?#<=!&*() abc", looking:true, match:false) } - func replaceRegularExpressionTest(_ patternString: String, _ patternOptions: RegularExpression.Options, _ searchString: String, _ searchOptions: NSMatchingOptions, _ searchRange: NSRange, _ templ: String, _ numberOfMatches: Int, _ result: String, file: StaticString = #file, line: UInt = #line) { + func replaceRegularExpressionTest(_ patternString: String, _ patternOptions: NSRegularExpression.Options, _ searchString: String, _ searchOptions: NSMatchingOptions, _ searchRange: NSRange, _ templ: String, _ numberOfMatches: Int, _ result: String, file: StaticString = #file, line: UInt = #line) { do { - let regex = try RegularExpression(pattern: patternString, options: patternOptions) + let regex = try NSRegularExpression(pattern: patternString, options: patternOptions) let mutableString = NSMutableString(string: searchString) let matchCount = regex.replaceMatches(in: mutableString, options: searchOptions, range: searchRange, withTemplate: templ) let replacedString = regex.stringByReplacingMatches(in: searchString, options: searchOptions, range: searchRange, withTemplate: templ) @@ -185,8 +185,8 @@ class TestNSRegularExpression : XCTestCase { replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), "*$2*", 1, "This this is ** way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), "*$10*", 1, "This this is *the0* way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), "*$*", 1, "This this is *$* way.") - replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), RegularExpression.escapedTemplate(for: "*$1*"), 1, "This this is *$1* way.") - replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), RegularExpression.escapedTemplate(for: "*\\$1*"), 1, "This this is *\\$1* way.") + replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), NSRegularExpression.escapedTemplate(for: "*$1*"), 1, "This this is *$1* way.") + replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), NSRegularExpression.escapedTemplate(for: "*\\$1*"), 1, "This this is *\\$1* way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), "*\\$1*", 1, "This this is *$1* way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", [], "This this is the the way.", [], NSMakeRange(0, 25), "*\\\\\\$1*", 1, "This this is *\\$1* way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), "$0", 2, "This this is the the way.") @@ -195,17 +195,17 @@ class TestNSRegularExpression : XCTestCase { replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), "*$2*", 2, "** is ** way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), "*$10*", 2, "*This0* is *the0* way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), "*$*", 2, "*$* is *$* way.") - replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), RegularExpression.escapedTemplate(for: "*$1*"), 2, "*$1* is *$1* way.") - replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), RegularExpression.escapedTemplate(for: "*\\$1*"), 2, "*\\$1* is *\\$1* way.") + replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), NSRegularExpression.escapedTemplate(for: "*$1*"), 2, "*$1* is *$1* way.") + replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), NSRegularExpression.escapedTemplate(for: "*\\$1*"), 2, "*\\$1* is *\\$1* way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), "*\\$1*", 2, "*$1* is *$1* way.") replaceRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "This this is the the way.", [], NSMakeRange(0, 25), "*\\\\\\$1*", 2, "*\\$1* is *\\$1* way.") replaceRegularExpressionTest("([1-9]a)([1-9]b)([1-9]c)([1-9]d)([1-9]e)([1-9]f)", [], "9a3b4c8d3e1f,9a3b4c8d3e1f", [], NSMakeRange(0,25), "$2$4 is your key", 2, "3b8d is your key,3b8d is your key") replaceRegularExpressionTest("([1-9]a)([1-9]b)([1-9]c)([1-9]d)([1-9]e)([1-9]f)([1-9]z)", [], "9a3b4c8d3e1f2z,9a3b4c8d3e1f2z", [], NSMakeRange(0,29), "$2$4$1 is your key", 2, "3b8d9a is your key,3b8d9a is your key") } - func complexRegularExpressionTest(_ patternString: String, _ patternOptions: RegularExpression.Options, _ searchString: String, _ searchOptions: NSMatchingOptions, _ searchRange: NSRange, _ numberOfMatches: Int, _ firstMatchOverallRange: NSRange, _ firstMatchFirstCaptureRange: NSRange, _ firstMatchLastCaptureRange: NSRange, file: StaticString = #file, line: UInt = #line) { + func complexRegularExpressionTest(_ patternString: String, _ patternOptions: NSRegularExpression.Options, _ searchString: String, _ searchOptions: NSMatchingOptions, _ searchRange: NSRange, _ numberOfMatches: Int, _ firstMatchOverallRange: NSRange, _ firstMatchFirstCaptureRange: NSRange, _ firstMatchLastCaptureRange: NSRange, file: StaticString = #file, line: UInt = #line) { do { - let regex = try RegularExpression(pattern: patternString, options: patternOptions) + let regex = try NSRegularExpression(pattern: patternString, options: patternOptions) let matches = regex.matches(in: searchString, options: searchOptions, range: searchRange) let matchCount = regex.numberOfMatches(in: searchString, options: searchOptions, range: searchRange) let firstResult = regex.firstMatch(in: searchString, options: searchOptions, range: searchRange) @@ -247,8 +247,8 @@ class TestNSRegularExpression : XCTestCase { complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "xThis this is the theway.", [], NSMakeRange(1, 20), 2, NSMakeRange(1, 9), NSMakeRange(1, 4), NSMakeRange(1, 4)) complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "xThis this is the theway.", .withTransparentBounds, NSMakeRange(1, 20), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) - complexRegularExpressionTest(RegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "This this is the the way.", [], NSMakeRange(0, 25), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) - complexRegularExpressionTest(RegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "x\\b(th[a-z]+) \\1\\by", [], NSMakeRange(0, 19), 1, NSMakeRange(1, 17), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) + complexRegularExpressionTest(NSRegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "This this is the the way.", [], NSMakeRange(0, 25), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) + complexRegularExpressionTest(NSRegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "x\\b(th[a-z]+) \\1\\by", [], NSMakeRange(0, 19), 1, NSMakeRange(1, 17), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .ignoreMetacharacters, "This this is the the way.", [], NSMakeRange(0, 25), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .ignoreMetacharacters, "x\\b(th[a-z]+) \\1\\by", [], NSMakeRange(0, 19), 1, NSMakeRange(1, 17), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) @@ -282,8 +282,8 @@ class TestNSRegularExpression : XCTestCase { complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "xThis this is the theway.", [], NSMakeRange(1, 20), 2, NSMakeRange(1, 9), NSMakeRange(1, 4), NSMakeRange(1, 4)) complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .caseInsensitive, "xThis this is the theway.", .withTransparentBounds, NSMakeRange(1, 20), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) - complexRegularExpressionTest(RegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "This this is the the way.", [], NSMakeRange(0, 25), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) - complexRegularExpressionTest(RegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "x\\b(th[a-z]+) \\1\\by", [], NSMakeRange(0, 19), 1, NSMakeRange(1, 17), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) + complexRegularExpressionTest(NSRegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "This this is the the way.", [], NSMakeRange(0, 25), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) + complexRegularExpressionTest(NSRegularExpression.escapedPattern(for: "\\b(th[a-z]+) \\1\\b"), [], "x\\b(th[a-z]+) \\1\\by", [], NSMakeRange(0, 19), 1, NSMakeRange(1, 17), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .ignoreMetacharacters, "This this is the the way.", [], NSMakeRange(0, 25), 0, NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) complexRegularExpressionTest("\\b(th[a-z]+) \\1\\b", .ignoreMetacharacters, "x\\b(th[a-z]+) \\1\\by", [], NSMakeRange(0, 19), 1, NSMakeRange(1, 17), NSMakeRange(NSNotFound, 0), NSMakeRange(NSNotFound, 0)) diff --git a/TestFoundation/TestNSTextCheckingResult.swift b/TestFoundation/TestNSTextCheckingResult.swift index 3019930cd2..7eabe4aa0a 100644 --- a/TestFoundation/TestNSTextCheckingResult.swift +++ b/TestFoundation/TestNSTextCheckingResult.swift @@ -27,8 +27,8 @@ class TestNSTextCheckingResult: XCTestCase { func test_textCheckingResult() { let patternString = "(a|b)x|123|(c|d)y" do { - let patternOptions: RegularExpression.Options = [] - let regex = try RegularExpression(pattern: patternString, options: patternOptions) + let patternOptions: NSRegularExpression.Options = [] + let regex = try NSRegularExpression(pattern: patternString, options: patternOptions) let searchString = "1x030cy" let searchOptions: NSMatchingOptions = [] let searchRange = NSMakeRange(0,7)