File tree Expand file tree Collapse file tree 2 files changed +39
-5
lines changed
test/Interop/Cxx/stdlib/overlay Expand file tree Collapse file tree 2 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -168,13 +168,9 @@ extension String {
168
168
///
169
169
/// - Complexity: O(*n*), where *n* is the number of bytes in the C++ string.
170
170
public init ( _ cxxString: std . string ) {
171
- let buffer = UnsafeBufferPointer < CChar > (
172
- start: cxxString. __c_strUnsafe ( ) ,
173
- count: cxxString. size ( ) )
174
- self = buffer. withMemoryRebound ( to: UInt8 . self) {
171
+ self = cxxString. withUTF8 {
175
172
String ( decoding: $0, as: UTF8 . self)
176
173
}
177
- withExtendedLifetime ( cxxString) { }
178
174
}
179
175
180
176
/// Creates a String having the same content as the given C++ UTF-16 string.
@@ -193,3 +189,17 @@ extension String {
193
189
withExtendedLifetime ( cxxU16String) { }
194
190
}
195
191
}
192
+
193
+ // MARK: Converting a C++ string to a C string
194
+
195
+ extension std . string {
196
+ @inlinable
197
+ public borrowing func withUTF8< Result> (
198
+ _ body: ( UnsafeBufferPointer < UInt8 > ) throws -> Result
199
+ ) rethrows -> Result {
200
+ let buffer = UnsafeBufferPointer < CChar > (
201
+ start: self . __c_strUnsafe ( ) ,
202
+ count: self . size ( ) )
203
+ return try buffer. withMemoryRebound ( to: UInt8 . self, body)
204
+ }
205
+ }
Original file line number Diff line number Diff line change @@ -256,4 +256,28 @@ StdStringOverlayTestSuite.test("std::string from C string") {
256
256
expectEqual ( str, std. string ( " abc " ) )
257
257
}
258
258
259
+ StdStringOverlayTestSuite . test ( " std::string to UTF-8 " ) {
260
+ std. string ( ) . withUTF8 { ptr in
261
+ expectEqual ( ptr. count, 0 )
262
+ }
263
+ std. string ( " abc " ) . withUTF8 { ptr in
264
+ expectEqual ( ptr. count, 3 )
265
+ expectEqual ( ptr. baseAddress? . pointee, 97 )
266
+ expectEqual ( ptr. baseAddress? . successor ( ) . pointee, 98 )
267
+ expectEqual ( ptr. baseAddress? . successor ( ) . successor ( ) . pointee, 99 )
268
+ }
269
+
270
+ let bytes : [ UInt8 ] = [ 0xE1 , 0xC1 , 0xAC ]
271
+ var str = std. string ( )
272
+ for byte in bytes {
273
+ str. push_back ( CChar ( bitPattern: byte) )
274
+ }
275
+ str. withUTF8 { ptr in
276
+ expectEqual ( ptr. count, 3 )
277
+ expectEqual ( ptr. baseAddress? . pointee, 0xE1 )
278
+ expectEqual ( ptr. baseAddress? . successor ( ) . pointee, 0xC1 )
279
+ expectEqual ( ptr. baseAddress? . successor ( ) . successor ( ) . pointee, 0xAC )
280
+ }
281
+ }
282
+
259
283
runAllTests ( )
You can’t perform that action at this time.
0 commit comments