0001    //
0002    //  JSONEncodingDetector.swift
0003    //  Freddy
0004    //
0005    //  Created by Robert Edwards on 1/27/16.
0006    //  Copyright © 2016 Big Nerd Ranch. All rights reserved.
0007    //
0008    
0009    /// Struct for attempting to detect the Unicode encoding used with the data supplied to the JSONParser
0010    public struct JSONEncodingDetector
JSONEncodingDetector.swift:65
        if let prefixInfo = JSONEncodingDetector.encodingFromBOM(prefix) {
JSONParser.swift:175
        let encodingPrefixInformation = JSONEncodingDetector.detectEncoding(header)
JSONParser.swift:176
        guard JSONEncodingDetector.supportedEncodings.contains(encodingPrefixInformation.encoding) else {
JSONParser.swift:689
        case InvalidUnicodeStreamEncoding(detectedEncoding: JSONEncodingDetector.Encoding)
{ 0011 0012 //// The Unicode encodings looked for during detection 0013 public enum Encoding
JSONEncodingDetector.swift:27
    public static let supportedEncodings: [Encoding] = [.UTF8]
JSONEncodingDetector.swift:29
    typealias ByteStreamPrefixInformation = (encoding: Encoding, byteOrderMarkLength: Int)
JSONParser.swift:689
        case InvalidUnicodeStreamEncoding(detectedEncoding: JSONEncodingDetector.Encoding)
{ 0014 //// UTF-8 0015 case UTF8
JSONEncodingDetector.swift:27
    public static let supportedEncodings: [Encoding] = [.UTF8]
JSONEncodingDetector.swift:62
            return (.UTF8, 0)
JSONEncodingDetector.swift:78
                return (.UTF8, 0)
JSONEncodingDetector.swift:101
            return (.UTF8, 3)
0016 //// UTF-16 Little Endian 0017 case UTF16LE
JSONEncodingDetector.swift:76
                return (.UTF16LE, 0)
JSONEncodingDetector.swift:105
            return (.UTF16LE, 2)
0018 //// UTF-16 Big Endian 0019 case UTF16BE
JSONEncodingDetector.swift:74
                return (.UTF16BE, 0)
JSONEncodingDetector.swift:97
            return (.UTF16BE, 2)
0020 //// UTF-32 Little Endian 0021 case UTF32LE
JSONEncodingDetector.swift:72
                return (.UTF32LE, 0)
JSONEncodingDetector.swift:103
            return (.UTF32LE, 4)
0022 //// UTF-32 Big Endian 0023 case UTF32BE
JSONEncodingDetector.swift:70
                return (.UTF32BE, 0)
JSONEncodingDetector.swift:99
            return (.UTF32BE, 4)
0024 } 0025 0026 //// The Unicode encodings supported by JSONParser.swift 0027 public static let supportedEncodings
JSONParser.swift:176
        guard JSONEncodingDetector.supportedEncodings.contains(encodingPrefixInformation.encoding) else {
: [Encoding] = [.UTF8] 0028 0029 typealias ByteStreamPrefixInformation
JSONEncodingDetector.swift:59
    static func detectEncoding(header: Slice<UnsafeBufferPointer<UInt8>>) -> ByteStreamPrefixInformation {
JSONEncodingDetector.swift:94
    private static func encodingFromBOM(prefix: EncodingBytePrefix) -> ByteStreamPrefixInformation? {
= (encoding: Encoding, byteOrderMarkLength: Int) 0030 0031 //// Attempts to detect the Unicode encoding used for a given set of data. 0032 //// 0033 //// This function initially looks for a Byte Order Mark in the following form: 0034 //// 0035 //// Bytes | Encoding Form 0036 //// --------------|---------------- 0037 //// 00 00 FE FF | UTF-32, big-endian 0038 //// FF FE 00 00 | UTF-32, little-endian 0039 //// FE FF | UTF-16, big-endian 0040 //// FF FE | UTF-16, little-endian 0041 //// EF BB BF | UTF-8 0042 //// 0043 //// If a BOM is not found then we detect using the following approach described in 0044 //// the JSON RFC http://www.ietf.org/rfc/rfc4627.txt: 0045 //// 0046 //// Since the first two characters of a JSON text will always be ASCII 0047 //// characters [RFC0020], it is possible to determine whether an octet 0048 //// stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking 0049 //// at the pattern of nulls in the first four octets. 0050 //// 0051 //// 00 00 00 xx UTF-32BE 0052 //// 00 xx 00 xx UTF-16BE 0053 //// xx 00 00 00 UTF-32LE 0054 //// xx 00 xx 00 UTF-16LE 0055 //// xx xx xx xx UTF-8 0056 //// 0057 //// - parameter header: The front Slice of data being read and evaluated. 0058 //// - returns: A tuple containing the detected Unicode encoding and the lenght of the byte order mark. 0059 static func detectEncoding
JSONParser.swift:175
        let encodingPrefixInformation = JSONEncodingDetector.detectEncoding(header)
(header: Slice<UnsafeBufferPointer<UInt8>>) -> ByteStreamPrefixInformation { 0060 0061 guard let prefix = prefixFromHeader(header) else { 0062 return (.UTF8, 0) 0063 } 0064 0065 if let prefixInfo = JSONEncodingDetector.encodingFromBOM(prefix) { 0066 return prefixInfo 0067 } else { 0068 switch prefix { 0069 case(0, 0, 0?, _): 0070 return (.UTF32BE, 0) 0071 case(_, 0, 0?, 0?): 0072 return (.UTF32LE, 0) 0073 case (0, _, 0?, _), (0, _, _, _): 0074 return (.UTF16BE, 0) 0075 case (_, 0, _, 0?), (_, 0, _, _): 0076 return (.UTF16LE, 0) 0077 default: 0078 return (.UTF8, 0) 0079 } 0080 } 0081 } 0082 0083 private typealias EncodingBytePrefix
JSONEncodingDetector.swift:85
    private static func prefixFromHeader(header: Slice<UnsafeBufferPointer<UInt8>>) -> EncodingBytePrefix? {
JSONEncodingDetector.swift:94
    private static func encodingFromBOM(prefix: EncodingBytePrefix) -> ByteStreamPrefixInformation? {
= (UInt8, UInt8, UInt8?, UInt8?) 0084 0085 private static func prefixFromHeader
JSONEncodingDetector.swift:61
        guard let prefix = prefixFromHeader(header) else {
(header: Slice<UnsafeBufferPointer<UInt8>>) -> EncodingBytePrefix? { 0086 if header.count >= 4 { 0087 return(header[0], header[1], header[2], header[3]) 0088 } else if header.count >= 2 { 0089 return (header[0], header[1], nil, nil) 0090 } 0091 return nil 0092 } 0093 0094 private static func encodingFromBOM
JSONEncodingDetector.swift:65
        if let prefixInfo = JSONEncodingDetector.encodingFromBOM(prefix) {
(prefix: EncodingBytePrefix) -> ByteStreamPrefixInformation? { 0095 switch prefix { 0096 case(0xFE, 0xFF, _, _): 0097 return (.UTF16BE, 2) 0098 case(0x00, 0x00, 0xFE?, 0xFF?): 0099 return (.UTF32BE, 4) 0100 case(0xEF, 0xBB, 0xBF?, _): 0101 return (.UTF8, 3) 0102 case(0xFF, 0xFE, 0?, 0?): 0103 return (.UTF32LE, 4) 0104 case(0xFF, 0xFE, _, _): 0105 return (.UTF16LE, 2) 0106 default: 0107 return nil 0108 } 0109 } 0110 } 0111