File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,29 @@ extension String {
49
49
UnsafeBufferPointer ( start: nullTerminatedUTF8. _asUInt8, count: len) ) . 0
50
50
}
51
51
52
+ @inlinable
53
+ @_alwaysEmitIntoClient
54
+ public init ( cString nullTerminatedUTF8: [ CChar ] ) {
55
+ self = nullTerminatedUTF8. withUnsafeBytes {
56
+ String ( _checkingCString: $0. assumingMemoryBound ( to: UInt8 . self) )
57
+ }
58
+ }
59
+
60
+ @_alwaysEmitIntoClient
61
+ private init ( _checkingCString bytes: UnsafeBufferPointer < UInt8 > ) {
62
+ guard let length = bytes. firstIndex ( of: 0 ) else {
63
+ _preconditionFailure (
64
+ " input of String.init(cString:) must be null-terminated "
65
+ )
66
+ }
67
+ self = String . _fromUTF8Repairing (
68
+ UnsafeBufferPointer (
69
+ start: bytes. baseAddress. _unsafelyUnwrappedUnchecked,
70
+ count: length
71
+ )
72
+ ) . 0
73
+ }
74
+
52
75
/// Creates a new string by copying the null-terminated UTF-8 data referenced
53
76
/// by the given pointer.
54
77
///
@@ -60,6 +83,14 @@ extension String {
60
83
UnsafeBufferPointer ( start: nullTerminatedUTF8, count: len) ) . 0
61
84
}
62
85
86
+ @inlinable
87
+ @_alwaysEmitIntoClient
88
+ public init ( cString nullTerminatedUTF8: [ UInt8 ] ) {
89
+ self = nullTerminatedUTF8. withUnsafeBufferPointer {
90
+ String ( _checkingCString: $0)
91
+ }
92
+ }
93
+
63
94
/// Creates a new string by copying and validating the null-terminated UTF-8
64
95
/// data referenced by the given pointer.
65
96
///
You can’t perform that action at this time.
0 commit comments