@@ -383,35 +383,21 @@ extension String {
383383 return _core. elementWidth == 1 ? _core. startASCII : nil
384384 }
385385
386- /// A contiguously stored null-terminated UTF-8 representation of
387- /// the string.
386+ /// A contiguously stored null-terminated UTF-8 representation of the string.
388387 ///
389- /// To access the underlying memory, invoke
390- /// `withUnsafeBufferPointer` on the array.
388+ /// To access the underlying memory, invoke `withUnsafeBufferPointer` on the
389+ /// array.
391390 ///
392391 /// let s = "Hello!"
393- /// let bytes = s.nulTerminatedUTF8
392+ /// let bytes = s.utf8CString
394393 /// print(bytes)
395394 /// // Prints "[72, 101, 108, 108, 111, 33, 0]"
396- ///
395+ ///
397396 /// bytes.withUnsafeBufferPointer { ptr in
398- /// print(strlen(UnsafePointer( ptr.baseAddress!) ))
397+ /// print(strlen(ptr.baseAddress!))
399398 /// }
400399 /// // Prints "6"
401- public var nulTerminatedUTF8 : ContiguousArray < UTF8 . CodeUnit > {
402- var result = ContiguousArray < UTF8 . CodeUnit > ( )
403- result. reserveCapacity ( utf8. count + 1 )
404- result += utf8
405- result. append ( 0 )
406- return result
407- }
408-
409- /// A contiguously stored null-terminated UTF-8 representation of
410- /// the string.
411- ///
412- /// This is a variation on nulTerminatedUTF8 that creates an array
413- /// of element type CChar for use with CString API's.
414- public var nulTerminatedUTF8CString : ContiguousArray < CChar > {
400+ public var utf8CString : ContiguousArray < CChar > {
415401 var result = ContiguousArray < CChar > ( )
416402 result. reserveCapacity ( utf8. count + 1 )
417403 for c in utf8 {
@@ -428,7 +414,11 @@ extension String {
428414 if ptr != nil {
429415 return try body ( UnsafeBufferPointer ( start: ptr, count: _core. count) )
430416 }
431- return try nulTerminatedUTF8. withUnsafeBufferPointer ( body)
417+ var nullTerminatedUTF8 = ContiguousArray < UTF8 . CodeUnit > ( )
418+ nullTerminatedUTF8. reserveCapacity ( utf8. count + 1 )
419+ nullTerminatedUTF8 += utf8
420+ nullTerminatedUTF8. append ( 0 )
421+ return try nullTerminatedUTF8. withUnsafeBufferPointer ( body)
432422 }
433423
434424 /// Creates a string corresponding to the given sequence of UTF-8 code units.
@@ -725,3 +715,9 @@ extension String.UTF8View : CustomPlaygroundQuickLookable {
725715 }
726716}
727717
718+ extension String {
719+ @available ( * , unavailable, message: " Please use String.utf8CString instead. " )
720+ public var nulTerminatedUTF8 : ContiguousArray < UTF8 . CodeUnit > {
721+ Builtin . unreachable ( )
722+ }
723+ }
0 commit comments