|
24 | 24 |
|
25 | 25 | // MARK: - Properties |
26 | 26 |
|
27 | | - public var bytes: [Byte] |
| 27 | + private var _bytes: ContiguousArray<Byte> |
| 28 | + |
| 29 | + public var bytes: [Byte] { |
| 30 | + |
| 31 | + @inline(__always) |
| 32 | + get { return Array(_bytes) } |
| 33 | + |
| 34 | + @inline(__always) |
| 35 | + set { _bytes = ContiguousArray(newValue) } |
| 36 | + } |
28 | 37 |
|
29 | 38 | // MARK: - Initialization |
30 | 39 |
|
|
34 | 43 | @inline(__always) |
35 | 44 | public init(bytes: [Byte] = []) { |
36 | 45 |
|
37 | | - self.bytes = bytes |
| 46 | + _bytes = ContiguousArray(bytes) |
38 | 47 | } |
39 | 48 |
|
40 | 49 | /// Initialize a `Data` with the contents of an Array. |
|
43 | 52 | @inline(__always) |
44 | 53 | public init(bytes: ArraySlice<UInt8>) { |
45 | 54 |
|
46 | | - self.bytes = Array(bytes) |
| 55 | + _bytes = ContiguousArray(bytes) |
47 | 56 | } |
48 | 57 |
|
49 | 58 | /// Initialize a `Data` with the specified size. |
|
53 | 62 | public init?(count: Int) { |
54 | 63 |
|
55 | 64 | // Never fails on Linux |
56 | | - self.bytes = Array(repeating: 0, count: count) |
| 65 | + _bytes = ContiguousArray.init(repeating: 0, count: count) |
57 | 66 | } |
58 | 67 |
|
59 | 68 | /// Initialize a `Data` with copied memory content. |
|
63 | 72 | @inline(__always) |
64 | 73 | public init(bytes pointer: UnsafePointer<Void>, count: Int) { |
65 | 74 |
|
66 | | - self.bytes = [UInt8](repeating: 0, count: count) |
| 75 | + _bytes = ContiguousArray<UInt8>(repeating: 0, count: count) |
67 | 76 |
|
68 | 77 | memcpy(&bytes, pointer, count) |
69 | 78 | } |
|
104 | 113 | @inline(__always) |
105 | 114 | public mutating func append(_ other: Data) { |
106 | 115 |
|
107 | | - self.bytes += other.bytes |
| 116 | + _bytes += other._bytes |
108 | 117 | } |
109 | 118 |
|
110 | 119 | /// Return a new copy of the data in a specified range. |
|
113 | 122 | @inline(__always) |
114 | 123 | public func subdata(in range: Range<Index>) -> Data { |
115 | 124 |
|
116 | | - return Data(bytes: bytes[range]) |
| 125 | + return Data(bytes: _bytes[range]) |
117 | 126 | } |
118 | 127 |
|
119 | 128 | // MARK: - Index and Subscript |
|
123 | 132 | public subscript(index: Index) -> Byte { |
124 | 133 |
|
125 | 134 | @inline(__always) |
126 | | - get { return bytes[index] } |
| 135 | + get { return _bytes[index] } |
127 | 136 |
|
128 | 137 | @inline(__always) |
129 | | - set { bytes[index] = newValue } |
| 138 | + set { _bytes[index] = newValue } |
130 | 139 | } |
131 | 140 |
|
132 | 141 | public subscript(bounds: Range<Int>) -> MutableRandomAccessSlice<Data> { |
|
135 | 144 | get { return MutableRandomAccessSlice(base: self, bounds: bounds) } |
136 | 145 |
|
137 | 146 | @inline(__always) |
138 | | - set { bytes.replaceSubrange(bounds, with: newValue) } |
| 147 | + set { _bytes.replaceSubrange(bounds, with: newValue) } |
139 | 148 | } |
140 | 149 |
|
141 | 150 | /// The start `Index` in the data. |
|
172 | 181 |
|
173 | 182 | public func == (lhs: Data, rhs: Data) -> Bool { |
174 | 183 |
|
175 | | - guard lhs.bytes.count == rhs.bytes.count else { return false } |
| 184 | + guard lhs.count == rhs.count else { return false } |
176 | 185 |
|
177 | 186 | var bytes1 = lhs.bytes |
178 | 187 |
|
|
185 | 194 |
|
186 | 195 | public func + (lhs: Data, rhs: Data) -> Data { |
187 | 196 |
|
188 | | - return Data(bytes: lhs.bytes + rhs.bytes) |
| 197 | + var result = Data() |
| 198 | + |
| 199 | + result._bytes = lhs._bytes + rhs._bytes |
| 200 | + |
| 201 | + return result |
189 | 202 | } |
190 | 203 |
|
191 | 204 | #endif |
|
0 commit comments