Skip to content

Commit

Permalink
Merge pull request #1161 from spevans/pr_nsregular_expression
Browse files Browse the repository at this point in the history
  • Loading branch information
swift-ci committed Aug 9, 2017
2 parents c85cbce + 6f89620 commit 3de37f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Foundation/NSRegularExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ 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.
*/

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

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

}

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

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

public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSRange {
public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions = [], range: NSRange) -> NSRange {
var firstRange = NSMakeRange(NSNotFound, 0)
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
if let match = result {
Expand All @@ -244,7 +244,7 @@ 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.
*/
public func stringByReplacingMatches(in string: String, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> String {
public func stringByReplacingMatches(in string: String, options: NSMatchingOptions = [], range: NSRange, withTemplate templ: String) -> String {
var str: String = ""
let length = string.length
var previousRange = NSMakeRange(0, 0)
Expand Down Expand Up @@ -272,7 +272,7 @@ extension NSRegularExpression {
return str
}

public func replaceMatches(in string: NSMutableString, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> Int {
public func replaceMatches(in string: NSMutableString, options: NSMatchingOptions = [], range: NSRange, withTemplate templ: String) -> Int {
let results = matches(in: string._swiftObject, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range)
var count = 0
var offset = 0
Expand Down
20 changes: 20 additions & 0 deletions TestFoundation/TestNSRegularExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TestNSRegularExpression : XCTestCase {
("test_complexRegularExpressions", test_complexRegularExpressions),
("test_Equal", test_Equal),
("test_NSCoding", test_NSCoding),
("test_defaultOptions", test_defaultOptions),
]
}

Expand Down Expand Up @@ -352,4 +353,23 @@ class TestNSRegularExpression : XCTestCase {
let regularExpressionB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: regularExpressionA)) as! NSRegularExpression
XCTAssertEqual(regularExpressionA, regularExpressionB, "Archived then unarchived `NSRegularExpression` must be equal.")
}

// Check all of the following functions do not need to be passed options:
func test_defaultOptions() {
let pattern = ".*fatal error: (.*): file (.*), line ([0-9]+)$"
let text = "fatal error: Some message: file /tmp/foo.swift, line 123"
let regex = try? NSRegularExpression(pattern: pattern)
XCTAssertNotNil(regex)
let range = NSRange(text.startIndex..., in: text)
regex!.enumerateMatches(in: text, range: range, using: {_,_,_ in })
XCTAssertEqual(regex!.matches(in: text, range: range).first?.numberOfRanges, 4)
XCTAssertEqual(regex!.numberOfMatches(in: text, range: range), 1)
XCTAssertEqual(regex!.firstMatch(in: text, range: range)?.numberOfRanges, 4)
XCTAssertEqual(regex!.rangeOfFirstMatch(in: text, range: range),
NSMakeRange(0, 56))
XCTAssertEqual(regex!.stringByReplacingMatches(in: text, range: range, withTemplate: "$1-$2-$3"),
"Some message-/tmp/foo.swift-123")
let str = NSMutableString(string: text)
XCTAssertEqual(regex!.replaceMatches(in: str, range: range, withTemplate: "$1-$2-$3"), 1)
}
}

0 comments on commit 3de37f7

Please sign in to comment.