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 @@ -229,13 +229,9 @@ extension String {
229
229
///
230
230
/// - Complexity: O(*n*), where *n* is the number of bytes in the C++ string.
231
231
public init ( _ cxxString: std . string ) {
232
- let buffer = UnsafeBufferPointer < CChar > (
233
- start: cxxString. __c_strUnsafe ( ) ,
234
- count: cxxString. size ( ) )
235
- self = buffer. withMemoryRebound ( to: UInt8 . self) {
232
+ self = cxxString. withUTF8 {
236
233
String ( decoding: $0, as: UTF8 . self)
237
234
}
238
- withExtendedLifetime ( cxxString) { }
239
235
}
240
236
241
237
/// Creates a String having the same content as the given C++ UTF-16 string.
@@ -272,3 +268,17 @@ extension String {
272
268
withExtendedLifetime ( cxxU32String) { }
273
269
}
274
270
}
271
+
272
+ // MARK: Converting a C++ string to a C string
273
+
274
+ extension std . string {
275
+ @inlinable
276
+ public borrowing func withUTF8< Result> (
277
+ _ body: ( UnsafeBufferPointer < UInt8 > ) throws -> Result
278
+ ) rethrows -> Result {
279
+ let buffer = UnsafeBufferPointer < CChar > (
280
+ start: self . __c_strUnsafe ( ) ,
281
+ count: self . size ( ) )
282
+ return try buffer. withMemoryRebound ( to: UInt8 . self, body)
283
+ }
284
+ }
Original file line number Diff line number Diff line change @@ -377,4 +377,28 @@ StdStringOverlayTestSuite.test("std::string from C string") {
377
377
expectEqual ( str, std. string ( " abc " ) )
378
378
}
379
379
380
+ StdStringOverlayTestSuite . test ( " std::string to UTF-8 " ) {
381
+ std. string ( ) . withUTF8 { ptr in
382
+ expectEqual ( ptr. count, 0 )
383
+ }
384
+ std. string ( " abc " ) . withUTF8 { ptr in
385
+ expectEqual ( ptr. count, 3 )
386
+ expectEqual ( ptr. baseAddress? . pointee, 97 )
387
+ expectEqual ( ptr. baseAddress? . successor ( ) . pointee, 98 )
388
+ expectEqual ( ptr. baseAddress? . successor ( ) . successor ( ) . pointee, 99 )
389
+ }
390
+
391
+ let bytes : [ UInt8 ] = [ 0xE1 , 0xC1 , 0xAC ]
392
+ var str = std. string ( )
393
+ for byte in bytes {
394
+ str. push_back ( CChar ( bitPattern: byte) )
395
+ }
396
+ str. withUTF8 { ptr in
397
+ expectEqual ( ptr. count, 3 )
398
+ expectEqual ( ptr. baseAddress? . pointee, 0xE1 )
399
+ expectEqual ( ptr. baseAddress? . successor ( ) . pointee, 0xC1 )
400
+ expectEqual ( ptr. baseAddress? . successor ( ) . successor ( ) . pointee, 0xAC )
401
+ }
402
+ }
403
+
380
404
runAllTests ( )
You can’t perform that action at this time.
0 commit comments