Skip to content

Commit 14893a0

Browse files
authored
Merge pull request #2821 from Molanda/implement_string_varargs
Added CVarArg support to NSString and String
2 parents 3184ab9 + b7c4b8c commit 14893a0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Sources/Foundation/NSString.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,3 +1644,24 @@ extension NSString : _StructTypeBridgeable {
16441644
return _StructType._unconditionallyBridgeFromObjectiveC(self)
16451645
}
16461646
}
1647+
1648+
extension NSString : CVarArg {
1649+
@inlinable // c-abi
1650+
public var _cVarArgEncoding: [Int] {
1651+
return _encodeBitsAsWords(unsafeBitCast(self, to: AnyObject.self))
1652+
}
1653+
}
1654+
1655+
#if !_runtime(_ObjC)
1656+
extension String : CVarArg, _CVarArgObject {
1657+
@inlinable // c-abi
1658+
public var _cVarArgObject: CVarArg {
1659+
return NSString(string: self)
1660+
}
1661+
1662+
@inlinable // c-abi
1663+
public var _cVarArgEncoding: [Int] {
1664+
fatalError("_cVarArgEncoding must be called on NSString instead")
1665+
}
1666+
}
1667+
#endif

Tests/Foundation/Tests/TestNSString.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,15 @@ class TestNSString: LoopbackServerTest {
813813
}
814814
}
815815

816+
func test_initializeWithFormat4() {
817+
let argument: [CVarArg] = ["One", "Two", "Three"]
818+
withVaList(argument) {
819+
pointer in
820+
let string = NSString(format: "Testing %@ %@ %@", arguments: pointer)
821+
XCTAssertEqual(string, "Testing One Two Three")
822+
}
823+
}
824+
816825
func test_appendingPathComponent() {
817826
do {
818827
let path: NSString = "/tmp"
@@ -1677,6 +1686,7 @@ class TestNSString: LoopbackServerTest {
16771686
("test_initializeWithFormat", test_initializeWithFormat),
16781687
("test_initializeWithFormat2", test_initializeWithFormat2),
16791688
("test_initializeWithFormat3", test_initializeWithFormat3),
1689+
("test_initializeWithFormat4", test_initializeWithFormat4),
16801690
("test_appendingPathComponent", test_appendingPathComponent),
16811691
("test_deletingLastPathComponent", test_deletingLastPathComponent),
16821692
("test_getCString_simple", test_getCString_simple),

0 commit comments

Comments
 (0)