Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 9e0f0b0

Browse files
committed
Fixed #33
1 parent 6b65d2f commit 9e0f0b0

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

Sources/SwiftFoundation/Data.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@
2424

2525
// MARK: - Properties
2626

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+
}
2837

2938
// MARK: - Initialization
3039

@@ -34,7 +43,7 @@
3443
@inline(__always)
3544
public init(bytes: [Byte] = []) {
3645

37-
self.bytes = bytes
46+
_bytes = ContiguousArray(bytes)
3847
}
3948

4049
/// Initialize a `Data` with the contents of an Array.
@@ -43,7 +52,7 @@
4352
@inline(__always)
4453
public init(bytes: ArraySlice<UInt8>) {
4554

46-
self.bytes = Array(bytes)
55+
_bytes = ContiguousArray(bytes)
4756
}
4857

4958
/// Initialize a `Data` with the specified size.
@@ -53,7 +62,7 @@
5362
public init?(count: Int) {
5463

5564
// Never fails on Linux
56-
self.bytes = Array(repeating: 0, count: count)
65+
_bytes = ContiguousArray.init(repeating: 0, count: count)
5766
}
5867

5968
/// Initialize a `Data` with copied memory content.
@@ -63,7 +72,7 @@
6372
@inline(__always)
6473
public init(bytes pointer: UnsafePointer<Void>, count: Int) {
6574

66-
self.bytes = [UInt8](repeating: 0, count: count)
75+
_bytes = ContiguousArray<UInt8>(repeating: 0, count: count)
6776

6877
memcpy(&bytes, pointer, count)
6978
}
@@ -104,7 +113,7 @@
104113
@inline(__always)
105114
public mutating func append(_ other: Data) {
106115

107-
self.bytes += other.bytes
116+
_bytes += other._bytes
108117
}
109118

110119
/// Return a new copy of the data in a specified range.
@@ -113,7 +122,7 @@
113122
@inline(__always)
114123
public func subdata(in range: Range<Index>) -> Data {
115124

116-
return Data(bytes: bytes[range])
125+
return Data(bytes: _bytes[range])
117126
}
118127

119128
// MARK: - Index and Subscript
@@ -123,10 +132,10 @@
123132
public subscript(index: Index) -> Byte {
124133

125134
@inline(__always)
126-
get { return bytes[index] }
135+
get { return _bytes[index] }
127136

128137
@inline(__always)
129-
set { bytes[index] = newValue }
138+
set { _bytes[index] = newValue }
130139
}
131140

132141
public subscript(bounds: Range<Int>) -> MutableRandomAccessSlice<Data> {
@@ -135,7 +144,7 @@
135144
get { return MutableRandomAccessSlice(base: self, bounds: bounds) }
136145

137146
@inline(__always)
138-
set { bytes.replaceSubrange(bounds, with: newValue) }
147+
set { _bytes.replaceSubrange(bounds, with: newValue) }
139148
}
140149

141150
/// The start `Index` in the data.
@@ -172,7 +181,7 @@
172181

173182
public func == (lhs: Data, rhs: Data) -> Bool {
174183

175-
guard lhs.bytes.count == rhs.bytes.count else { return false }
184+
guard lhs.count == rhs.count else { return false }
176185

177186
var bytes1 = lhs.bytes
178187

@@ -185,7 +194,11 @@
185194

186195
public func + (lhs: Data, rhs: Data) -> Data {
187196

188-
return Data(bytes: lhs.bytes + rhs.bytes)
197+
var result = Data()
198+
199+
result._bytes = lhs._bytes + rhs._bytes
200+
201+
return result
189202
}
190203

191204
#endif

0 commit comments

Comments
 (0)