Skip to content

Commit fca0a34

Browse files
authored
Merge pull request swiftlang#870 from naithar/nscoding-regularexpression
2 parents 78a22e6 + d3ae89a commit fca0a34

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

Docs/Status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ There is no _Complete_ status for test coverage because there are always additio
196196
197197
| Entity Name | Status | Test Coverage | Notes |
198198
|-----------------------------|-----------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
199-
| `RegularExpression` | Mostly Complete | Substantial | `NSCoding` remains unimplemented |
199+
| `NSRegularExpression` | Complete | Substantial | |
200200
| `Scanner` | Mostly Complete | Incomplete | `scanHex<T: _FloatLike>(_:locale:locationToScanFrom:to:)` and `localizedScannerWithString(_:)` remain unimplemented |
201201
| `NSTextCheckingResult` | Mostly Complete | Incomplete | `NSCoding`, `NSCopying`, `resultType`, and `range(at:)` remain unimplemented |
202202
| `NSAttributedString` | Incomplete | Incomplete | `NSCoding`, `NS[Mutable]Copying`, `attributedSubstring(from:)`, `isEqual(to:)`, `init(NSAttributedString:)` remain unimplemented |

Foundation/NSRegularExpression.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,37 @@ open class NSRegularExpression: NSObject, NSCopying, NSCoding {
3939
}
4040

4141
open func encode(with aCoder: NSCoder) {
42-
NSUnimplemented()
42+
guard aCoder.allowsKeyedCoding else {
43+
preconditionFailure("Unkeyed coding is unsupported.")
44+
}
45+
46+
aCoder.encode(self.pattern._nsObject, forKey: "NSPattern")
47+
aCoder.encode(self.options.rawValue._bridgeToObjectiveC(), forKey: "NSOptions")
48+
}
49+
50+
public required convenience init?(coder aDecoder: NSCoder) {
51+
guard aDecoder.allowsKeyedCoding else {
52+
preconditionFailure("Unkeyed coding is unsupported.")
53+
}
54+
55+
guard let pattern = aDecoder.decodeObject(forKey: "NSPattern") as? NSString,
56+
let options = aDecoder.decodeObject(forKey: "NSOptions") as? NSNumber else {
57+
return nil
58+
}
59+
60+
do {
61+
try self.init(pattern: pattern._swiftObject, options: Options(rawValue: options.uintValue))
62+
} catch {
63+
return nil
64+
}
4365
}
4466

45-
public required init?(coder aDecoder: NSCoder) {
46-
NSUnimplemented()
67+
open override func isEqual(_ object: Any?) -> Bool {
68+
guard let other = object as? NSRegularExpression else { return false }
69+
70+
return self === other
71+
|| (self.pattern == other.pattern
72+
&& self.options == other.options)
4773
}
4874

4975
/* An instance of NSRegularExpression is created from a regular expression pattern and a set of options. If the pattern is invalid, nil will be returned and an NSError will be returned by reference. The pattern syntax currently supported is that specified by ICU.

TestFoundation/TestNSRegularExpression.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class TestNSRegularExpression : XCTestCase {
2525
return [
2626
("test_simpleRegularExpressions", test_simpleRegularExpressions),
2727
("test_regularExpressionReplacement", test_regularExpressionReplacement),
28-
("test_complexRegularExpressions", test_complexRegularExpressions)
28+
("test_complexRegularExpressions", test_complexRegularExpressions),
29+
("test_Equal", test_Equal),
30+
("test_NSCoding", test_NSCoding),
2931
]
3032
}
3133

@@ -317,4 +319,37 @@ class TestNSRegularExpression : XCTestCase {
317319
complexRegularExpressionTest("(a|b)x|123|(c|d)y", [], "axcy", [], NSMakeRange(0, 4), 2, NSMakeRange(0, 2), NSMakeRange(0, 1), NSMakeRange(NSNotFound, 0))
318320
complexRegularExpressionTest("(a|b)x|123|(c|d)y", [], "cya", [], NSMakeRange(0, 3), 1, NSMakeRange(0, 2), NSMakeRange(NSNotFound, 0), NSMakeRange(0, 1))
319321
}
322+
323+
func test_Equal() {
324+
var regularExpressionA = try! NSRegularExpression(pattern: "[a-z]+", options: [])
325+
var regularExpressionB = try! NSRegularExpression(pattern: "[a-z]+", options: [])
326+
XCTAssertTrue(regularExpressionA == regularExpressionB)
327+
XCTAssertFalse(regularExpressionA === regularExpressionB)
328+
329+
regularExpressionA = try! NSRegularExpression(pattern: "[a-z]+", options: .caseInsensitive)
330+
regularExpressionB = try! NSRegularExpression(pattern: "[a-z]+", options: .caseInsensitive)
331+
XCTAssertTrue(regularExpressionA == regularExpressionB)
332+
XCTAssertFalse(regularExpressionA === regularExpressionB)
333+
334+
regularExpressionA = try! NSRegularExpression(pattern: "[a-z]+", options: [.caseInsensitive, .allowCommentsAndWhitespace])
335+
regularExpressionB = try! NSRegularExpression(pattern: "[a-z]+", options: [.caseInsensitive, .allowCommentsAndWhitespace])
336+
XCTAssertTrue(regularExpressionA == regularExpressionB)
337+
XCTAssertFalse(regularExpressionA === regularExpressionB)
338+
339+
regularExpressionA = try! NSRegularExpression(pattern: "[a-z]+", options: .caseInsensitive)
340+
regularExpressionB = try! NSRegularExpression(pattern: "[a-z]+", options: [.caseInsensitive, .allowCommentsAndWhitespace])
341+
XCTAssertFalse(regularExpressionA == regularExpressionB)
342+
XCTAssertFalse(regularExpressionA === regularExpressionB)
343+
344+
regularExpressionA = try! NSRegularExpression(pattern: "[a-y]+", options: .caseInsensitive)
345+
regularExpressionB = try! NSRegularExpression(pattern: "[a-z]+", options: .caseInsensitive)
346+
XCTAssertFalse(regularExpressionA == regularExpressionB)
347+
XCTAssertFalse(regularExpressionA === regularExpressionB)
348+
}
349+
350+
func test_NSCoding() {
351+
let regularExpressionA = try! NSRegularExpression(pattern: "[a-z]+", options: [.caseInsensitive, .allowCommentsAndWhitespace])
352+
let regularExpressionB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: regularExpressionA)) as! NSRegularExpression
353+
XCTAssertEqual(regularExpressionA, regularExpressionB, "Archived then unarchived `NSRegularExpression` must be equal.")
354+
}
320355
}

0 commit comments

Comments
 (0)