@@ -1248,33 +1248,37 @@ extension NSString {
1248
1248
public convenience init ( contentsOf url: URL , usedEncoding enc: UnsafeMutablePointer < UInt > ? ) throws {
1249
1249
let readResult = try NSData ( contentsOf: url, options: [ ] )
1250
1250
1251
- var offset = 0
1251
+ let encoding : UInt
1252
+ let offset : Int
1252
1253
let bytePtr = readResult. bytes. bindMemory ( to: UInt8 . self, capacity: readResult. length)
1253
1254
if readResult. length >= 4 && bytePtr [ 0 ] == 0xFF && bytePtr [ 1 ] == 0xFE && bytePtr [ 2 ] == 0x00 && bytePtr [ 3 ] == 0x00 {
1254
- enc ? . pointee = String . Encoding. utf32LittleEndian. rawValue
1255
+ encoding = String . Encoding. utf32LittleEndian. rawValue
1255
1256
offset = 4
1256
1257
}
1257
1258
else if readResult. length >= 2 && bytePtr [ 0 ] == 0xFE && bytePtr [ 1 ] == 0xFF {
1258
- enc ? . pointee = String . Encoding. utf16BigEndian. rawValue
1259
+ encoding = String . Encoding. utf16BigEndian. rawValue
1259
1260
offset = 2
1260
1261
}
1261
1262
else if readResult. length >= 2 && bytePtr [ 0 ] == 0xFF && bytePtr [ 1 ] == 0xFE {
1262
- enc ? . pointee = String . Encoding. utf16LittleEndian. rawValue
1263
+ encoding = String . Encoding. utf16LittleEndian. rawValue
1263
1264
offset = 2
1264
1265
}
1265
1266
else if readResult. length >= 4 && bytePtr [ 0 ] == 0x00 && bytePtr [ 1 ] == 0x00 && bytePtr [ 2 ] == 0xFE && bytePtr [ 3 ] == 0xFF {
1266
- enc ? . pointee = String . Encoding. utf32BigEndian. rawValue
1267
+ encoding = String . Encoding. utf32BigEndian. rawValue
1267
1268
offset = 4
1268
1269
}
1269
1270
else {
1270
1271
//Need to work on more conditions. This should be the default
1271
- enc? . pointee = String . Encoding. utf8. rawValue
1272
+ encoding = String . Encoding. utf8. rawValue
1273
+ offset = 0
1272
1274
}
1273
1275
1276
+ enc? . pointee = encoding
1277
+
1274
1278
// Since the encoding being passed includes the byte order the BOM wont be checked or skipped, so pass offset to
1275
1279
// manually skip the BOM header.
1276
- guard let enc = enc , let cf = CFStringCreateWithBytes ( kCFAllocatorDefault, bytePtr + offset, readResult. length - offset,
1277
- CFStringConvertNSStringEncodingToEncoding ( enc . pointee ) , true ) else {
1280
+ guard let cf = CFStringCreateWithBytes ( kCFAllocatorDefault, bytePtr + offset, readResult. length - offset,
1281
+ CFStringConvertNSStringEncodingToEncoding ( encoding ) , true ) else {
1278
1282
throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileReadInapplicableStringEncoding. rawValue, userInfo: [
1279
1283
" NSDebugDescription " : " Unable to create a string using the specified encoding. "
1280
1284
] )
@@ -1286,11 +1290,11 @@ extension NSString {
1286
1290
throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileReadInapplicableStringEncoding. rawValue, userInfo: [
1287
1291
" NSDebugDescription " : " Unable to bridge CFString to String. "
1288
1292
] )
1289
- }
1293
+ }
1290
1294
}
1291
1295
1292
1296
public convenience init ( contentsOfFile path: String , usedEncoding enc: UnsafeMutablePointer < UInt > ? ) throws {
1293
- NSUnimplemented ( )
1297
+ try self . init ( contentsOf : URL ( fileURLWithPath : path ) , usedEncoding : enc )
1294
1298
}
1295
1299
}
1296
1300
0 commit comments