@@ -124,6 +124,39 @@ extension String {
124
124
self = " "
125
125
}
126
126
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
+
127
160
/// Creates a new string by copying and validating the null-terminated UTF-8
128
161
/// data referenced by the given pointer.
129
162
///
@@ -149,7 +182,11 @@ extension String {
149
182
/// }
150
183
/// // Prints "nil"
151
184
///
185
+ /// Note: This initializer is deprecated. Use
186
+ /// `String.init?(validatingCString:)` instead.
187
+ ///
152
188
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
189
+ @available ( * , deprecated, renamed: " String.init(validatingCString:) " )
153
190
public init ? ( validatingUTF8 cString: UnsafePointer < CChar > ) {
154
191
let len = UTF8 . _nullCodeUnitOffset ( in: cString)
155
192
guard let str = cString. withMemoryRebound ( to: UInt8 . self, capacity: len, {
@@ -162,39 +199,59 @@ extension String {
162
199
163
200
@inlinable
164
201
@_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 {
167
204
_preconditionFailure (
168
- " input of String.init(validatingUTF8 :) must be null-terminated "
205
+ " input of String.init(validatingCString :) must be null-terminated "
169
206
)
170
207
}
171
- guard let string = cString . prefix ( length) . withUnsafeBufferPointer ( {
208
+ let string = nullTerminatedUTF8 . prefix ( length) . withUnsafeBufferPointer {
172
209
$0. withMemoryRebound ( to: UInt8 . self, String . _tryFromUTF8 ( _: ) )
173
- } )
174
- else { return nil }
175
-
210
+ }
211
+ guard let string else { return nil }
176
212
self = string
177
213
}
178
214
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
+
179
229
@inlinable
180
230
@_alwaysEmitIntoClient
181
231
@available ( * , deprecated, message: " Use a copy of the String argument " )
182
232
public init ? ( validatingUTF8 cString: String ) {
183
- self = cString . withCString ( String . init ( cString : ) )
233
+ self . init ( validatingCString : cString )
184
234
}
185
235
186
236
@inlinable
187
237
@_alwaysEmitIntoClient
188
238
@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 {
191
241
_preconditionFailure (
192
242
" input of String.init(validatingUTF8:) must be null-terminated "
193
243
)
194
244
}
195
245
self = " "
196
246
}
197
247
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
+
198
255
/// Creates a new string by copying the null-terminated data referenced by
199
256
/// the given pointer using the specified encoding.
200
257
///
0 commit comments