@@ -273,7 +273,7 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
273
273
}
274
274
275
275
public init ( characters: UnsafePointer < unichar > , length: Int ) {
276
- _storage = String . _fromWellFormedCodeUnitSequence ( UTF16 . self , input : UnsafeBufferPointer ( start: characters, count: length) )
276
+ _storage = String ( decoding : UnsafeBufferPointer ( start: characters, count: length) , as : UTF16 . self )
277
277
}
278
278
279
279
public required convenience init ( unicodeScalarLiteral value: StaticString ) {
@@ -1166,16 +1166,8 @@ extension NSString {
1166
1166
}
1167
1167
1168
1168
public convenience init ? ( utf8String nullTerminatedCString: UnsafePointer < Int8 > ) {
1169
- let count = Int ( strlen ( nullTerminatedCString) )
1170
- if let str = nullTerminatedCString. withMemoryRebound ( to: UInt8 . self, capacity: count, {
1171
- let buffer = UnsafeBufferPointer < UInt8 > ( start: $0, count: count)
1172
- return String . _fromCodeUnitSequence ( UTF8 . self, input: buffer)
1173
- } ) as String ?
1174
- {
1175
- self . init ( str)
1176
- } else {
1177
- return nil
1178
- }
1169
+ guard let str = String ( validatingUTF8: nullTerminatedCString) else { return nil }
1170
+ self . init ( str)
1179
1171
}
1180
1172
1181
1173
public convenience init ( format: String , arguments argList: CVaListPointer ) {
@@ -1357,23 +1349,19 @@ open class NSMutableString : NSString {
1357
1349
}
1358
1350
1359
1351
public required init ( stringLiteral value: StaticString ) {
1360
- if value. hasPointerRepresentation {
1361
- super. init ( String . _fromWellFormedCodeUnitSequence ( UTF8 . self, input: UnsafeBufferPointer ( start: value. utf8Start, count: Int ( value. utf8CodeUnitCount) ) ) )
1362
- } else {
1363
- var uintValue = value. unicodeScalar. value
1364
- super. init ( String . _fromWellFormedCodeUnitSequence ( UTF32 . self, input: UnsafeBufferPointer ( start: & uintValue, count: 1 ) ) )
1365
- }
1352
+ super. init ( value. description)
1366
1353
}
1367
1354
1368
1355
public required init ( string aString: String ) {
1369
1356
super. init ( aString)
1370
1357
}
1371
1358
1372
1359
internal func appendCharacters( _ characters: UnsafePointer < unichar > , length: Int ) {
1360
+ let str = String ( decoding: UnsafeBufferPointer ( start: characters, count: length) , as: UTF16 . self)
1373
1361
if type ( of: self ) == NSMutableString . self {
1374
- _storage. append ( String . _fromWellFormedCodeUnitSequence ( UTF16 . self , input : UnsafeBufferPointer ( start : characters , count : length ) ) )
1362
+ _storage. append ( str )
1375
1363
} else {
1376
- replaceCharacters ( in: NSRange ( location: self . length, length: 0 ) , with: String . _fromWellFormedCodeUnitSequence ( UTF16 . self , input : UnsafeBufferPointer ( start : characters , count : length ) ) )
1364
+ replaceCharacters ( in: NSRange ( location: self . length, length: 0 ) , with: str )
1377
1365
}
1378
1366
}
1379
1367
0 commit comments