@@ -124,6 +124,39 @@ extension String {
124124 self = " "
125125 }
126126
127+ /// Creates a new string by copying and validating the null-terminated UTF-8
128+ /// data referenced by the given pointer.
129+ ///
130+ /// This initializer does not try to repair ill-formed UTF-8 code unit
131+ /// sequences. If any are found, the result of the initializer is `nil`.
132+ ///
133+ /// The following example calls this initializer with pointers to the
134+ /// contents of two different `CChar` arrays---the first with well-formed
135+ /// UTF-8 code unit sequences and the second with an ill-formed sequence at
136+ /// the end.
137+ ///
138+ /// let validUTF8: [CChar] = [67, 97, 102, -61, -87, 0]
139+ /// validUTF8.withUnsafeBufferPointer { ptr in
140+ /// let s = String(validatingCString: ptr.baseAddress!)
141+ /// print(s)
142+ /// }
143+ /// // Prints "Optional("Café")"
144+ ///
145+ /// let invalidUTF8: [CChar] = [67, 97, 102, -61, 0]
146+ /// invalidUTF8.withUnsafeBufferPointer { ptr in
147+ /// let s = String(validatingCString: ptr.baseAddress!)
148+ /// print(s)
149+ /// }
150+ /// // Prints "nil"
151+ ///
152+ /// - Parameter nullTerminatedUTF8:
153+ /// A pointer to a null-terminated UTF-8 code sequence.
154+ @inlinable
155+ @_alwaysEmitIntoClient
156+ public init ? ( validatingCString nullTerminatedUTF8: UnsafePointer < CChar > ) {
157+ self . init ( validatingUTF8: nullTerminatedUTF8)
158+ }
159+
127160 /// Creates a new string by copying and validating the null-terminated UTF-8
128161 /// data referenced by the given pointer.
129162 ///
@@ -149,7 +182,11 @@ extension String {
149182 /// }
150183 /// // Prints "nil"
151184 ///
185+ /// Note: This initializer is deprecated. Use
186+ /// `String.init?(validatingCString:)` instead.
187+ ///
152188 /// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
189+ @available ( * , deprecated, renamed: " String.init(validatingCString:) " )
153190 public init ? ( validatingUTF8 cString: UnsafePointer < CChar > ) {
154191 let len = UTF8 . _nullCodeUnitOffset ( in: cString)
155192 guard let str = cString. withMemoryRebound ( to: UInt8 . self, capacity: len, {
@@ -162,39 +199,59 @@ extension String {
162199
163200 @inlinable
164201 @_alwaysEmitIntoClient
165- public init ? ( validatingUTF8 cString : [ CChar ] ) {
166- guard let length = cString . firstIndex ( of: 0 ) else {
202+ public init ? ( validatingCString nullTerminatedUTF8 : [ CChar ] ) {
203+ guard let length = nullTerminatedUTF8 . firstIndex ( of: 0 ) else {
167204 _preconditionFailure (
168- " input of String.init(validatingUTF8 :) must be null-terminated "
205+ " input of String.init(validatingCString :) must be null-terminated "
169206 )
170207 }
171- guard let string = cString . prefix ( length) . withUnsafeBufferPointer ( {
208+ let string = nullTerminatedUTF8 . prefix ( length) . withUnsafeBufferPointer {
172209 $0. withMemoryRebound ( to: UInt8 . self, String . _tryFromUTF8 ( _: ) )
173- } )
174- else { return nil }
175-
210+ }
211+ guard let string else { return nil }
176212 self = string
177213 }
178214
215+ @inlinable
216+ @_alwaysEmitIntoClient
217+ @available ( * , deprecated, renamed: " String.init(validatingCString:) " )
218+ public init ? ( validatingUTF8 cString: [ CChar ] ) {
219+ self . init ( validatingCString: cString)
220+ }
221+
222+ @inlinable
223+ @_alwaysEmitIntoClient
224+ @available ( * , deprecated, message: " Use a copy of the String argument " )
225+ public init ? ( validatingCString nullTerminatedUTF8: String ) {
226+ self = nullTerminatedUTF8. withCString ( String . init ( cString: ) )
227+ }
228+
179229 @inlinable
180230 @_alwaysEmitIntoClient
181231 @available ( * , deprecated, message: " Use a copy of the String argument " )
182232 public init ? ( validatingUTF8 cString: String ) {
183- self = cString . withCString ( String . init ( cString : ) )
233+ self . init ( validatingCString : cString )
184234 }
185235
186236 @inlinable
187237 @_alwaysEmitIntoClient
188238 @available ( * , deprecated, message: " Use String(_ scalar: Unicode.Scalar) " )
189- public init ? ( validatingUTF8 cString : inout CChar ) {
190- guard cString == 0 else {
239+ public init ? ( validatingCString nullTerminatedUTF8 : inout CChar ) {
240+ guard nullTerminatedUTF8 == 0 else {
191241 _preconditionFailure (
192242 " input of String.init(validatingUTF8:) must be null-terminated "
193243 )
194244 }
195245 self = " "
196246 }
197247
248+ @inlinable
249+ @_alwaysEmitIntoClient
250+ @available ( * , deprecated, message: " Use String(_ scalar: Unicode.Scalar) " )
251+ public init ? ( validatingUTF8 cString: inout CChar ) {
252+ self . init ( validatingCString: & cString)
253+ }
254+
198255 /// Creates a new string by copying the null-terminated data referenced by
199256 /// the given pointer using the specified encoding.
200257 ///
0 commit comments