Skip to content

Commit ea9bb20

Browse files
authored
Merge pull request #65754 from hyp/eng/5.9/c41774188cf345a645c81ab6270bc75bb5663a69
[5.9][interop][SwiftToCxx] add swift::String overlay constructor for const…
2 parents ec192f6 + c2e1c10 commit ea9bb20

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ SWIFT_INLINE_THUNK T_0_0 get() const
3535

3636
#ifndef SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY
3737

38+
/// Constructs a Swift string from a C string.
39+
SWIFT_INLINE_THUNK String(const char *cString) noexcept {
40+
if (!cString) {
41+
auto res = _impl::$sS2SycfC();
42+
memcpy(_getOpaquePointer(), &res, sizeof(res));
43+
return;
44+
}
45+
auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(cString);
46+
memcpy(_getOpaquePointer(), &res, sizeof(res));
47+
}
48+
3849
/// Constructs a Swift string from a C++ string.
3950
SWIFT_INLINE_THUNK String(const std::string &str) noexcept {
4051
auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(str.c_str());

test/Interop/SwiftToCxx/stdlib/string/string-conversions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ int main() {
3434
{
3535
auto s = String("hello world");
3636
printString(s);
37+
swift::String s2 = "Hello literal";
38+
printString(s2);
39+
const char *literal = "Test literal via ptr";
40+
printString(literal);
41+
swift::String s3 = nullptr;
42+
printString(s3);
3743
}
3844
// CHECK: '''hello world'''
45+
// CHECK-NEXT: '''Hello literal'''
46+
// CHECK-NEXT: '''Test literal via ptr'''
47+
// CHECK-NEXT: ''''''
3948

4049
{
4150
std::string str = "test std::string";

0 commit comments

Comments
 (0)