Skip to content

Commit 54f3153

Browse files
committed
Copy lazily bridged Cocoa strings correctly without hanging
1 parent 4edddf2 commit 54f3153

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ static_assert(sizeof(_impl::_impl_String) >= 0,
7070
SWIFT_INLINE_THUNK String::operator std::string() const {
7171
auto u = getUtf8();
7272
std::string result;
73-
result.reserve(u.getCount() + 1);
74-
using IndexType = decltype(u.getStartIndex());
75-
for (auto s = u.getStartIndex().getEncodedOffset(),
76-
e = u.getEndIndex().getEncodedOffset();
77-
s != e; s = u.indexOffsetBy(IndexType::init(s), 1).getEncodedOffset()) {
78-
result.push_back(u[IndexType::init(s)]);
73+
result.reserve(u.getCount());
74+
75+
auto end_offset = u.getEndIndex().getEncodedOffset();
76+
for (auto idx = u.getStartIndex(); idx.getEncodedOffset() < end_offset;
77+
idx = u.indexAfter(idx)) {
78+
result.push_back(static_cast<char>(u[idx]));
7979
}
80+
8081
return result;
8182
}
8283

test/Interop/SwiftToCxx/stdlib/string/string-to-nsstring.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,12 @@ int main() {
6969
assert(std::string(nsStr.UTF8String) == "nsstr");
7070
assert([nsStr2 isEqualToString:nsStr]);
7171
}
72+
73+
NSString *nsStrContainingUnicode = @"👨‍💻👩‍💻åäö";
74+
{
75+
swift::String swiftStr = swift::String::init(nsStrContainingUnicode);
76+
assert(std::string(swiftStr) == "👨‍💻👩‍💻åäö");
77+
}
78+
7279
return 0;
7380
}

0 commit comments

Comments
 (0)