|
9 | 9 | */ |
10 | 10 |
|
11 | 11 | import Foundation |
| 12 | +import TSCBasic |
12 | 13 |
|
13 | 14 | extension Bitcode { |
14 | 15 | /// Parse a bitstream from data. |
| 16 | + @available(*, deprecated, message: "Use Bitcode.init(bytes:) instead") |
15 | 17 | public init(data: Data) throws { |
16 | 18 | precondition(data.count > 4) |
17 | | - let signatureValue = UInt32(Bits(buffer: data).readBits(atOffset: 0, count: 32)) |
18 | | - let bitstreamData = data[4..<data.count] |
19 | | - |
20 | | - var reader = BitstreamReader(buffer: bitstreamData) |
| 19 | + try self.init(bytes: ByteString(data)) |
| 20 | + } |
| 21 | + |
| 22 | + public init(bytes: ByteString) throws { |
| 23 | + precondition(bytes.count > 4) |
| 24 | + var reader = BitstreamReader(buffer: bytes) |
| 25 | + let signature = try reader.readSignature() |
21 | 26 | var visitor = CollectingVisitor() |
22 | 27 | try reader.readBlock(id: BitstreamReader.fakeTopLevelBlockID, |
23 | 28 | abbrevWidth: 2, |
24 | 29 | abbrevInfo: [], |
25 | 30 | visitor: &visitor) |
26 | | - self.init(signature: .init(value: signatureValue), |
| 31 | + self.init(signature: signature, |
27 | 32 | elements: visitor.finalizeTopLevelElements(), |
28 | 33 | blockInfo: reader.blockInfo) |
29 | 34 | } |
30 | 35 |
|
31 | 36 | /// Traverse a bitstream using the specified `visitor`, which will receive |
32 | 37 | /// callbacks when blocks and records are encountered. |
| 38 | + @available(*, deprecated, message: "Use Bitcode.read(bytes:using:) instead") |
33 | 39 | public static func read<Visitor: BitstreamVisitor>(stream data: Data, using visitor: inout Visitor) throws { |
34 | 40 | precondition(data.count > 4) |
35 | | - let signatureValue = UInt32(Bits(buffer: data).readBits(atOffset: 0, count: 32)) |
36 | | - try visitor.validate(signature: .init(value: signatureValue)) |
| 41 | + try Self.read(bytes: ByteString(data), using: &visitor) |
| 42 | + } |
37 | 43 |
|
38 | | - let bitstreamData = data[4..<data.count] |
39 | | - var reader = BitstreamReader(buffer: bitstreamData) |
| 44 | + public static func read<Visitor: BitstreamVisitor>(bytes: ByteString, using visitor: inout Visitor) throws { |
| 45 | + precondition(bytes.count > 4) |
| 46 | + var reader = BitstreamReader(buffer: bytes) |
| 47 | + try visitor.validate(signature: reader.readSignature()) |
40 | 48 | try reader.readBlock(id: BitstreamReader.fakeTopLevelBlockID, |
41 | 49 | abbrevWidth: 2, |
42 | 50 | abbrevInfo: [], |
@@ -113,8 +121,14 @@ private struct BitstreamReader { |
113 | 121 | var blockInfo: [UInt64: BlockInfo] = [:] |
114 | 122 | var globalAbbrevs: [UInt64: [Bitstream.Abbreviation]] = [:] |
115 | 123 |
|
116 | | - init(buffer: Data) { |
117 | | - cursor = Bits.Cursor(buffer: buffer) |
| 124 | + init(buffer: ByteString) { |
| 125 | + self.cursor = Bits.Cursor(buffer: buffer) |
| 126 | + } |
| 127 | + |
| 128 | + mutating func readSignature() throws -> Bitcode.Signature { |
| 129 | + precondition(self.cursor.isAtStart) |
| 130 | + let bits = try UInt32(self.cursor.read(MemoryLayout<UInt32>.size * 8)) |
| 131 | + return Bitcode.Signature(value: bits) |
118 | 132 | } |
119 | 133 |
|
120 | 134 | mutating func readAbbrevOp() throws -> Bitstream.Abbreviation.Operand { |
@@ -220,7 +234,7 @@ private struct BitstreamReader { |
220 | 234 | case .blob: |
221 | 235 | let length = Int(try cursor.readVBR(6)) |
222 | 236 | try cursor.advance(toBitAlignment: 32) |
223 | | - payload = .blob(try cursor.read(bytes: length)) |
| 237 | + payload = .blob(try Data(cursor.read(bytes: length))) |
224 | 238 | try cursor.advance(toBitAlignment: 32) |
225 | 239 | default: |
226 | 240 | fatalError() |
|
0 commit comments