14
14
//
15
15
//===----------------------------------------------------------------------===//
16
16
17
+ // Important Note
18
+ // ==============
19
+ //
20
+ // This file is shared between two projects:
21
+ //
22
+ // 1. https://github.com/apple/swift/tree/master/stdlib/public/SDK/Foundation
23
+ // 2. https://github.com/apple/swift-corelibs-foundation/tree/master/Foundation
24
+ //
25
+ // If you change this file, you must update it in both places.
26
+
27
+ #if !DEPLOYMENT_RUNTIME_SWIFT
17
28
@_exported import Foundation // Clang module
29
+ #endif
18
30
19
31
// Open Issues
20
32
// ===========
@@ -36,6 +48,7 @@ func _toNSRange(_ r: Range<String.Index>) -> NSRange {
36
48
length: r. upperBound. encodedOffset - r. lowerBound. encodedOffset)
37
49
}
38
50
51
+ #if !DEPLOYMENT_RUNTIME_SWIFT
39
52
// We only need this for UnsafeMutablePointer, but there's not currently a way
40
53
// to write that constraint.
41
54
extension Optional {
@@ -57,6 +70,7 @@ extension Optional {
57
70
return self == nil ? body ( nil ) : body ( & object)
58
71
}
59
72
}
73
+ #endif
60
74
61
75
extension String {
62
76
//===--- Class Methods --------------------------------------------------===//
@@ -156,7 +170,7 @@ extension String {
156
170
/// C array of UTF8-encoded bytes.
157
171
public init ? ( utf8String bytes: UnsafePointer < CChar > ) {
158
172
if let ns = NSString ( utf8String: bytes) {
159
- self = ns as String
173
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
160
174
} else {
161
175
return nil
162
176
}
@@ -185,7 +199,7 @@ extension String {
185
199
if let ns = NSString (
186
200
bytes: byteArray, length: byteArray. count, encoding: encoding. rawValue) {
187
201
188
- self = ns as String
202
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
189
203
} else {
190
204
return nil
191
205
}
@@ -210,7 +224,7 @@ extension String {
210
224
bytesNoCopy: bytes, length: length, encoding: encoding. rawValue,
211
225
freeWhenDone: flag) {
212
226
213
- self = ns as String
227
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
214
228
} else {
215
229
return nil
216
230
}
@@ -227,7 +241,7 @@ extension String {
227
241
utf16CodeUnits: UnsafePointer < unichar > ,
228
242
count: Int
229
243
) {
230
- self = NSString ( characters: utf16CodeUnits, length: count) as String
244
+ self = String . _unconditionallyBridgeFromObjectiveC ( NSString ( characters: utf16CodeUnits, length: count) )
231
245
}
232
246
233
247
// - (instancetype)
@@ -242,10 +256,10 @@ extension String {
242
256
count: Int ,
243
257
freeWhenDone flag: Bool
244
258
) {
245
- self = NSString (
259
+ self = String . _unconditionallyBridgeFromObjectiveC ( NSString (
246
260
charactersNoCopy: UnsafeMutablePointer ( mutating: utf16CodeUnitsNoCopy) ,
247
261
length: count,
248
- freeWhenDone: flag) as String
262
+ freeWhenDone: flag) )
249
263
}
250
264
251
265
//===--- Initializers that can fail -------------------------------------===//
@@ -263,7 +277,7 @@ extension String {
263
277
encoding enc: Encoding
264
278
) throws {
265
279
let ns = try NSString ( contentsOfFile: path, encoding: enc. rawValue)
266
- self = ns as String
280
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
267
281
}
268
282
269
283
// - (instancetype)
@@ -281,14 +295,14 @@ extension String {
281
295
var enc : UInt = 0
282
296
let ns = try NSString ( contentsOfFile: path, usedEncoding: & enc)
283
297
usedEncoding = Encoding ( rawValue: enc)
284
- self = ns as String
298
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
285
299
}
286
300
287
301
public init (
288
302
contentsOfFile path: String
289
303
) throws {
290
304
let ns = try NSString ( contentsOfFile: path, usedEncoding: nil )
291
- self = ns as String
305
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
292
306
}
293
307
294
308
// - (instancetype)
@@ -304,7 +318,7 @@ extension String {
304
318
encoding enc: Encoding
305
319
) throws {
306
320
let ns = try NSString ( contentsOf: url, encoding: enc. rawValue)
307
- self = ns as String
321
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
308
322
}
309
323
310
324
// - (instancetype)
@@ -322,14 +336,14 @@ extension String {
322
336
var enc : UInt = 0
323
337
let ns = try NSString ( contentsOf: url as URL , usedEncoding: & enc)
324
338
usedEncoding = Encoding ( rawValue: enc)
325
- self = ns as String
339
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
326
340
}
327
341
328
342
public init (
329
343
contentsOf url: URL
330
344
) throws {
331
345
let ns = try NSString ( contentsOf: url, usedEncoding: nil )
332
- self = ns as String
346
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
333
347
}
334
348
335
349
// - (instancetype)
@@ -343,7 +357,7 @@ extension String {
343
357
encoding enc: Encoding
344
358
) {
345
359
if let ns = NSString ( cString: cString, encoding: enc. rawValue) {
346
- self = ns as String
360
+ self = String . _unconditionallyBridgeFromObjectiveC ( ns )
347
361
} else {
348
362
return nil
349
363
}
@@ -359,7 +373,7 @@ extension String {
359
373
/// Unicode characters using a given `encoding`.
360
374
public init ? ( data: Data , encoding: Encoding ) {
361
375
guard let s = NSString ( data: data, encoding: encoding. rawValue) else { return nil }
362
- self = s as String
376
+ self = String . _unconditionallyBridgeFromObjectiveC ( s )
363
377
}
364
378
365
379
// - (instancetype)initWithFormat:(NSString *)format, ...
@@ -400,9 +414,17 @@ extension String {
400
414
/// format string as a template into which the remaining argument
401
415
/// values are substituted according to given locale information.
402
416
public init ( format: String , locale: Locale ? , arguments: [ CVarArg ] ) {
417
+ #if DEPLOYMENT_RUNTIME_SWIFT
418
+ self = withVaList ( arguments) {
419
+ String . _unconditionallyBridgeFromObjectiveC (
420
+ NSString ( format: format, locale: locale? . _bridgeToObjectiveC ( ) , arguments: $0)
421
+ )
422
+ }
423
+ #else
403
424
self = withVaList ( arguments) {
404
425
NSString ( format: format, locale: locale, arguments: $0) as String
405
426
}
427
+ #endif
406
428
}
407
429
408
430
}
@@ -414,7 +436,7 @@ extension StringProtocol where Index == String.Index {
414
436
/// The corresponding `NSString` - a convenience for bridging code.
415
437
// FIXME(strings): There is probably a better way to bridge Self to NSString
416
438
var _ns : NSString {
417
- return self . _ephemeralString as NSString
439
+ return self . _ephemeralString. _bridgeToObjectiveC ( )
418
440
}
419
441
420
442
/// Return an `Index` corresponding to the given offset in our UTF-16
@@ -584,7 +606,7 @@ extension StringProtocol where Index == String.Index {
584
606
range: _toNSRange (
585
607
range ?? startIndex..< endIndex
586
608
) ,
587
- locale: locale
609
+ locale: locale? . _bridgeToObjectiveC ( )
588
610
)
589
611
590
612
: range != nil ? _ns. compare (
@@ -616,6 +638,23 @@ extension StringProtocol where Index == String.Index {
616
638
matchesInto outputArray: UnsafeMutablePointer < [ String ] > ? = nil ,
617
639
filterTypes: [ String ] ? = nil
618
640
) -> Int {
641
+ #if DEPLOYMENT_RUNTIME_SWIFT
642
+ var outputNamePlaceholder : String ?
643
+ var outputArrayPlaceholder = [ String] ( )
644
+ let res = self . _ns. completePath (
645
+ into: & outputNamePlaceholder,
646
+ caseSensitive: caseSensitive,
647
+ matchesInto: & outputArrayPlaceholder,
648
+ filterTypes: filterTypes
649
+ )
650
+ if let n = outputNamePlaceholder {
651
+ outputName? . pointee = n
652
+ } else {
653
+ outputName? . pointee = " "
654
+ }
655
+ outputArray? . pointee = outputArrayPlaceholder
656
+ return res
657
+ #else // DEPLOYMENT_RUNTIME_SWIFT
619
658
var nsMatches : NSArray ?
620
659
var nsOutputName : NSString ?
621
660
@@ -647,6 +686,7 @@ extension StringProtocol where Index == String.Index {
647
686
outputName? . pointee = n as String
648
687
}
649
688
return result
689
+ #endif // DEPLOYMENT_RUNTIME_SWIFT
650
690
}
651
691
652
692
// - (NSArray *)
@@ -875,6 +915,7 @@ extension StringProtocol where Index == String.Index {
875
915
return _ns. precomposedStringWithCompatibilityMapping
876
916
}
877
917
918
+ #if !DEPLOYMENT_RUNTIME_SWIFT
878
919
// - (id)propertyList
879
920
880
921
/// Parses the `String` as a text representation of a
@@ -891,6 +932,7 @@ extension StringProtocol where Index == String.Index {
891
932
public func propertyListFromStringsFileFormat( ) -> [ String : String ] {
892
933
return _ns. propertyListFromStringsFileFormat ( ) as! [ String : String ] ? ?? [ : ]
893
934
}
935
+ #endif
894
936
895
937
// - (BOOL)localizedStandardContainsString:(NSString *)str NS_AVAILABLE(10_11, 9_0);
896
938
@@ -1048,6 +1090,7 @@ extension StringProtocol where Index == String.Index {
1048
1090
: _ns. replacingOccurrences ( of: target, with: replacement)
1049
1091
}
1050
1092
1093
+ #if !DEPLOYMENT_RUNTIME_SWIFT
1051
1094
// - (NSString *)
1052
1095
// stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
1053
1096
@@ -1061,6 +1104,7 @@ extension StringProtocol where Index == String.Index {
1061
1104
) -> String ? {
1062
1105
return _ns. replacingPercentEscapes ( using: encoding. rawValue)
1063
1106
}
1107
+ #endif
1064
1108
1065
1109
// - (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
1066
1110
@@ -1129,6 +1173,7 @@ extension StringProtocol where Index == String.Index {
1129
1173
1130
1174
// - (nullable NSString *)stringByApplyingTransform:(NSString *)transform reverse:(BOOL)reverse NS_AVAILABLE(10_11, 9_0);
1131
1175
1176
+ #if !DEPLOYMENT_RUNTIME_SWIFT
1132
1177
/// Perform string transliteration.
1133
1178
@available ( OSX 10 . 11 , iOS 9 . 0 , * )
1134
1179
public func applyingTransform(
@@ -1175,6 +1220,7 @@ extension StringProtocol where Index == String.Index {
1175
1220
}
1176
1221
}
1177
1222
}
1223
+ #endif
1178
1224
1179
1225
// - (void)
1180
1226
// enumerateSubstringsInRange:(NSRange)range
@@ -1385,6 +1431,7 @@ extension StringProtocol where Index == String.Index {
1385
1431
return _range ( _ns. lineRange ( for: _toNSRange ( aRange. relative ( to: self ) ) ) )
1386
1432
}
1387
1433
1434
+ #if !DEPLOYMENT_RUNTIME_SWIFT
1388
1435
// - (NSArray *)
1389
1436
// linguisticTagsInRange:(NSRange)range
1390
1437
// scheme:(NSString *)tagScheme
@@ -1432,6 +1479,7 @@ extension StringProtocol where Index == String.Index {
1432
1479
return _range (
1433
1480
_ns. paragraphRange ( for: _toNSRange ( aRange. relative ( to: self ) ) ) )
1434
1481
}
1482
+ #endif
1435
1483
1436
1484
// - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet
1437
1485
//
@@ -1553,6 +1601,7 @@ extension StringProtocol where Index == String.Index {
1553
1601
_ns. localizedStandardRange ( of: string. _ephemeralString) )
1554
1602
}
1555
1603
1604
+ #if !DEPLOYMENT_RUNTIME_SWIFT
1556
1605
// - (NSString *)
1557
1606
// stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
1558
1607
@@ -1566,6 +1615,7 @@ extension StringProtocol where Index == String.Index {
1566
1615
) -> String ? {
1567
1616
return _ns. addingPercentEscapes ( using: encoding. rawValue)
1568
1617
}
1618
+ #endif
1569
1619
1570
1620
//===--- From the 10.10 release notes; not in public documentation ------===//
1571
1621
// No need to make these unavailable on earlier OSes, since they can
@@ -1874,6 +1924,7 @@ extension StringProtocol {
1874
1924
fatalError ( " unavailable function can't be called " )
1875
1925
}
1876
1926
1927
+ #if !DEPLOYMENT_RUNTIME_SWIFT
1877
1928
@available ( * , unavailable, renamed: " enumerateLinguisticTags(in:scheme:options:orthography:_:) " )
1878
1929
public func enumerateLinguisticTagsIn(
1879
1930
_ range: Range < Index > ,
@@ -1885,6 +1936,7 @@ extension StringProtocol {
1885
1936
) {
1886
1937
fatalError ( " unavailable function can't be called " )
1887
1938
}
1939
+ #endif
1888
1940
1889
1941
@available ( * , unavailable, renamed: " enumerateSubstrings(in:options:_:) " )
1890
1942
public func enumerateSubstringsIn(
@@ -1941,6 +1993,7 @@ extension StringProtocol {
1941
1993
fatalError ( " unavailable function can't be called " )
1942
1994
}
1943
1995
1996
+ #if !DEPLOYMENT_RUNTIME_SWIFT
1944
1997
@available ( * , unavailable, renamed: " linguisticTags(in:scheme:options:orthography:tokenRanges:) " )
1945
1998
public func linguisticTagsIn(
1946
1999
_ range: Range < Index > ,
@@ -1951,6 +2004,7 @@ extension StringProtocol {
1951
2004
) -> [ String ] {
1952
2005
fatalError ( " unavailable function can't be called " )
1953
2006
}
2007
+ #endif
1954
2008
1955
2009
@available ( * , unavailable, renamed: " lowercased(with:) " )
1956
2010
public func lowercaseStringWith( _ locale: Locale ? ) -> String {
0 commit comments