Skip to content

Commit 4d38b84

Browse files
author
Pushkar N Kulkarni
committed
Adding NSRegularExpression.escapedPatternForString
Implementation of class method escapedPatternForString() which simply calls _CFRegularExpressionCreateEscapedPattern(). Also added two test to TestFoundation/TestNSRegularExpression.swift.
1 parent 2ef2e06 commit 4d38b84

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Foundation/NSRegularExpression.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public class NSRegularExpression : NSObject, NSCopying, NSCoding {
8181

8282
/* This class method will produce a string by adding backslash escapes as necessary to the given string, to escape any characters that would otherwise be treated as pattern metacharacters.
8383
*/
84-
public class func escapedPatternForString(string: String) -> String { NSUnimplemented() }
84+
public class func escapedPatternForString(string: String) -> String {
85+
return _CFRegularExpressionCreateEscapedPattern(string._cfObject)._swiftObject
86+
}
8587
}
8688

8789
public struct NSMatchingOptions : OptionSetType {

TestFoundation/TestNSRegularExpression.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class TestNSRegularExpression : XCTestCase {
157157
simpleRegularExpressionTestWithPattern(".*\\Ax", target:"xyz", looking:true, match:false)
158158
simpleRegularExpressionTestWithPattern(".*\\Ax", target:" xyz", looking:false, match:false)
159159
simpleRegularExpressionTestWithPattern("\\\\\\|\\(\\)\\[\\{\\~\\$\\*\\+\\?\\.", target:"\\|()[{~$*+?.", looking:true, match:true)
160+
simpleRegularExpressionTestWithPattern(NSRegularExpression.escapedPatternForString("+\\{}[].^$?#<=!&*()"), target:"+\\{}[].^$?#<=!&*()", looking:true, match:true)
161+
simpleRegularExpressionTestWithPattern(NSRegularExpression.escapedPatternForString("+\\{}[].^$?#<=!&*()"), target:"+\\{}[].^$?#<=!&*() abc", looking:true, match:false)
160162
}
161163

162164
func replaceRegularExpressionTest(patternString: String, _ patternOptions: NSRegularExpressionOptions, _ searchString: String, _ searchOptions: NSMatchingOptions, _ searchRange: NSRange, _ templ: String, _ numberOfMatches: Int, _ result: String, file: StaticString = #file, line: UInt = #line) {
@@ -274,4 +276,4 @@ class TestNSRegularExpression : XCTestCase {
274276
complexRegularExpressionTest("(a(b|c|d)(x|y|z)*|123)", [], "abxy", [], NSMakeRange(0, 4), 1, NSMakeRange(0, 4), NSMakeRange(0, 4), NSMakeRange(3, 1));
275277
complexRegularExpressionTest("a(b|c|d)(x|y|z)*", [], "abxy", [], NSMakeRange(0, 4), 1, NSMakeRange(0, 4), NSMakeRange(1, 1), NSMakeRange(3, 1));
276278
}
277-
}
279+
}

0 commit comments

Comments
 (0)