Skip to content

Commit 96a3b97

Browse files
ianpartridgephausler
authored andcommitted
Sync NSStringAPI.swift from overlay
1 parent db5c967 commit 96a3b97

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

stdlib/public/SDK/Foundation/NSStringAPI.swift

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

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
1728
@_exported import Foundation // Clang module
29+
#endif
1830

1931
// Open Issues
2032
// ===========
@@ -36,6 +48,7 @@ func _toNSRange(_ r: Range<String.Index>) -> NSRange {
3648
length: r.upperBound.encodedOffset - r.lowerBound.encodedOffset)
3749
}
3850

51+
#if !DEPLOYMENT_RUNTIME_SWIFT
3952
// We only need this for UnsafeMutablePointer, but there's not currently a way
4053
// to write that constraint.
4154
extension Optional {
@@ -57,6 +70,7 @@ extension Optional {
5770
return self == nil ? body(nil) : body(&object)
5871
}
5972
}
73+
#endif
6074

6175
extension String {
6276
//===--- Class Methods --------------------------------------------------===//
@@ -156,7 +170,7 @@ extension String {
156170
/// C array of UTF8-encoded bytes.
157171
public init?(utf8String bytes: UnsafePointer<CChar>) {
158172
if let ns = NSString(utf8String: bytes) {
159-
self = ns as String
173+
self = String._unconditionallyBridgeFromObjectiveC(ns)
160174
} else {
161175
return nil
162176
}
@@ -185,7 +199,7 @@ extension String {
185199
if let ns = NSString(
186200
bytes: byteArray, length: byteArray.count, encoding: encoding.rawValue) {
187201

188-
self = ns as String
202+
self = String._unconditionallyBridgeFromObjectiveC(ns)
189203
} else {
190204
return nil
191205
}
@@ -210,7 +224,7 @@ extension String {
210224
bytesNoCopy: bytes, length: length, encoding: encoding.rawValue,
211225
freeWhenDone: flag) {
212226

213-
self = ns as String
227+
self = String._unconditionallyBridgeFromObjectiveC(ns)
214228
} else {
215229
return nil
216230
}
@@ -227,7 +241,7 @@ extension String {
227241
utf16CodeUnits: UnsafePointer<unichar>,
228242
count: Int
229243
) {
230-
self = NSString(characters: utf16CodeUnits, length: count) as String
244+
self = String._unconditionallyBridgeFromObjectiveC(NSString(characters: utf16CodeUnits, length: count))
231245
}
232246

233247
// - (instancetype)
@@ -242,10 +256,10 @@ extension String {
242256
count: Int,
243257
freeWhenDone flag: Bool
244258
) {
245-
self = NSString(
259+
self = String._unconditionallyBridgeFromObjectiveC(NSString(
246260
charactersNoCopy: UnsafeMutablePointer(mutating: utf16CodeUnitsNoCopy),
247261
length: count,
248-
freeWhenDone: flag) as String
262+
freeWhenDone: flag))
249263
}
250264

251265
//===--- Initializers that can fail -------------------------------------===//
@@ -263,7 +277,7 @@ extension String {
263277
encoding enc: Encoding
264278
) throws {
265279
let ns = try NSString(contentsOfFile: path, encoding: enc.rawValue)
266-
self = ns as String
280+
self = String._unconditionallyBridgeFromObjectiveC(ns)
267281
}
268282

269283
// - (instancetype)
@@ -281,14 +295,14 @@ extension String {
281295
var enc: UInt = 0
282296
let ns = try NSString(contentsOfFile: path, usedEncoding: &enc)
283297
usedEncoding = Encoding(rawValue: enc)
284-
self = ns as String
298+
self = String._unconditionallyBridgeFromObjectiveC(ns)
285299
}
286300

287301
public init(
288302
contentsOfFile path: String
289303
) throws {
290304
let ns = try NSString(contentsOfFile: path, usedEncoding: nil)
291-
self = ns as String
305+
self = String._unconditionallyBridgeFromObjectiveC(ns)
292306
}
293307

294308
// - (instancetype)
@@ -304,7 +318,7 @@ extension String {
304318
encoding enc: Encoding
305319
) throws {
306320
let ns = try NSString(contentsOf: url, encoding: enc.rawValue)
307-
self = ns as String
321+
self = String._unconditionallyBridgeFromObjectiveC(ns)
308322
}
309323

310324
// - (instancetype)
@@ -322,14 +336,14 @@ extension String {
322336
var enc: UInt = 0
323337
let ns = try NSString(contentsOf: url as URL, usedEncoding: &enc)
324338
usedEncoding = Encoding(rawValue: enc)
325-
self = ns as String
339+
self = String._unconditionallyBridgeFromObjectiveC(ns)
326340
}
327341

328342
public init(
329343
contentsOf url: URL
330344
) throws {
331345
let ns = try NSString(contentsOf: url, usedEncoding: nil)
332-
self = ns as String
346+
self = String._unconditionallyBridgeFromObjectiveC(ns)
333347
}
334348

335349
// - (instancetype)
@@ -343,7 +357,7 @@ extension String {
343357
encoding enc: Encoding
344358
) {
345359
if let ns = NSString(cString: cString, encoding: enc.rawValue) {
346-
self = ns as String
360+
self = String._unconditionallyBridgeFromObjectiveC(ns)
347361
} else {
348362
return nil
349363
}
@@ -359,7 +373,7 @@ extension String {
359373
/// Unicode characters using a given `encoding`.
360374
public init?(data: Data, encoding: Encoding) {
361375
guard let s = NSString(data: data, encoding: encoding.rawValue) else { return nil }
362-
self = s as String
376+
self = String._unconditionallyBridgeFromObjectiveC(s)
363377
}
364378

365379
// - (instancetype)initWithFormat:(NSString *)format, ...
@@ -400,9 +414,17 @@ extension String {
400414
/// format string as a template into which the remaining argument
401415
/// values are substituted according to given locale information.
402416
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
403424
self = withVaList(arguments) {
404425
NSString(format: format, locale: locale, arguments: $0) as String
405426
}
427+
#endif
406428
}
407429

408430
}
@@ -414,7 +436,7 @@ extension StringProtocol where Index == String.Index {
414436
/// The corresponding `NSString` - a convenience for bridging code.
415437
// FIXME(strings): There is probably a better way to bridge Self to NSString
416438
var _ns: NSString {
417-
return self._ephemeralString as NSString
439+
return self._ephemeralString._bridgeToObjectiveC()
418440
}
419441

420442
/// Return an `Index` corresponding to the given offset in our UTF-16
@@ -584,7 +606,7 @@ extension StringProtocol where Index == String.Index {
584606
range: _toNSRange(
585607
range ?? startIndex..<endIndex
586608
),
587-
locale: locale
609+
locale: locale?._bridgeToObjectiveC()
588610
)
589611

590612
: range != nil ? _ns.compare(
@@ -616,6 +638,23 @@ extension StringProtocol where Index == String.Index {
616638
matchesInto outputArray: UnsafeMutablePointer<[String]>? = nil,
617639
filterTypes: [String]? = nil
618640
) -> 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
619658
var nsMatches: NSArray?
620659
var nsOutputName: NSString?
621660

@@ -647,6 +686,7 @@ extension StringProtocol where Index == String.Index {
647686
outputName?.pointee = n as String
648687
}
649688
return result
689+
#endif // DEPLOYMENT_RUNTIME_SWIFT
650690
}
651691

652692
// - (NSArray *)
@@ -875,6 +915,7 @@ extension StringProtocol where Index == String.Index {
875915
return _ns.precomposedStringWithCompatibilityMapping
876916
}
877917

918+
#if !DEPLOYMENT_RUNTIME_SWIFT
878919
// - (id)propertyList
879920

880921
/// Parses the `String` as a text representation of a
@@ -891,6 +932,7 @@ extension StringProtocol where Index == String.Index {
891932
public func propertyListFromStringsFileFormat() -> [String : String] {
892933
return _ns.propertyListFromStringsFileFormat() as! [String : String]? ?? [:]
893934
}
935+
#endif
894936

895937
// - (BOOL)localizedStandardContainsString:(NSString *)str NS_AVAILABLE(10_11, 9_0);
896938

@@ -1048,6 +1090,7 @@ extension StringProtocol where Index == String.Index {
10481090
: _ns.replacingOccurrences(of: target, with: replacement)
10491091
}
10501092

1093+
#if !DEPLOYMENT_RUNTIME_SWIFT
10511094
// - (NSString *)
10521095
// stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
10531096

@@ -1061,6 +1104,7 @@ extension StringProtocol where Index == String.Index {
10611104
) -> String? {
10621105
return _ns.replacingPercentEscapes(using: encoding.rawValue)
10631106
}
1107+
#endif
10641108

10651109
// - (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
10661110

@@ -1129,6 +1173,7 @@ extension StringProtocol where Index == String.Index {
11291173

11301174
// - (nullable NSString *)stringByApplyingTransform:(NSString *)transform reverse:(BOOL)reverse NS_AVAILABLE(10_11, 9_0);
11311175

1176+
#if !DEPLOYMENT_RUNTIME_SWIFT
11321177
/// Perform string transliteration.
11331178
@available(OSX 10.11, iOS 9.0, *)
11341179
public func applyingTransform(
@@ -1175,6 +1220,7 @@ extension StringProtocol where Index == String.Index {
11751220
}
11761221
}
11771222
}
1223+
#endif
11781224

11791225
// - (void)
11801226
// enumerateSubstringsInRange:(NSRange)range
@@ -1385,6 +1431,7 @@ extension StringProtocol where Index == String.Index {
13851431
return _range(_ns.lineRange(for: _toNSRange(aRange.relative(to: self))))
13861432
}
13871433

1434+
#if !DEPLOYMENT_RUNTIME_SWIFT
13881435
// - (NSArray *)
13891436
// linguisticTagsInRange:(NSRange)range
13901437
// scheme:(NSString *)tagScheme
@@ -1432,6 +1479,7 @@ extension StringProtocol where Index == String.Index {
14321479
return _range(
14331480
_ns.paragraphRange(for: _toNSRange(aRange.relative(to: self))))
14341481
}
1482+
#endif
14351483

14361484
// - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet
14371485
//
@@ -1553,6 +1601,7 @@ extension StringProtocol where Index == String.Index {
15531601
_ns.localizedStandardRange(of: string._ephemeralString))
15541602
}
15551603

1604+
#if !DEPLOYMENT_RUNTIME_SWIFT
15561605
// - (NSString *)
15571606
// stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
15581607

@@ -1566,6 +1615,7 @@ extension StringProtocol where Index == String.Index {
15661615
) -> String? {
15671616
return _ns.addingPercentEscapes(using: encoding.rawValue)
15681617
}
1618+
#endif
15691619

15701620
//===--- From the 10.10 release notes; not in public documentation ------===//
15711621
// No need to make these unavailable on earlier OSes, since they can
@@ -1874,6 +1924,7 @@ extension StringProtocol {
18741924
fatalError("unavailable function can't be called")
18751925
}
18761926

1927+
#if !DEPLOYMENT_RUNTIME_SWIFT
18771928
@available(*, unavailable, renamed: "enumerateLinguisticTags(in:scheme:options:orthography:_:)")
18781929
public func enumerateLinguisticTagsIn(
18791930
_ range: Range<Index>,
@@ -1885,6 +1936,7 @@ extension StringProtocol {
18851936
) {
18861937
fatalError("unavailable function can't be called")
18871938
}
1939+
#endif
18881940

18891941
@available(*, unavailable, renamed: "enumerateSubstrings(in:options:_:)")
18901942
public func enumerateSubstringsIn(
@@ -1941,6 +1993,7 @@ extension StringProtocol {
19411993
fatalError("unavailable function can't be called")
19421994
}
19431995

1996+
#if !DEPLOYMENT_RUNTIME_SWIFT
19441997
@available(*, unavailable, renamed: "linguisticTags(in:scheme:options:orthography:tokenRanges:)")
19451998
public func linguisticTagsIn(
19461999
_ range: Range<Index>,
@@ -1951,6 +2004,7 @@ extension StringProtocol {
19512004
) -> [String] {
19522005
fatalError("unavailable function can't be called")
19532006
}
2007+
#endif
19542008

19552009
@available(*, unavailable, renamed: "lowercased(with:)")
19562010
public func lowercaseStringWith(_ locale: Locale?) -> String {

0 commit comments

Comments
 (0)