@@ -15,20 +15,29 @@ extension NSTextCheckingResult {
15
15
public let rawValue : UInt64
16
16
public init ( rawValue: UInt64 ) { self . rawValue = rawValue }
17
17
18
- public static let RegularExpression = CheckingType ( rawValue: 1 << 10 ) // regular expression matches
18
+ public static let regularExpression = CheckingType ( rawValue: 1 << 10 )
19
19
}
20
20
}
21
21
22
- open class NSTextCheckingResult : NSObject , NSCopying , NSCoding {
22
+ open class NSTextCheckingResult : NSObject , NSCopying , NSSecureCoding {
23
23
24
24
public override init ( ) {
25
25
if type ( of: self ) == NSTextCheckingResult . self {
26
26
NSRequiresConcreteImplementation ( )
27
27
}
28
28
}
29
29
30
- open class func regularExpressionCheckingResultWithRanges( _ ranges: NSRangePointer , count: Int , regularExpression: NSRegularExpression ) -> NSTextCheckingResult {
31
- return _NSRegularExpressionNSTextCheckingResultResult ( ranges: ranges, count: count, regularExpression: regularExpression)
30
+ open class func regularExpressionCheckingResult( ranges: NSRangePointer , count: Int , regularExpression: NSRegularExpression ) -> NSTextCheckingResult {
31
+ let buffer = UnsafeBufferPointer ( start: ranges, count: count)
32
+ let array = Array ( buffer)
33
+
34
+ if count > 0 && count <= 3 {
35
+ return NSSimpleRegularExpressionCheckingResult ( rangeArray: array, regularExpression: regularExpression)
36
+ } else if count > 3 && count <= 7 {
37
+ return NSExtendedRegularExpressionCheckingResult ( rangeArray: array, regularExpression: regularExpression)
38
+ } else {
39
+ return NSComplexRegularExpressionCheckingResult ( rangeArray: array, regularExpression: regularExpression)
40
+ }
32
41
}
33
42
34
43
public required init ? ( coder aDecoder: NSCoder ) {
@@ -41,6 +50,10 @@ open class NSTextCheckingResult: NSObject, NSCopying, NSCoding {
41
50
NSRequiresConcreteImplementation ( )
42
51
}
43
52
53
+ open class var supportsSecureCoding : Bool {
54
+ NSRequiresConcreteImplementation ( )
55
+ }
56
+
44
57
open override func copy( ) -> Any {
45
58
return copy ( with: nil )
46
59
}
@@ -58,44 +71,102 @@ open class NSTextCheckingResult: NSObject, NSCopying, NSCoding {
58
71
open func range( withName: String ) -> NSRange { NSRequiresConcreteImplementation ( ) }
59
72
open var regularExpression : NSRegularExpression ? { return nil }
60
73
open var numberOfRanges : Int { return 1 }
74
+
75
+ internal func encodeRange( with coder: NSCoder ) {
76
+ guard coder. allowsKeyedCoding else {
77
+ fatalError ( " Encoding this class requires keyed coding " )
78
+ }
79
+
80
+ coder. encode ( range. location, forKey: " NSRangeLocation " )
81
+ coder. encode ( range. length, forKey: " NSRangeLength " )
82
+ }
83
+
84
+ internal func decodeRange( from coder: NSCoder ) -> NSRange {
85
+ guard coder. allowsKeyedCoding else {
86
+ fatalError ( " Decoding this class requires keyed coding " )
87
+ }
88
+
89
+ return NSMakeRange ( coder. decodeInteger ( forKey: " NSRangeLocation " ) , coder. decodeInteger ( forKey: " NSRangeLength " ) )
90
+ }
61
91
}
62
92
63
- internal class _NSRegularExpressionNSTextCheckingResultResult : NSTextCheckingResult {
64
- var _ranges = [ NSRange] ( )
65
- let _regularExpression : NSRegularExpression
93
+ // Darwin uses these private subclasses, each of which can be archived. We reimplement all three here so that NSKeyed{Una,A}rchiver can find them.
94
+ // Since we do not have to box an array of NSRanges into a NSArray of NSValues, we do not implement the storage optimization these subclasses would have on Darwin. They exist purely to be encoded or decoded. When we produce instances, we will produce the correct subclass for the count Darwin expects so that _that_ implementation can likewise be efficient.
95
+
96
+ internal class NSRegularExpressionCheckingResult : NSTextCheckingResult {
97
+ let _regularExpression : NSRegularExpression !
98
+ override var regularExpression : NSRegularExpression ? { return _regularExpression }
66
99
67
- init ( ranges: NSRangePointer , count: Int , regularExpression: NSRegularExpression ) {
100
+ let _rangeArray : [ NSRange ] !
101
+ var rangeArray : [ NSRange ] { return _rangeArray }
102
+
103
+ init ( rangeArray: [ NSRange ] , regularExpression: NSRegularExpression ) {
104
+ _rangeArray = rangeArray. map { $0. location == kCFNotFound ? NSMakeRange ( NSNotFound, 0 ) : $0 }
68
105
_regularExpression = regularExpression
69
106
super. init ( )
70
- let notFound = NSRange ( location: NSNotFound, length: 0 )
71
- for i in 0 ..< count {
72
- ranges [ i] . location == kCFNotFound ? _ranges. append ( notFound) : _ranges. append ( ranges [ i] )
73
- }
74
107
}
75
-
76
- internal required init ? ( coder aDecoder: NSCoder ) {
77
- NSUnimplemented ( )
108
+
109
+ override init ( ) {
110
+ if type ( of: self ) == NSRegularExpressionCheckingResult . self {
111
+ NSRequiresConcreteImplementation ( )
112
+ }
113
+
114
+ _regularExpression = nil
115
+ _rangeArray = nil
116
+ super. init ( )
78
117
}
79
118
80
- internal override func encode( with aCoder: NSCoder ) {
81
- NSUnimplemented ( )
119
+ public convenience required init ? ( coder aDecoder: NSCoder ) {
120
+ guard aDecoder. allowsKeyedCoding else {
121
+ fatalError ( " Decoding this class requires keyed coding " )
122
+ }
123
+
124
+ let regularExpression = aDecoder. decodeObject ( of: NSRegularExpression . self, forKey: " NSRegularExpression " ) !
125
+ let nsRanges = aDecoder. decodeObject ( of: [ NSArray . self, NSValue . self ] , forKey: " NSRangeArray " ) as! NSArray
126
+ let rangeArray = nsRanges. compactMap { return ( $0 as! NSValue ) . rangeValue }
127
+
128
+ self . init ( rangeArray: rangeArray, regularExpression: regularExpression)
129
+ }
130
+
131
+ override func encode( with aCoder: NSCoder ) {
132
+ guard aCoder. allowsKeyedCoding else {
133
+ fatalError ( " Encoding this class requires keyed coding " )
134
+ }
135
+
136
+ let ranges = rangeArray. map { NSValue ( range: $0) } . _nsObject
137
+
138
+ encodeRange ( with: aCoder)
139
+ aCoder. encode ( regularExpression, forKey: " NSRegularExpression " )
140
+ aCoder. encode ( ranges, forKey: " NSRangeArray " )
82
141
}
83
142
84
- override var resultType : CheckingType { return . RegularExpression }
85
- override func range( at idx: Int ) -> NSRange { return _ranges [ idx] }
143
+ override class var supportsSecureCoding : Bool { return true }
144
+
145
+ override var resultType : NSTextCheckingResult . CheckingType { return . regularExpression }
146
+
86
147
override func range( withName name: String ) -> NSRange {
87
- let idx = _regularExpression . _captureGroupNumber ( withName: name)
148
+ let idx = regularExpression! . _captureGroupNumber ( withName: name)
88
149
if idx != kCFNotFound, idx < numberOfRanges {
89
150
return range ( at: idx)
90
151
}
91
-
152
+
92
153
return NSRange ( location: NSNotFound, length: 0 )
93
154
}
94
-
95
- override var numberOfRanges : Int { return _ranges. count }
96
- override var regularExpression : NSRegularExpression ? { return _regularExpression }
155
+
156
+ override func range( at idx: Int ) -> NSRange {
157
+ return rangeArray [ idx]
158
+ }
159
+
160
+ override var numberOfRanges : Int {
161
+ return rangeArray. count
162
+ }
97
163
}
98
164
165
+ internal class NSSimpleRegularExpressionCheckingResult : NSRegularExpressionCheckingResult { }
166
+ internal class NSExtendedRegularExpressionCheckingResult : NSRegularExpressionCheckingResult { }
167
+ internal class NSComplexRegularExpressionCheckingResult : NSRegularExpressionCheckingResult { }
168
+
169
+
99
170
extension NSTextCheckingResult {
100
171
101
172
public func adjustingRanges( offset: Int ) -> NSTextCheckingResult {
@@ -111,8 +182,149 @@ extension NSTextCheckingResult {
111
182
newRanges. append ( NSRange ( location: currentRange. location + offset, length: currentRange. length) )
112
183
}
113
184
}
114
- let result = NSTextCheckingResult . regularExpressionCheckingResultWithRanges ( & newRanges, count: count, regularExpression: self . regularExpression!)
185
+ let result = NSTextCheckingResult . regularExpressionCheckingResult ( ranges : & newRanges, count: count, regularExpression: self . regularExpression!)
115
186
return result
116
187
}
117
188
}
118
189
190
+ // MARK: Availability diagnostics for unsupported features
191
+
192
+ @available ( * , deprecated, message: " These types of result will not be returned by swift-corelibs-foundation API. " )
193
+ extension NSTextCheckingResult . CheckingType {
194
+ public static let orthography = NSTextCheckingResult . CheckingType ( rawValue: 1 << 1 )
195
+ public static let spelling = NSTextCheckingResult . CheckingType ( rawValue: 1 << 2 )
196
+ public static let grammar = NSTextCheckingResult . CheckingType ( rawValue: 1 << 3 )
197
+ public static let date = NSTextCheckingResult . CheckingType ( rawValue: 1 << 4 )
198
+ public static let address = NSTextCheckingResult . CheckingType ( rawValue: 1 << 5 )
199
+ public static let link = NSTextCheckingResult . CheckingType ( rawValue: 1 << 6 )
200
+ public static let quote = NSTextCheckingResult . CheckingType ( rawValue: 1 << 7 )
201
+ public static let dash = NSTextCheckingResult . CheckingType ( rawValue: 1 << 8 )
202
+ public static let replacement = NSTextCheckingResult . CheckingType ( rawValue: 1 << 9 )
203
+ public static let correction = NSTextCheckingResult . CheckingType ( rawValue: 1 << 10 )
204
+ public static let phoneNumber = NSTextCheckingResult . CheckingType ( rawValue: 1 << 11 )
205
+ public static let transitInformation = NSTextCheckingResult . CheckingType ( rawValue: 1 << 12 )
206
+ }
207
+
208
+ public struct NSTextCheckingKey : RawRepresentable , Hashable {
209
+ public var rawValue : String
210
+
211
+ init ( _ string: String ) {
212
+ self . init ( rawValue: string)
213
+ }
214
+
215
+ public init ( rawValue: Self . RawValue ) {
216
+ self . rawValue = rawValue
217
+ }
218
+ }
219
+
220
+ @available ( * , deprecated, message: " Results associated with these keys are not available in swift-corelibs-foundation. " )
221
+ extension NSTextCheckingKey {
222
+ static let airline = NSTextCheckingKey ( rawValue: " Airline " )
223
+ static let city = NSTextCheckingKey ( rawValue: " City " )
224
+ static let country = NSTextCheckingKey ( rawValue: " Country " )
225
+ static let flight = NSTextCheckingKey ( rawValue: " Flight " )
226
+ static let jobTitle = NSTextCheckingKey ( rawValue: " JobTitle " )
227
+ static let name = NSTextCheckingKey ( rawValue: " Name " )
228
+ static let organization = NSTextCheckingKey ( rawValue: " Organization " )
229
+ static let phone = NSTextCheckingKey ( rawValue: " Phone " )
230
+ static let state = NSTextCheckingKey ( rawValue: " State " )
231
+ static let street = NSTextCheckingKey ( rawValue: " Street " )
232
+ static let zip = NSTextCheckingKey ( rawValue: " Zip " )
233
+ }
234
+
235
+ @available ( * , unavailable, message: " These types of results cannot be constructed in swift-corelibs-foundation " )
236
+ extension NSTextCheckingResult {
237
+ open class func orthographyCheckingResult( range: NSRange , orthography: NSOrthography ) -> NSTextCheckingResult {
238
+ NSUnsupported ( )
239
+ }
240
+
241
+ open class func spellCheckingResult( range: NSRange ) -> NSTextCheckingResult {
242
+ NSUnsupported ( )
243
+ }
244
+
245
+ open class func grammarCheckingResult( range: NSRange , details: [ [ String : Any ] ] ) -> NSTextCheckingResult {
246
+ NSUnsupported ( )
247
+ }
248
+
249
+ open class func dateCheckingResult( range: NSRange , date: Date ) -> NSTextCheckingResult {
250
+ NSUnsupported ( )
251
+ }
252
+
253
+ open class func dateCheckingResult( range: NSRange , date: Date , timeZone: TimeZone , duration: TimeInterval ) -> NSTextCheckingResult {
254
+ NSUnsupported ( )
255
+ }
256
+
257
+ open class func addressCheckingResult( range: NSRange , components: [ NSTextCheckingKey : String ] ) -> NSTextCheckingResult {
258
+ NSUnsupported ( )
259
+ }
260
+
261
+ open class func linkCheckingResult( range: NSRange , url: URL ) -> NSTextCheckingResult {
262
+ NSUnsupported ( )
263
+ }
264
+
265
+ open class func quoteCheckingResult( range: NSRange , replacementString: String ) -> NSTextCheckingResult {
266
+ NSUnsupported ( )
267
+ }
268
+
269
+ open class func dashCheckingResult( range: NSRange , replacementString: String ) -> NSTextCheckingResult {
270
+ NSUnsupported ( )
271
+ }
272
+
273
+ open class func replacementCheckingResult( range: NSRange , replacementString: String ) -> NSTextCheckingResult {
274
+ NSUnsupported ( )
275
+ }
276
+
277
+ open class func correctionCheckingResult( range: NSRange , replacementString: String , alternativeStrings: [ String ] ) -> NSTextCheckingResult {
278
+ NSUnsupported ( )
279
+ }
280
+
281
+ open class func phoneNumberCheckingResult( range: NSRange , phoneNumber: String ) -> NSTextCheckingResult {
282
+ NSUnsupported ( )
283
+ }
284
+
285
+ open class func transitInformationCheckingResult( range: NSRange , components: [ NSTextCheckingKey : String ] ) -> NSTextCheckingResult {
286
+ NSUnsupported ( )
287
+ }
288
+ }
289
+
290
+ @available ( * , deprecated, message: " NSOrtography is not available in swift-corelibs-foundation " )
291
+ open class NSOrthography : NSObject , NSCopying , NSSecureCoding {
292
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
293
+ open class func defaultOrtography( forLanguage: String ) -> Self {
294
+ NSUnsupported ( )
295
+ }
296
+
297
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
298
+ public init ( dominantScript: String , languageMap: [ String : [ String ] ] ) {
299
+ NSUnsupported ( )
300
+ }
301
+
302
+ public func copy( with zone: NSZone ? ) -> Any {
303
+ NSUnsupported ( )
304
+ }
305
+
306
+ open class var supportsSecureCoding : Bool { NSUnsupported ( ) }
307
+
308
+ public func encode( with aCoder: NSCoder ) {
309
+ NSUnsupported ( )
310
+ }
311
+
312
+ public required init ? ( coder aDecoder: NSCoder ) {
313
+ NSUnsupported ( )
314
+ }
315
+
316
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
317
+ open var languageMap : [ String : [ String ] ] { NSUnsupported ( ) }
318
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
319
+ open var dominantLanguage : String { NSUnsupported ( ) }
320
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
321
+ open var dominantScript : String { NSUnsupported ( ) }
322
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
323
+ open func dominantLanguage( forScript: String ) -> String ? { NSUnsupported ( ) }
324
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
325
+ open func language( forScript: String ) -> [ String ] ? { NSUnsupported ( ) }
326
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
327
+ open var alLScripts : [ String ] { NSUnsupported ( ) }
328
+ @available ( * , unavailable, message: " NSOrtography is not available in swift-corelibs-foundation " )
329
+ open var allLanguages : [ String ] { NSUnsupported ( ) }
330
+ }
0 commit comments