0001    // Data.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    
0026    @_exported import System
0027    
0028    public typealias Byte
Data.swift:40
    private var bytes: [Byte]
Data.swift:42
    public init(bytes: [Byte]) {
Data.swift:66
    public init<S: SequenceType where S.Generator.Element == Byte>(_ bytes: S) {
Data.swift:67
        self.bytes = [Byte](bytes)
Data.swift:70
    public init<C: CollectionType where C.Generator.Element == Byte>(_ bytes: C) {
Data.swift:71
        self.bytes = [Byte](bytes)
Data.swift:76
    public func generate() -> IndexingGenerator<[Byte]> {
Data.swift:108
            bytes[bounds] = ArraySlice<Byte>(newValue.bytes)
Data.swift:120
    public init(arrayLiteral bytes: Byte...) {
Data.swift:127
        self.init(bytes: [Byte](string.utf8))
Data.swift:131
        self.init(bytes: [Byte](string.utf8))
Data.swift:135
        self.init(bytes: [Byte](string.utf8))
Data.swift:188
            Array(UnsafeBufferPointer(start: UnsafePointer<Byte>($0), count: sizeof(T)))
Data.swift:192
    public func withUnsafeBufferPointer<R>(@noescape body: (UnsafeBufferPointer<Byte>) throws -> R) rethrows -> R {
Data.swift:196
    public mutating func withUnsafeMutableBufferPointer<R>(@noescape body: (inout UnsafeMutableBufferPointer<Byte>) throws -> R) rethrows -> R {
Data.swift:204
    public mutating func replaceBytesInRange<C: CollectionType where C.Generator.Element == Byte>(subRange: Range<Int>, with newBytes: C) {
Data.swift:212
    public mutating func appendByte(newByte: Byte) {
Data.swift:216
    public mutating func appendBytes<S: SequenceType where S.Generator.Element == Byte>(newBytes: S) {
Data.swift:220
    public mutating func insertByte(newByte: Byte, atIndex i: Int) {
Data.swift:224
    public mutating func insertBytes<S : CollectionType where S.Generator.Element == Byte>(newBytes: S, at i: Int) {
Data.swift:228
    public mutating func removeByteAtIndex(index: Int) -> Byte {
Data.swift:232
    public mutating func removeFirstByte() -> Byte {
Data.swift:248
    public init(count: Int, repeatedValue: Byte) {
Data.swift:249
        self.init(bytes: [Byte](count: count, repeatedValue: repeatedValue))
Data.swift:260
    public mutating func popLastByte() -> Byte? {
Data.swift:265
public func +=<S : SequenceType where S.Generator.Element == Byte>(inout lhs: Data, rhs: S) {
Data.swift:314
        return Data(bytes: [Byte](utf8))
= UInt8 0029 0030 public protocol ByteType
Data.swift:31
extension UInt8: ByteType {}
Data.swift:32
extension Int8: ByteType {}
Data.swift:56
    public init<T: ByteType>(pointer: UnsafePointer<T>, length: Int) {
{} 0031 extension UInt8: ByteType {} 0032 extension Int8: ByteType {} 0033 0034 public protocol DataConvertible
Data.swift:62
    public init(_ convertible: DataConvertible) {
Data.swift:273
public func +=(inout lhs: Data, rhs: DataConvertible) {
Data.swift:283
public func +(lhs: Data, rhs: DataConvertible) -> Data {
Data.swift:288
public func +(lhs: DataConvertible, rhs: Data) -> Data {
Data.swift:292
extension String: DataConvertible {
{ 0035 var data
Data.swift:63
        self.bytes = convertible.data.bytes
Data.swift:274
    return lhs += rhs.data
Data.swift:284
    return lhs + rhs.data
Data.swift:289
    return lhs.data + rhs
: Data { get } 0036 init(data: Data) throws 0037 } 0038 0039 public struct Data
Data.swift:35
    var data: Data { get }
Data.swift:36
    init(data: Data) throws
Data.swift:51
extension Data {
Data.swift:75
extension Data: MutableCollectionType {
Data.swift:104
            return Data(bytes[bounds])
Data.swift:113
extension Data: Equatable {}
Data.swift:115
public func ==(lhs: Data, rhs: Data) -> Bool {
Data.swift:115
public func ==(lhs: Data, rhs: Data) -> Bool {
Data.swift:119
extension Data: ArrayLiteralConvertible {
Data.swift:125
extension Data: StringLiteralConvertible {
Data.swift:139
extension Data {
Data.swift:156
extension Data: CustomStringConvertible {
Data.swift:166
extension Data: CustomDebugStringConvertible {
Data.swift:172
extension Data: NilLiteralConvertible {
Data.swift:178
extension Data {
Data.swift:200
    public static func bufferWithSize(size: Int) -> Data {
Data.swift:201
        return Data([UInt8](count: size, repeatedValue: 0))
Data.swift:265
public func +=<S : SequenceType where S.Generator.Element == Byte>(inout lhs: Data, rhs: S) {
Data.swift:269
public func +=(inout lhs: Data, rhs: Data) {
Data.swift:269
public func +=(inout lhs: Data, rhs: Data) {
Data.swift:273
public func +=(inout lhs: Data, rhs: DataConvertible) {
Data.swift:278
public func +(lhs: Data, rhs: Data) -> Data {
Data.swift:278
public func +(lhs: Data, rhs: Data) -> Data {
Data.swift:278
public func +(lhs: Data, rhs: Data) -> Data {
Data.swift:279
    return Data(bytes: lhs.bytes + rhs.bytes)
Data.swift:283
public func +(lhs: Data, rhs: DataConvertible) -> Data {
Data.swift:283
public func +(lhs: Data, rhs: DataConvertible) -> Data {
Data.swift:288
public func +(lhs: DataConvertible, rhs: Data) -> Data {
Data.swift:288
public func +(lhs: DataConvertible, rhs: Data) -> Data {
Data.swift:293
    public init(data: Data) throws {
Data.swift:314
        return Data(bytes: [Byte](utf8))
Data.swift:313
    public var data: Data {
{ 0040 private var bytes
Data.swift:43
        self.bytes = bytes
Data.swift:53
        self.bytes = []
Data.swift:59
        self.bytes = bytes
Data.swift:63
        self.bytes = convertible.data.bytes
Data.swift:63
        self.bytes = convertible.data.bytes
Data.swift:67
        self.bytes = [Byte](bytes)
Data.swift:71
        self.bytes = [Byte](bytes)
Data.swift:77
        return bytes.generate()
Data.swift:89
        return bytes.count
Data.swift:94
            return bytes[index]
Data.swift:98
            bytes[index] = newValue
Data.swift:104
            return Data(bytes[bounds])
Data.swift:108
            bytes[bounds] = ArraySlice<Byte>(newValue.bytes)
Data.swift:108
            bytes[bounds] = ArraySlice<Byte>(newValue.bytes)
Data.swift:116
    return lhs.bytes == rhs.bytes
Data.swift:116
    return lhs.bytes == rhs.bytes
Data.swift:180
        return bytes.withUnsafeBufferPointer {
Data.swift:187
        self.bytes = withUnsafePointer(&value) {
Data.swift:193
        return try bytes.withUnsafeBufferPointer(body)
Data.swift:197
        return try bytes.withUnsafeMutableBufferPointer(body)
Data.swift:205
        bytes.replaceRange(subRange, with: newBytes)
Data.swift:209
        bytes.reserveCapacity(minimumCapacity)
Data.swift:213
        bytes.append(newByte)
Data.swift:217
        bytes.appendContentsOf(newBytes)
Data.swift:221
        bytes.insert(newByte, atIndex: i)
Data.swift:225
        bytes.insertContentsOf(newBytes, at: i)
Data.swift:229
        return bytes.removeAtIndex(index)
Data.swift:233
        return bytes.removeFirst()
Data.swift:237
        bytes.removeFirst(n)
Data.swift:241
        bytes.removeRange(subRange)
Data.swift:245
        bytes.removeAll(keepCapacity: keepCapacity)
Data.swift:253
        return bytes.capacity
Data.swift:257
        return bytes.isEmpty
Data.swift:261
        return bytes.popLast()
Data.swift:266
    return lhs.bytes += rhs
Data.swift:270
    return lhs.bytes += rhs.bytes
Data.swift:270
    return lhs.bytes += rhs.bytes
Data.swift:279
    return Data(bytes: lhs.bytes + rhs.bytes)
Data.swift:279
    return Data(bytes: lhs.bytes + rhs.bytes)
: [Byte] 0041 0042 public init
Data.swift:121
        self.init(bytes: bytes)
Data.swift:127
        self.init(bytes: [Byte](string.utf8))
Data.swift:131
        self.init(bytes: [Byte](string.utf8))
Data.swift:135
        self.init(bytes: [Byte](string.utf8))
Data.swift:174
        self.init(bytes: [])
Data.swift:249
        self.init(bytes: [Byte](count: count, repeatedValue: repeatedValue))
Data.swift:279
    return Data(bytes: lhs.bytes + rhs.bytes)
Data.swift:314
        return Data(bytes: [Byte](utf8))
(bytes: [Byte]) { 0043 self.bytes = bytes 0044 } 0045 } 0046 0047 public struct DataError: ErrorType, CustomStringConvertible { 0048 public let description: String 0049 } 0050 0051 extension Data { 0052 public init() { 0053 self.bytes = [] 0054 } 0055 0056 public init<T: ByteType>(pointer: UnsafePointer<T>, length: Int) { 0057 var bytes: [UInt8] = [UInt8](count: length, repeatedValue: 0) 0058 memcpy(&bytes, pointer, length) 0059 self.bytes = bytes 0060 } 0061 0062 public init(_ convertible: DataConvertible) { 0063 self.bytes = convertible.data.bytes 0064 } 0065 0066 public init<S: SequenceType where S.Generator.Element == Byte>(_ bytes: S) { 0067 self.bytes = [Byte](bytes) 0068 } 0069 0070 public init
Data.swift:104
            return Data(bytes[bounds])
Data.swift:201
        return Data([UInt8](count: size, repeatedValue: 0))
<C: CollectionType where C.Generator.Element == Byte>(_ bytes: C) { 0071 self.bytes = [Byte](bytes) 0072 } 0073 } 0074 0075 extension Data: MutableCollectionType { 0076 public func generate
Data.swift:297
        var generator = data.generate()
() -> IndexingGenerator<[Byte]> { 0077 return bytes.generate() 0078 } 0079 0080 public var startIndex: Int { 0081 return 0 0082 } 0083 0084 public var endIndex: Int { 0085 return count 0086 } 0087 0088 public var count
Data.swift:85
        return count
: Int { 0089 return bytes.count 0090 } 0091 0092 public subscript(index: Int) -> Byte { 0093 get { 0094 return bytes[index] 0095 } 0096 0097 set { 0098 bytes[index] = newValue 0099 } 0100 } 0101 0102 public subscript (bounds: Range<Int>) -> Data { 0103 get { 0104 return Data(bytes[bounds]) 0105 } 0106 0107 set { 0108 bytes[bounds] = ArraySlice<Byte>(newValue.bytes) 0109 } 0110 } 0111 } 0112 0113 extension Data: Equatable {} 0114 0115 public func ==(lhs: Data, rhs: Data) -> Bool { 0116 return lhs.bytes == rhs.bytes 0117 } 0118 0119 extension Data: ArrayLiteralConvertible { 0120 public init(arrayLiteral bytes: Byte...) { 0121 self.init(bytes: bytes) 0122 } 0123 } 0124 0125 extension Data: StringLiteralConvertible { 0126 public init(stringLiteral string: String) { 0127 self.init(bytes: [Byte](string.utf8)) 0128 } 0129 0130 public init(extendedGraphemeClusterLiteral string: String){ 0131 self.init(bytes: [Byte](string.utf8)) 0132 } 0133 0134 public init(unicodeScalarLiteral string: String){ 0135 self.init(bytes: [Byte](string.utf8)) 0136 } 0137 } 0138 0139 extension Data { 0140 public func hexString
Data.swift:152
		return hexString(delimiter: 2)
(delimiter delimiter: Int = 0) -> String { 0141 var string = "" 0142 for (index, value) in enumerate() { 0143 if delimiter != 0 && index > 0 && index % delimiter == 0 { 0144 string += " " 0145 } 0146 string += (value < 16 ? "0" : "") + String(value, radix: 16) 0147 } 0148 return string 0149 } 0150 0151 public var hexDescription
Data.swift:168
        return hexDescription
: String { 0152 return hexString(delimiter: 2) 0153 } 0154 } 0155 0156 extension Data: CustomStringConvertible { 0157 public var description: String { 0158 if let string = try? String(data: self) { 0159 return string 0160 } 0161 0162 return debugDescription 0163 } 0164 } 0165 0166 extension Data: CustomDebugStringConvertible { 0167 public var debugDescription
Data.swift:162
        return debugDescription
: String { 0168 return hexDescription 0169 } 0170 } 0171 0172 extension Data: NilLiteralConvertible { 0173 public init(nilLiteral: Void) { 0174 self.init(bytes: []) 0175 } 0176 } 0177 0178 extension Data { 0179 internal func convert<T>() -> T { 0180 return bytes.withUnsafeBufferPointer { 0181 return UnsafePointer<T>($0.baseAddress).memory 0182 } 0183 } 0184 0185 internal init<T>(value: T) { 0186 var value = value 0187 self.bytes = withUnsafePointer(&value) { 0188 Array(UnsafeBufferPointer(start: UnsafePointer<Byte>($0), count: sizeof(T))) 0189 } 0190 } 0191 0192 public func withUnsafeBufferPointer<R>(@noescape body: (UnsafeBufferPointer<Byte>) throws -> R) rethrows -> R { 0193 return try bytes.withUnsafeBufferPointer(body) 0194 } 0195 0196 public mutating func withUnsafeMutableBufferPointer<R>(@noescape body: (inout UnsafeMutableBufferPointer<Byte>) throws -> R) rethrows -> R { 0197 return try bytes.withUnsafeMutableBufferPointer(body) 0198 } 0199 0200 public static func bufferWithSize(size: Int) -> Data { 0201 return Data([UInt8](count: size, repeatedValue: 0)) 0202 } 0203 0204 public mutating func replaceBytesInRange<C: CollectionType where C.Generator.Element == Byte>(subRange: Range<Int>, with newBytes: C) { 0205 bytes.replaceRange(subRange, with: newBytes) 0206 } 0207 0208 public mutating func reserveCapacity(minimumCapacity: Int) { 0209 bytes.reserveCapacity(minimumCapacity) 0210 } 0211 0212 public mutating func appendByte(newByte: Byte) { 0213 bytes.append(newByte) 0214 } 0215 0216 public mutating func appendBytes<S: SequenceType where S.Generator.Element == Byte>(newBytes: S) { 0217 bytes.appendContentsOf(newBytes) 0218 } 0219 0220 public mutating func insertByte(newByte: Byte, atIndex i: Int) { 0221 bytes.insert(newByte, atIndex: i) 0222 } 0223 0224 public mutating func insertBytes<S : CollectionType where S.Generator.Element == Byte>(newBytes: S, at i: Int) { 0225 bytes.insertContentsOf(newBytes, at: i) 0226 } 0227 0228 public mutating func removeByteAtIndex(index: Int) -> Byte { 0229 return bytes.removeAtIndex(index) 0230 } 0231 0232 public mutating func removeFirstByte() -> Byte { 0233 return bytes.removeFirst() 0234 } 0235 0236 public mutating func removeFirstBytes(n: Int) { 0237 bytes.removeFirst(n) 0238 } 0239 0240 public mutating func removeBytesInRange(subRange: Range<Int>) { 0241 bytes.removeRange(subRange) 0242 } 0243 0244 public mutating func removeAllBytes(keepCapacity keepCapacity: Bool = true) { 0245 bytes.removeAll(keepCapacity: keepCapacity) 0246 } 0247 0248 public init(count: Int, repeatedValue: Byte) { 0249 self.init(bytes: [Byte](count: count, repeatedValue: repeatedValue)) 0250 } 0251 0252 public var capacity: Int { 0253 return bytes.capacity 0254 } 0255 0256 public var isEmpty: Bool { 0257 return bytes.isEmpty 0258 } 0259 0260 public mutating func popLastByte() -> Byte? { 0261 return bytes.popLast() 0262 } 0263 } 0264 0265 public func +=<S : SequenceType where S.Generator.Element == Byte>(inout lhs: Data, rhs: S) { 0266 return lhs.bytes += rhs 0267 } 0268 0269 public func +=(inout lhs: Data, rhs: Data) { 0270 return lhs.bytes += rhs.bytes 0271 } 0272 0273 public func +=(inout lhs: Data, rhs: DataConvertible) { 0274 return lhs += rhs.data 0275 } 0276 0277 @warn_unused_result 0278 public func +(lhs: Data, rhs: Data) -> Data { 0279 return Data(bytes: lhs.bytes + rhs.bytes) 0280 } 0281 0282 @warn_unused_result 0283 public func +(lhs: Data, rhs: DataConvertible) -> Data { 0284 return lhs + rhs.data 0285 } 0286 0287 @warn_unused_result 0288 public func +(lhs: DataConvertible, rhs: Data) -> Data { 0289 return lhs.data + rhs 0290 } 0291 0292 extension String: DataConvertible { 0293 public init
Data.swift:158
        if let string = try? String(data: self) {
(data: Data) throws { 0294 struct Error: ErrorType {} 0295 var string = "" 0296 var decoder = UTF8() 0297 var generator = data.generate() 0298 var finished = false 0299 0300 while !finished { 0301 let decodingResult = decoder.decode(&generator) 0302 switch decodingResult { 0303 case .Result(let char): string.append(char) 0304 case .EmptyInput: finished = true 0305 case .Error: 0306 throw Error() 0307 } 0308 } 0309 0310 self.init(string) 0311 } 0312 0313 public var data: Data { 0314 return Data(bytes: [Byte](utf8)) 0315 } 0316 } 0317