@@ -42,7 +42,8 @@ extension String {
42
42
/// }
43
43
/// // Prints "Caf�"
44
44
///
45
- /// - Parameter nullTerminatedUTF8: A pointer to a null-terminated UTF-8 code sequence.
45
+ /// - Parameter nullTerminatedUTF8:
46
+ /// A pointer to a null-terminated sequence of UTF-8 code units.
46
47
public init ( cString nullTerminatedUTF8: UnsafePointer < CChar > ) {
47
48
let len = UTF8 . _nullCodeUnitOffset ( in: nullTerminatedUTF8)
48
49
let buffer = UnsafeBufferPointer ( start: nullTerminatedUTF8, count: len)
@@ -51,8 +52,24 @@ extension String {
51
52
}
52
53
}
53
54
55
+ /// Creates a new string by copying the null-terminated UTF-8 data referenced
56
+ /// by the given array.
57
+ ///
58
+ /// If `cString` contains ill-formed UTF-8 code unit sequences, this
59
+ /// initializer replaces them with the Unicode replacement character
60
+ /// (`"\u{FFFD}"`).
61
+ ///
62
+ /// - Note: This initializer is deprecated. Use the initializer
63
+ /// `String.init(decoding: array, as: UTF8.self)` instead,
64
+ /// remembering that "\0" is a valid character in Swift.
65
+ ///
66
+ /// - Parameter nullTerminatedUTF8:
67
+ /// An array containing a null-terminated sequence of UTF-8 code units.
54
68
@inlinable
55
69
@_alwaysEmitIntoClient
70
+ @available ( swift, deprecated: 6 , message:
71
+ " Use String(decoding: array, as: UTF8.self) instead, after truncating the null termination. "
72
+ )
56
73
public init ( cString nullTerminatedUTF8: [ CChar ] ) {
57
74
self = nullTerminatedUTF8. withUnsafeBufferPointer {
58
75
$0. withMemoryRebound ( to: UInt8 . self, String . init ( _checkingCString: ) )
@@ -91,14 +108,32 @@ extension String {
91
108
///
92
109
/// This is identical to `init(cString: UnsafePointer<CChar>)` but operates on
93
110
/// an unsigned sequence of bytes.
111
+ ///
112
+ /// - Parameter nullTerminatedUTF8:
113
+ /// A pointer to a null-terminated sequence of UTF-8 code units.
94
114
public init ( cString nullTerminatedUTF8: UnsafePointer < UInt8 > ) {
95
115
let len = UTF8 . _nullCodeUnitOffset ( in: nullTerminatedUTF8)
96
116
self = String . _fromUTF8Repairing (
97
117
UnsafeBufferPointer ( start: nullTerminatedUTF8, count: len) ) . 0
98
118
}
99
119
120
+ /// Creates a new string by copying the null-terminated UTF-8 data referenced
121
+ /// by the given array.
122
+ ///
123
+ /// This is identical to `init(cString: [CChar])` but operates on
124
+ /// an unsigned sequence of bytes.
125
+ ///
126
+ /// - Note: This initializer is deprecated. Use the initializer
127
+ /// `String.init(decoding: array, as: UTF8.self)` instead,
128
+ /// remembering that "\0" is a valid character in Swift.
129
+ ///
130
+ /// - Parameter nullTerminatedUTF8:
131
+ /// An array containing a null-terminated UTF-8 code unit sequence.
100
132
@inlinable
101
133
@_alwaysEmitIntoClient
134
+ @available ( swift, deprecated: 6 , message:
135
+ " Use String(decoding: array, as: UTF8.self) instead, after truncating the null termination. "
136
+ )
102
137
public init ( cString nullTerminatedUTF8: [ UInt8 ] ) {
103
138
self = nullTerminatedUTF8. withUnsafeBufferPointer {
104
139
String ( _checkingCString: $0)
@@ -150,7 +185,7 @@ extension String {
150
185
/// // Prints "nil"
151
186
///
152
187
/// - Parameter nullTerminatedUTF8:
153
- /// A pointer to a null-terminated UTF-8 code sequence .
188
+ /// A pointer to a null-terminated sequence of UTF-8 code units .
154
189
@inlinable
155
190
@_alwaysEmitIntoClient
156
191
public init ? ( validatingCString nullTerminatedUTF8: UnsafePointer < CChar > ) {
@@ -183,10 +218,11 @@ extension String {
183
218
/// }
184
219
/// // Prints "nil"
185
220
///
186
- /// Note: This initializer is deprecated . Use
187
- /// `String.init?(validatingCString:)` instead.
221
+ /// - Note: This initializer has been renamed . Use
222
+ /// `String.init?(validatingCString:)` instead.
188
223
///
189
- /// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
224
+ /// - Parameter cString:
225
+ /// A pointer to a null-terminated sequence of UTF-8 code units.
190
226
@available ( swift, deprecated: 6 , renamed: " String.init(validatingCString:) " )
191
227
public init ? ( validatingUTF8 cString: UnsafePointer < CChar > ) {
192
228
let len = UTF8 . _nullCodeUnitOffset ( in: cString)
@@ -198,8 +234,23 @@ extension String {
198
234
self = str
199
235
}
200
236
237
+ /// Creates a new string by copying and validating the null-terminated UTF-8
238
+ /// data referenced by the given array.
239
+ ///
240
+ /// This initializer does not try to repair ill-formed UTF-8 code unit
241
+ /// sequences. If any are found, the result of the initializer is `nil`.
242
+ ///
243
+ /// - Note: This initializer is deprecated. Use the initializer
244
+ /// `String.init?(validating: array, as: UTF8.self)` instead,
245
+ /// remembering that "\0" is a valid character in Swift.
246
+ ///
247
+ /// - Parameter nullTerminatedUTF8:
248
+ /// An array containing a null-terminated sequence of UTF-8 code units.
201
249
@inlinable
202
250
@_alwaysEmitIntoClient
251
+ @available ( swift, deprecated: 6 , message:
252
+ " Use String(validating: array, as: UTF8.self) instead, after truncating the null termination. "
253
+ )
203
254
public init ? ( validatingCString nullTerminatedUTF8: [ CChar ] ) {
204
255
guard let length = nullTerminatedUTF8. firstIndex ( of: 0 ) else {
205
256
_preconditionFailure (
@@ -213,9 +264,23 @@ extension String {
213
264
self = string
214
265
}
215
266
267
+ /// Creates a new string by copying and validating the null-terminated UTF-8
268
+ /// data referenced by the given array.
269
+ ///
270
+ /// This initializer does not try to repair ill-formed UTF-8 code unit
271
+ /// sequences. If any are found, the result of the initializer is `nil`.
272
+ ///
273
+ /// - Note: This initializer is deprecated. Use the initializer
274
+ /// `String.init?(validating: array, as: UTF8.self)` instead,
275
+ /// remembering that "\0" is a valid character in Swift.
276
+ ///
277
+ /// - Parameter cString:
278
+ /// An array containing a null-terminated sequence of UTF-8 code units.
216
279
@inlinable
217
280
@_alwaysEmitIntoClient
218
- @available ( swift, deprecated: 6 , renamed: " String.init(validatingCString:) " )
281
+ @available ( swift, deprecated: 6 , message:
282
+ " Use String(validating: array, as: UTF8.self) instead, after truncating the null termination. "
283
+ )
219
284
public init ? ( validatingUTF8 cString: [ CChar ] ) {
220
285
self . init ( validatingCString: cString)
221
286
}
@@ -240,7 +305,7 @@ extension String {
240
305
public init ? ( validatingCString nullTerminatedUTF8: inout CChar ) {
241
306
guard nullTerminatedUTF8 == 0 else {
242
307
_preconditionFailure (
243
- " input of String.init(validatingUTF8 :) must be null-terminated "
308
+ " input of String.init(validatingCString :) must be null-terminated "
244
309
)
245
310
}
246
311
self = " "
@@ -284,8 +349,8 @@ extension String {
284
349
/// // Prints "Optional((result: "Caf�", repairsMade: true))"
285
350
///
286
351
/// - Parameters:
287
- /// - cString: A pointer to a null-terminated code sequence encoded in
288
- /// `encoding`.
352
+ /// - cString: A pointer to a null-terminated sequence of
353
+ /// code units encoded in `encoding`.
289
354
/// - encoding: The Unicode encoding of the data referenced by `cString`.
290
355
/// - isRepairing: Pass `true` to create a new string, even when the data
291
356
/// referenced by `cString` contains ill-formed sequences. Ill-formed
@@ -329,9 +394,7 @@ extension String {
329
394
codeUnits, encoding: encoding, repair: isRepairing)
330
395
}
331
396
332
- @_specialize ( where Encoding == Unicode. UTF8 )
333
- @_specialize ( where Encoding == Unicode. UTF16 )
334
- @inlinable // Fold away specializations
397
+ @inlinable
335
398
@_alwaysEmitIntoClient
336
399
public static func decodeCString< Encoding: _UnicodeEncoding > (
337
400
_ cString: [ Encoding . CodeUnit ] ,
@@ -365,8 +428,6 @@ extension String {
365
428
}
366
429
}
367
430
368
- @_specialize ( where Encoding == Unicode. UTF8 )
369
- @_specialize ( where Encoding == Unicode. UTF16 )
370
431
@inlinable
371
432
@_alwaysEmitIntoClient
372
433
@available ( * , deprecated, message: " Use a copy of the String argument " )
@@ -382,8 +443,6 @@ extension String {
382
443
}
383
444
}
384
445
385
- @_specialize ( where Encoding == Unicode. UTF8 )
386
- @_specialize ( where Encoding == Unicode. UTF16 )
387
446
@inlinable
388
447
@_alwaysEmitIntoClient
389
448
@available ( * , deprecated, message: " Use String(_ scalar: Unicode.Scalar) " )
@@ -400,58 +459,74 @@ extension String {
400
459
return ( " " , false )
401
460
}
402
461
403
- /// Creates a string from the null-terminated sequence of bytes at the given
404
- /// pointer.
462
+ /// Creates a new string by copying the null-terminated sequence of code units
463
+ /// referenced by the given pointer.
464
+ ///
465
+ /// If `nullTerminatedCodeUnits` contains ill-formed code unit sequences, this
466
+ /// initializer replaces them with the Unicode replacement character
467
+ /// (`"\u{FFFD}"`).
405
468
///
406
469
/// - Parameters:
407
- /// - nullTerminatedCodeUnits: A pointer to a sequence of contiguous code
408
- /// units in the encoding specified in `sourceEncoding`, ending just
409
- /// before the first zero code unit.
410
- /// - sourceEncoding: The encoding in which the code units should be
470
+ /// - nullTerminatedCodeUnits: A pointer to a null-terminated sequence of
471
+ /// code units encoded in `encoding`.
472
+ /// - encoding: The encoding in which the code units should be
411
473
/// interpreted.
412
474
@_specialize ( where Encoding == Unicode. UTF8 )
413
475
@_specialize ( where Encoding == Unicode. UTF16 )
414
476
@inlinable // Fold away specializations
415
477
public init < Encoding: Unicode . Encoding > (
416
478
decodingCString nullTerminatedCodeUnits: UnsafePointer < Encoding . CodeUnit > ,
417
- as sourceEncoding : Encoding . Type
479
+ as encoding : Encoding . Type
418
480
) {
419
- self = String . decodeCString ( nullTerminatedCodeUnits, as: sourceEncoding ) !. 0
481
+ self = String . decodeCString ( nullTerminatedCodeUnits, as: encoding ) !. 0
420
482
}
421
483
422
- @_specialize ( where Encoding == Unicode. UTF8 )
423
- @_specialize ( where Encoding == Unicode. UTF16 )
424
- @inlinable // Fold away specializations
484
+ /// Creates a new string by copying the null-terminated sequence of code units
485
+ /// referenced by the given array.
486
+ ///
487
+ /// If `nullTerminatedCodeUnits` contains ill-formed code unit sequences, this
488
+ /// initializer replaces them with the Unicode replacement character
489
+ /// (`"\u{FFFD}"`).
490
+ ///
491
+ /// - Note: This initializer is deprecated. Use the initializer
492
+ /// `String.init(decoding: array, as: Encoding.self)` instead,
493
+ /// remembering that "\0" is a valid character in Swift.
494
+ ///
495
+ /// - Parameters:
496
+ /// - nullTerminatedCodeUnits: An array containing a null-terminated
497
+ /// sequence of code units encoded in `encoding`.
498
+ /// - encoding: The encoding in which the code units should be
499
+ /// interpreted.
500
+ @inlinable
425
501
@_alwaysEmitIntoClient
502
+ @available ( swift, deprecated: 6 , message:
503
+ " Use String(decoding: array, as: Encoding.self) instead, after truncating the null termination. "
504
+ )
426
505
public init < Encoding: Unicode . Encoding > (
427
506
decodingCString nullTerminatedCodeUnits: [ Encoding . CodeUnit ] ,
428
- as sourceEncoding : Encoding . Type
507
+ as encoding : Encoding . Type
429
508
) {
430
- self = String . decodeCString ( nullTerminatedCodeUnits, as: sourceEncoding ) !. 0
509
+ self = String . decodeCString ( nullTerminatedCodeUnits, as: encoding ) !. 0
431
510
}
432
511
433
- @_specialize ( where Encoding == Unicode. UTF8 )
434
- @_specialize ( where Encoding == Unicode. UTF16 )
435
512
@inlinable
436
513
@_alwaysEmitIntoClient
437
514
@available ( * , deprecated, message: " Use a copy of the String argument " )
438
515
public init < Encoding: _UnicodeEncoding > (
439
516
decodingCString nullTerminatedCodeUnits: String ,
440
- as sourceEncoding : Encoding . Type
517
+ as encoding : Encoding . Type
441
518
) {
442
- self = nullTerminatedCodeUnits. withCString ( encodedAs: sourceEncoding ) {
443
- String ( decodingCString: $0, as: sourceEncoding . self)
519
+ self = nullTerminatedCodeUnits. withCString ( encodedAs: encoding ) {
520
+ String ( decodingCString: $0, as: encoding . self)
444
521
}
445
522
}
446
523
447
- @_specialize ( where Encoding == Unicode. UTF8 )
448
- @_specialize ( where Encoding == Unicode. UTF16 )
449
- @inlinable // Fold away specializations
524
+ @inlinable
450
525
@_alwaysEmitIntoClient
451
526
@available ( * , deprecated, message: " Use String(_ scalar: Unicode.Scalar) " )
452
527
public init < Encoding: Unicode . Encoding > (
453
528
decodingCString nullTerminatedCodeUnits: inout Encoding . CodeUnit ,
454
- as sourceEncoding : Encoding . Type
529
+ as encoding : Encoding . Type
455
530
) {
456
531
guard nullTerminatedCodeUnits == 0 else {
457
532
_preconditionFailure (
0 commit comments