Skip to content

Commit 0dc0a02

Browse files
committed
Merge remote-tracking branch 'origin/master' into swift-4.0-branch
2 parents 9293cff + 5fd6130 commit 0dc0a02

File tree

4 files changed

+44
-41
lines changed

4 files changed

+44
-41
lines changed

Foundation/NSRegularExpression.swift

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,36 @@ open class NSRegularExpression: NSObject, NSCopying, NSCoding {
114114
}
115115
}
116116

117-
public struct NSMatchingOptions : OptionSet {
118-
public let rawValue : UInt
119-
public init(rawValue: UInt) { self.rawValue = rawValue }
120-
121-
public static let reportProgress = NSMatchingOptions(rawValue: 1 << 0) /* Call the block periodically during long-running match operations. */
122-
public static let reportCompletion = NSMatchingOptions(rawValue: 1 << 1) /* Call the block once after the completion of any matching. */
123-
public static let anchored = NSMatchingOptions(rawValue: 1 << 2) /* Limit matches to those at the start of the search range. */
124-
public static let withTransparentBounds = NSMatchingOptions(rawValue: 1 << 3) /* Allow matching to look beyond the bounds of the search range. */
125-
public static let withoutAnchoringBounds = NSMatchingOptions(rawValue: 1 << 4) /* Prevent ^ and $ from automatically matching the beginning and end of the search range. */
126-
internal static let OmitResult = NSMatchingOptions(rawValue: 1 << 13)
127-
}
117+
extension NSRegularExpression {
128118

129-
public struct NSMatchingFlags : OptionSet {
130-
public let rawValue : UInt
131-
public init(rawValue: UInt) { self.rawValue = rawValue }
132-
133-
public static let progress = NSMatchingFlags(rawValue: 1 << 0) /* Set when the block is called to report progress during a long-running match operation. */
134-
public static let completed = NSMatchingFlags(rawValue: 1 << 1) /* Set when the block is called after completion of any matching. */
135-
public static let hitEnd = NSMatchingFlags(rawValue: 1 << 2) /* Set when the current match operation reached the end of the search range. */
136-
public static let requiredEnd = NSMatchingFlags(rawValue: 1 << 3) /* Set when the current match depended on the location of the end of the search range. */
137-
public static let internalError = NSMatchingFlags(rawValue: 1 << 4) /* Set when matching failed due to an internal error. */
119+
public struct MatchingOptions : OptionSet {
120+
public let rawValue : UInt
121+
public init(rawValue: UInt) { self.rawValue = rawValue }
122+
123+
public static let reportProgress = MatchingOptions(rawValue: 1 << 0) /* Call the block periodically during long-running match operations. */
124+
public static let reportCompletion = MatchingOptions(rawValue: 1 << 1) /* Call the block once after the completion of any matching. */
125+
public static let anchored = MatchingOptions(rawValue: 1 << 2) /* Limit matches to those at the start of the search range. */
126+
public static let withTransparentBounds = MatchingOptions(rawValue: 1 << 3) /* Allow matching to look beyond the bounds of the search range. */
127+
public static let withoutAnchoringBounds = MatchingOptions(rawValue: 1 << 4) /* Prevent ^ and $ from automatically matching the beginning and end of the search range. */
128+
internal static let OmitResult = MatchingOptions(rawValue: 1 << 13)
129+
}
130+
131+
public struct MatchingFlags : OptionSet {
132+
public let rawValue : UInt
133+
public init(rawValue: UInt) { self.rawValue = rawValue }
134+
135+
public static let progress = MatchingFlags(rawValue: 1 << 0) /* Set when the block is called to report progress during a long-running match operation. */
136+
public static let completed = MatchingFlags(rawValue: 1 << 1) /* Set when the block is called after completion of any matching. */
137+
public static let hitEnd = MatchingFlags(rawValue: 1 << 2) /* Set when the current match operation reached the end of the search range. */
138+
public static let requiredEnd = MatchingFlags(rawValue: 1 << 3) /* Set when the current match depended on the location of the end of the search range. */
139+
public static let internalError = MatchingFlags(rawValue: 1 << 4) /* Set when matching failed due to an internal error. */
140+
}
138141
}
139142

140143
internal class _NSRegularExpressionMatcher {
141144
var regex: NSRegularExpression
142-
var block: (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void
143-
init(regex: NSRegularExpression, block: @escaping (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void) {
145+
var block: (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void
146+
init(regex: NSRegularExpression, block: @escaping (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void) {
144147
self.regex = regex
145148
self.block = block
146149
}
@@ -155,16 +158,16 @@ internal func _NSRegularExpressionMatch(_ context: UnsafeMutableRawPointer?, ran
155158
let opts = options
156159
#endif
157160
stop.withMemoryRebound(to: ObjCBool.self, capacity: 1, {
158-
matcher.block(nil, NSMatchingFlags(rawValue: opts), $0)
161+
matcher.block(nil, NSRegularExpression.MatchingFlags(rawValue: opts), $0)
159162
})
160163
} else {
161164
let result = ranges!.withMemoryRebound(to: NSRange.self, capacity: count) { rangePtr in
162165
NSTextCheckingResult.regularExpressionCheckingResultWithRanges(rangePtr, count: count, regularExpression: matcher.regex)
163166
}
164167
#if os(OSX) || os(iOS)
165-
let flags = NSMatchingFlags(rawValue: options.rawValue)
168+
let flags = NSRegularExpression.MatchingFlags(rawValue: options.rawValue)
166169
#else
167-
let flags = NSMatchingFlags(rawValue: options)
170+
let flags = NSRegularExpression.MatchingFlags(rawValue: options)
168171
#endif
169172
stop.withMemoryRebound(to: ObjCBool.self, capacity: 1, {
170173
matcher.block(result, flags, $0)
@@ -177,7 +180,7 @@ extension NSRegularExpression {
177180
/* 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.
178181
*/
179182

180-
public func enumerateMatches(in string: String, options: NSMatchingOptions = [], range: NSRange, using block: @escaping (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
183+
public func enumerateMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, using block: @escaping (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
181184
let matcher = _NSRegularExpressionMatcher(regex: self, block: block)
182185
withExtendedLifetime(matcher) { (m: _NSRegularExpressionMatcher) -> Void in
183186
#if os(OSX) || os(iOS)
@@ -189,9 +192,9 @@ extension NSRegularExpression {
189192
}
190193
}
191194

192-
public func matches(in string: String, options: NSMatchingOptions = [], range: NSRange) -> [NSTextCheckingResult] {
195+
public func matches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> [NSTextCheckingResult] {
193196
var matches = [NSTextCheckingResult]()
194-
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
197+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
195198
if let match = result {
196199
matches.append(match)
197200
}
@@ -200,26 +203,26 @@ extension NSRegularExpression {
200203

201204
}
202205

203-
public func numberOfMatches(in string: String, options: NSMatchingOptions = [], range: NSRange) -> Int {
206+
public func numberOfMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> Int {
204207
var count = 0
205208
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion).union(.OmitResult), range: range) {_,_,_ in
206209
count += 1
207210
}
208211
return count
209212
}
210213

211-
public func firstMatch(in string: String, options: NSMatchingOptions = [], range: NSRange) -> NSTextCheckingResult? {
214+
public func firstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSTextCheckingResult? {
212215
var first: NSTextCheckingResult?
213-
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
216+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
214217
first = result
215218
stop.pointee = true
216219
}
217220
return first
218221
}
219222

220-
public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions = [], range: NSRange) -> NSRange {
223+
public func rangeOfFirstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSRange {
221224
var firstRange = NSMakeRange(NSNotFound, 0)
222-
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
225+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
223226
if let match = result {
224227
firstRange = match.range
225228
} else {
@@ -244,7 +247,7 @@ extension NSRegularExpression {
244247

245248
/* 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.
246249
*/
247-
public func stringByReplacingMatches(in string: String, options: NSMatchingOptions = [], range: NSRange, withTemplate templ: String) -> String {
250+
public func stringByReplacingMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> String {
248251
var str: String = ""
249252
let length = string.length
250253
var previousRange = NSMakeRange(0, 0)
@@ -272,7 +275,7 @@ extension NSRegularExpression {
272275
return str
273276
}
274277

275-
public func replaceMatches(in string: NSMutableString, options: NSMatchingOptions = [], range: NSRange, withTemplate templ: String) -> Int {
278+
public func replaceMatches(in string: NSMutableString, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> Int {
276279
let results = matches(in: string._swiftObject, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range)
277280
var count = 0
278281
var offset = 0

Foundation/NSString.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ extension NSString {
488488
internal func _rangeOfRegularExpressionPattern(regex pattern: String, options mask: CompareOptions, range searchRange: NSRange, locale: Locale?) -> NSRange {
489489
var matchedRange = NSMakeRange(NSNotFound, 0)
490490
let regexOptions: NSRegularExpression.Options = mask.contains(.caseInsensitive) ? .caseInsensitive : []
491-
let matchingOptions: NSMatchingOptions = mask.contains(.anchored) ? .anchored : []
491+
let matchingOptions: NSRegularExpression.MatchingOptions = mask.contains(.anchored) ? .anchored : []
492492
if let regex = _createRegexForPattern(pattern, regexOptions) {
493493
matchedRange = regex.rangeOfFirstMatch(in: _swiftObject, options: matchingOptions, range: searchRange)
494494
}
@@ -1061,7 +1061,7 @@ extension NSString {
10611061

10621062
internal func _stringByReplacingOccurrencesOfRegularExpressionPattern(_ pattern: String, withTemplate replacement: String, options: CompareOptions, range: NSRange) -> String {
10631063
let regexOptions: NSRegularExpression.Options = options.contains(.caseInsensitive) ? .caseInsensitive : []
1064-
let matchingOptions: NSMatchingOptions = options.contains(.anchored) ? .anchored : []
1064+
let matchingOptions: NSRegularExpression.MatchingOptions = options.contains(.anchored) ? .anchored : []
10651065
if let regex = _createRegexForPattern(pattern, regexOptions) {
10661066
return regex.stringByReplacingMatches(in: _swiftObject, options: matchingOptions, range: range, withTemplate: replacement)
10671067
}
@@ -1391,7 +1391,7 @@ extension NSMutableString {
13911391

13921392
internal func _replaceOccurrencesOfRegularExpressionPattern(_ pattern: String, withTemplate replacement: String, options: CompareOptions, range searchRange: NSRange) -> Int {
13931393
let regexOptions: NSRegularExpression.Options = options.contains(.caseInsensitive) ? .caseInsensitive : []
1394-
let matchingOptions: NSMatchingOptions = options.contains(.anchored) ? .anchored : []
1394+
let matchingOptions: NSRegularExpression.MatchingOptions = options.contains(.anchored) ? .anchored : []
13951395
if let regex = _createRegexForPattern(pattern, regexOptions) {
13961396
return regex.replaceMatches(in: self, options: matchingOptions, range: searchRange, withTemplate: replacement)
13971397
}

TestFoundation/TestNSRegularExpression.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class TestNSRegularExpression : XCTestCase {
165165
simpleRegularExpressionTestWithPattern(NSRegularExpression.escapedPattern(for: "+\\{}[].^$?#<=!&*()"), target:"+\\{}[].^$?#<=!&*() abc", looking:true, match:false)
166166
}
167167

168-
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) {
168+
func replaceRegularExpressionTest(_ patternString: String, _ patternOptions: NSRegularExpression.Options, _ searchString: String, _ searchOptions: NSRegularExpression.MatchingOptions, _ searchRange: NSRange, _ templ: String, _ numberOfMatches: Int, _ result: String, file: StaticString = #file, line: UInt = #line) {
169169
do {
170170
let regex = try NSRegularExpression(pattern: patternString, options: patternOptions)
171171
let mutableString = NSMutableString(string: searchString)
@@ -206,7 +206,7 @@ class TestNSRegularExpression : XCTestCase {
206206
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")
207207
}
208208

209-
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) {
209+
func complexRegularExpressionTest(_ patternString: String, _ patternOptions: NSRegularExpression.Options, _ searchString: String, _ searchOptions: NSRegularExpression.MatchingOptions, _ searchRange: NSRange, _ numberOfMatches: Int, _ firstMatchOverallRange: NSRange, _ firstMatchFirstCaptureRange: NSRange, _ firstMatchLastCaptureRange: NSRange, file: StaticString = #file, line: UInt = #line) {
210210
do {
211211
let regex = try NSRegularExpression(pattern: patternString, options: patternOptions)
212212
let matches = regex.matches(in: searchString, options: searchOptions, range: searchRange)

TestFoundation/TestNSTextCheckingResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TestNSTextCheckingResult: XCTestCase {
3030
let patternOptions: NSRegularExpression.Options = []
3131
let regex = try NSRegularExpression(pattern: patternString, options: patternOptions)
3232
let searchString = "1x030cy"
33-
let searchOptions: NSMatchingOptions = []
33+
let searchOptions: NSRegularExpression.MatchingOptions = []
3434
let searchRange = NSMakeRange(0,7)
3535
let match: NSTextCheckingResult = regex.firstMatch(in: searchString, options: searchOptions, range: searchRange)!
3636
//Positive offset

0 commit comments

Comments
 (0)