Skip to content

Commit 9293cff

Browse files
committed
Merge remote-tracking branch 'origin/master' into swift-4.0-branch
2 parents ad9bfc6 + 3de37f7 commit 9293cff

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Foundation/NSRegularExpression.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ extension NSRegularExpression {
177177
/* 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.
178178
*/
179179

180-
public func enumerateMatches(in string: String, options: NSMatchingOptions, range: NSRange, using block: @escaping (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
180+
public func enumerateMatches(in string: String, options: NSMatchingOptions = [], range: NSRange, using block: @escaping (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
181181
let matcher = _NSRegularExpressionMatcher(regex: self, block: block)
182182
withExtendedLifetime(matcher) { (m: _NSRegularExpressionMatcher) -> Void in
183183
#if os(OSX) || os(iOS)
@@ -189,7 +189,7 @@ extension NSRegularExpression {
189189
}
190190
}
191191

192-
public func matches(in string: String, options: NSMatchingOptions, range: NSRange) -> [NSTextCheckingResult] {
192+
public func matches(in string: String, options: NSMatchingOptions = [], range: NSRange) -> [NSTextCheckingResult] {
193193
var matches = [NSTextCheckingResult]()
194194
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
195195
if let match = result {
@@ -200,15 +200,15 @@ extension NSRegularExpression {
200200

201201
}
202202

203-
public func numberOfMatches(in string: String, options: NSMatchingOptions, range: NSRange) -> Int {
203+
public func numberOfMatches(in string: String, options: NSMatchingOptions = [], range: NSRange) -> Int {
204204
var count = 0
205205
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion).union(.OmitResult), range: range) {_,_,_ in
206206
count += 1
207207
}
208208
return count
209209
}
210210

211-
public func firstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult? {
211+
public func firstMatch(in string: String, options: NSMatchingOptions = [], range: NSRange) -> NSTextCheckingResult? {
212212
var first: NSTextCheckingResult?
213213
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
214214
first = result
@@ -217,7 +217,7 @@ extension NSRegularExpression {
217217
return first
218218
}
219219

220-
public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSRange {
220+
public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions = [], range: NSRange) -> NSRange {
221221
var firstRange = NSMakeRange(NSNotFound, 0)
222222
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
223223
if let match = result {
@@ -244,7 +244,7 @@ extension NSRegularExpression {
244244

245245
/* 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.
246246
*/
247-
public func stringByReplacingMatches(in string: String, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> String {
247+
public func stringByReplacingMatches(in string: String, options: NSMatchingOptions = [], range: NSRange, withTemplate templ: String) -> String {
248248
var str: String = ""
249249
let length = string.length
250250
var previousRange = NSMakeRange(0, 0)
@@ -272,7 +272,7 @@ extension NSRegularExpression {
272272
return str
273273
}
274274

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

TestFoundation/TestNSRegularExpression.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class TestNSRegularExpression : XCTestCase {
2828
("test_complexRegularExpressions", test_complexRegularExpressions),
2929
("test_Equal", test_Equal),
3030
("test_NSCoding", test_NSCoding),
31+
("test_defaultOptions", test_defaultOptions),
3132
]
3233
}
3334

@@ -352,4 +353,23 @@ class TestNSRegularExpression : XCTestCase {
352353
let regularExpressionB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: regularExpressionA)) as! NSRegularExpression
353354
XCTAssertEqual(regularExpressionA, regularExpressionB, "Archived then unarchived `NSRegularExpression` must be equal.")
354355
}
356+
357+
// Check all of the following functions do not need to be passed options:
358+
func test_defaultOptions() {
359+
let pattern = ".*fatal error: (.*): file (.*), line ([0-9]+)$"
360+
let text = "fatal error: Some message: file /tmp/foo.swift, line 123"
361+
let regex = try? NSRegularExpression(pattern: pattern)
362+
XCTAssertNotNil(regex)
363+
let range = NSRange(text.startIndex..., in: text)
364+
regex!.enumerateMatches(in: text, range: range, using: {_,_,_ in })
365+
XCTAssertEqual(regex!.matches(in: text, range: range).first?.numberOfRanges, 4)
366+
XCTAssertEqual(regex!.numberOfMatches(in: text, range: range), 1)
367+
XCTAssertEqual(regex!.firstMatch(in: text, range: range)?.numberOfRanges, 4)
368+
XCTAssertEqual(regex!.rangeOfFirstMatch(in: text, range: range),
369+
NSMakeRange(0, 56))
370+
XCTAssertEqual(regex!.stringByReplacingMatches(in: text, range: range, withTemplate: "$1-$2-$3"),
371+
"Some message-/tmp/foo.swift-123")
372+
let str = NSMutableString(string: text)
373+
XCTAssertEqual(regex!.replaceMatches(in: str, range: range, withTemplate: "$1-$2-$3"), 1)
374+
}
355375
}

0 commit comments

Comments
 (0)