Skip to content

Commit a27996f

Browse files
authored
Merge pull request #871 from naithar/textcheckingresult
2 parents 8e87138 + eb781d3 commit a27996f

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

Docs/Status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ There is no _Complete_ status for test coverage because there are always additio
198198
|-----------------------------|-----------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
199199
| `RegularExpression` | Mostly Complete | Substantial | `NSCoding` remains unimplemented |
200200
| `Scanner` | Mostly Complete | Incomplete | `scanHex<T: _FloatLike>(_:locale:locationToScanFrom:to:)` and `localizedScannerWithString(_:)` remain unimplemented |
201-
| `TextCheckingResult` | Mostly Complete | Incomplete | `NSCoding`, `NSCopying`, `resultType`, and `range(at:)` remain unimplemented |
201+
| `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 |
203203
| `NSMutableAttributedString` | Unimplemented | Incomplete | Only `addAttribute(_:value:range:)` is implemented |
204204
| `NSCharacterSet` | Mostly Complete | Incomplete | `NSCoding` remains unimplemented |

Foundation/NSRegularExpression.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public struct NSMatchingFlags : OptionSet {
113113

114114
internal class _NSRegularExpressionMatcher {
115115
var regex: NSRegularExpression
116-
var block: (TextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void
117-
init(regex: NSRegularExpression, block: @escaping (TextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void) {
116+
var block: (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void
117+
init(regex: NSRegularExpression, block: @escaping (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void) {
118118
self.regex = regex
119119
self.block = block
120120
}
@@ -133,7 +133,7 @@ internal func _NSRegularExpressionMatch(_ context: UnsafeMutableRawPointer?, ran
133133
})
134134
} else {
135135
let result = ranges!.withMemoryRebound(to: NSRange.self, capacity: count) { rangePtr in
136-
TextCheckingResult.regularExpressionCheckingResultWithRanges(rangePtr, count: count, regularExpression: matcher.regex)
136+
NSTextCheckingResult.regularExpressionCheckingResultWithRanges(rangePtr, count: count, regularExpression: matcher.regex)
137137
}
138138
#if os(OSX) || os(iOS)
139139
let flags = NSMatchingFlags(rawValue: options.rawValue)
@@ -151,7 +151,7 @@ extension NSRegularExpression {
151151
/* 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.
152152
*/
153153

154-
public func enumerateMatches(in string: String, options: NSMatchingOptions, range: NSRange, using block: @escaping (TextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
154+
public func enumerateMatches(in string: String, options: NSMatchingOptions, range: NSRange, using block: @escaping (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
155155
let matcher = _NSRegularExpressionMatcher(regex: self, block: block)
156156
withExtendedLifetime(matcher) { (m: _NSRegularExpressionMatcher) -> Void in
157157
#if os(OSX) || os(iOS)
@@ -163,9 +163,9 @@ extension NSRegularExpression {
163163
}
164164
}
165165

166-
public func matches(in string: String, options: NSMatchingOptions, range: NSRange) -> [TextCheckingResult] {
167-
var matches = [TextCheckingResult]()
168-
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: TextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
166+
public func matches(in string: String, options: NSMatchingOptions, range: NSRange) -> [NSTextCheckingResult] {
167+
var matches = [NSTextCheckingResult]()
168+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
169169
if let match = result {
170170
matches.append(match)
171171
}
@@ -182,9 +182,9 @@ extension NSRegularExpression {
182182
return count
183183
}
184184

185-
public func firstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> TextCheckingResult? {
186-
var first: TextCheckingResult?
187-
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: TextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
185+
public func firstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult? {
186+
var first: NSTextCheckingResult?
187+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
188188
first = result
189189
stop.pointee = true
190190
}
@@ -193,7 +193,7 @@ extension NSRegularExpression {
193193

194194
public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSRange {
195195
var firstRange = NSMakeRange(NSNotFound, 0)
196-
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: TextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
196+
enumerateMatches(in: string, options: options.subtracting(.reportProgress).subtracting(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
197197
if let match = result {
198198
firstRange = match.range
199199
} else {
@@ -264,7 +264,7 @@ extension NSRegularExpression {
264264

265265
/* For clients implementing their own replace functionality, this is a method to perform the template substitution for a single result, given the string from which the result was matched, an offset to be added to the location of the result in the string (for example, in case modifications to the string moved the result since it was matched), and a replacement template.
266266
*/
267-
public func replacementString(for result: TextCheckingResult, in string: String, offset: Int, template templ: String) -> String {
267+
public func replacementString(for result: NSTextCheckingResult, in string: String, offset: Int, template templ: String) -> String {
268268
// ??? need to consider what happens if offset takes range out of bounds due to replacement
269269
struct once {
270270
static let characterSet = CharacterSet(charactersIn: "\\$")

Foundation/NSTextCheckingResult.swift

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import CoreFoundation
1111

1212
/* NSTextCheckingType in this project is limited to regular expressions. */
13-
extension TextCheckingResult {
13+
extension NSTextCheckingResult {
1414
public struct CheckingType : OptionSet {
1515
public let rawValue: UInt64
1616
public init(rawValue: UInt64) { self.rawValue = rawValue }
@@ -19,44 +19,49 @@ extension TextCheckingResult {
1919
}
2020
}
2121

22-
open class TextCheckingResult: NSObject, NSCopying, NSCoding {
22+
open class NSTextCheckingResult: NSObject, NSCopying, NSCoding {
2323

2424
public override init() {
25-
super.init()
25+
if type(of: self) == NSTextCheckingResult.self {
26+
NSRequiresConcreteImplementation()
27+
}
2628
}
2729

28-
open class func regularExpressionCheckingResultWithRanges(_ ranges: NSRangePointer, count: Int, regularExpression: NSRegularExpression) -> TextCheckingResult {
29-
return _NSRegularExpressionTextCheckingResultResult(ranges: ranges, count: count, regularExpression: regularExpression)
30+
open class func regularExpressionCheckingResultWithRanges(_ ranges: NSRangePointer, count: Int, regularExpression: NSRegularExpression) -> NSTextCheckingResult {
31+
return _NSRegularExpressionNSTextCheckingResultResult(ranges: ranges, count: count, regularExpression: regularExpression)
3032
}
3133

3234
public required init?(coder aDecoder: NSCoder) {
33-
NSUnimplemented()
35+
if type(of: self) == NSTextCheckingResult.self {
36+
NSRequiresConcreteImplementation()
37+
}
3438
}
3539

3640
open func encode(with aCoder: NSCoder) {
37-
NSUnimplemented()
41+
NSRequiresConcreteImplementation()
3842
}
3943

4044
open override func copy() -> Any {
4145
return copy(with: nil)
4246
}
4347

4448
open func copy(with zone: NSZone? = nil) -> Any {
45-
NSUnimplemented()
49+
return self
4650
}
4751

4852
/* Mandatory properties, used with all types of results. */
49-
open var resultType: CheckingType { NSUnimplemented() }
53+
open var resultType: CheckingType { NSRequiresConcreteImplementation() }
5054
open var range: NSRange { return range(at: 0) }
5155
/* A result must have at least one range, but may optionally have more (for example, to represent regular expression capture groups). The range at index 0 always matches the range property. Additional ranges, if any, will have indexes from 1 to numberOfRanges-1. */
52-
open func range(at idx: Int) -> NSRange { NSUnimplemented() }
56+
open func range(at idx: Int) -> NSRange { NSRequiresConcreteImplementation() }
5357
open var regularExpression: NSRegularExpression? { return nil }
5458
open var numberOfRanges: Int { return 1 }
5559
}
5660

57-
internal class _NSRegularExpressionTextCheckingResultResult : TextCheckingResult {
61+
internal class _NSRegularExpressionNSTextCheckingResultResult : NSTextCheckingResult {
5862
var _ranges = [NSRange]()
5963
let _regularExpression: NSRegularExpression
64+
6065
init(ranges: NSRangePointer, count: Int, regularExpression: NSRegularExpression) {
6166
_regularExpression = regularExpression
6267
super.init()
@@ -67,7 +72,11 @@ internal class _NSRegularExpressionTextCheckingResultResult : TextCheckingResult
6772
}
6873

6974
internal required init?(coder aDecoder: NSCoder) {
70-
fatalError("init(coder:) has not been implemented")
75+
NSUnimplemented()
76+
}
77+
78+
internal override func encode(with aCoder: NSCoder) {
79+
NSUnimplemented()
7180
}
7281

7382
override var resultType: CheckingType { return .RegularExpression }
@@ -76,9 +85,9 @@ internal class _NSRegularExpressionTextCheckingResultResult : TextCheckingResult
7685
override var regularExpression: NSRegularExpression? { return _regularExpression }
7786
}
7887

79-
extension TextCheckingResult {
88+
extension NSTextCheckingResult {
8089

81-
public func resultByAdjustingRangesWithOffset(_ offset: Int) -> TextCheckingResult {
90+
public func resultByAdjustingRangesWithOffset(_ offset: Int) -> NSTextCheckingResult {
8291
let count = self.numberOfRanges
8392
var newRanges = [NSRange]()
8493
for idx in 0..<count {
@@ -91,7 +100,7 @@ extension TextCheckingResult {
91100
newRanges.append(NSRange(location: currentRange.location + offset,length: currentRange.length))
92101
}
93102
}
94-
let result = TextCheckingResult.regularExpressionCheckingResultWithRanges(&newRanges, count: count, regularExpression: self.regularExpression!)
103+
let result = NSTextCheckingResult.regularExpressionCheckingResultWithRanges(&newRanges, count: count, regularExpression: self.regularExpression!)
95104
return result
96105
}
97106
}

TestFoundation/TestNSTextCheckingResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TestNSTextCheckingResult: XCTestCase {
3232
let searchString = "1x030cy"
3333
let searchOptions: NSMatchingOptions = []
3434
let searchRange = NSMakeRange(0,7)
35-
let match: TextCheckingResult = regex.firstMatch(in: searchString, options: searchOptions, range: searchRange)!
35+
let match: NSTextCheckingResult = regex.firstMatch(in: searchString, options: searchOptions, range: searchRange)!
3636
//Positive offset
3737
var result = match.resultByAdjustingRangesWithOffset(1)
3838
XCTAssertEqual(result.range(at: 0).location, 6)

0 commit comments

Comments
 (0)