James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 1 | import { BitWidth } from './bit-width.js' |
| 2 | import { toByteWidth, fromByteWidth } from './bit-width-util.js' |
| 3 | import { toUTF8Array, fromUTF8Array } from './flexbuffers-util.js' |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 4 | |
| 5 | export function validateOffset(dataView: DataView, offset: number, width: number): void { |
| 6 | if (dataView.byteLength <= offset + width || (offset & (toByteWidth(width) - 1)) !== 0) { |
| 7 | throw "Bad offset: " + offset + ", width: " + width; |
| 8 | } |
| 9 | } |
| 10 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 11 | export function readInt(dataView: DataView, offset: number, width: number): number | bigint { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 12 | if (width < 2) { |
| 13 | if (width < 1) { |
| 14 | return dataView.getInt8(offset); |
| 15 | } else { |
| 16 | return dataView.getInt16(offset, true); |
| 17 | } |
| 18 | } else { |
| 19 | if (width < 3) { |
| 20 | return dataView.getInt32(offset, true) |
| 21 | } else { |
| 22 | if (dataView.setBigInt64 === undefined) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 23 | return BigInt(dataView.getUint32(offset, true)) + (BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32)); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 24 | } |
| 25 | return dataView.getBigInt64(offset, true) |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 30 | export function readUInt(dataView: DataView, offset: number, width: number): number | bigint { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 31 | if (width < 2) { |
| 32 | if (width < 1) { |
| 33 | return dataView.getUint8(offset); |
| 34 | } else { |
| 35 | return dataView.getUint16(offset, true); |
| 36 | } |
| 37 | } else { |
| 38 | if (width < 3) { |
| 39 | return dataView.getUint32(offset, true) |
| 40 | } else { |
| 41 | if (dataView.getBigUint64 === undefined) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 42 | return BigInt(dataView.getUint32(offset, true)) + (BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32)); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 43 | } |
| 44 | return dataView.getBigUint64(offset, true) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | export function readFloat(dataView: DataView, offset: number, width: number): number { |
| 50 | if (width < BitWidth.WIDTH32) { |
| 51 | throw "Bad width: " + width; |
| 52 | } |
| 53 | if (width === BitWidth.WIDTH32) { |
| 54 | return dataView.getFloat32(offset, true); |
| 55 | } |
| 56 | return dataView.getFloat64(offset, true); |
| 57 | } |
| 58 | |
| 59 | export function indirect(dataView: DataView, offset: number, width: number): number { |
| 60 | const step = readUInt(dataView, offset, width) as number; |
| 61 | return offset - step; |
| 62 | } |
| 63 | |
| 64 | export function keyIndex(key: string, dataView: DataView, offset: number, parentWidth: number, byteWidth: number, length: number): number | null { |
| 65 | const input = toUTF8Array(key); |
| 66 | const keysVectorOffset = indirect(dataView, offset, parentWidth) - byteWidth * 3; |
| 67 | const bitWidth = fromByteWidth(byteWidth); |
| 68 | const indirectOffset = keysVectorOffset - (readUInt(dataView, keysVectorOffset, bitWidth) as number); |
| 69 | const _byteWidth = readUInt(dataView, keysVectorOffset + byteWidth, bitWidth) as number; |
| 70 | let low = 0; |
| 71 | let high = length - 1; |
| 72 | while (low <= high) { |
| 73 | const mid = (high + low) >> 1; |
| 74 | const dif = diffKeys(input, mid, dataView, indirectOffset, _byteWidth); |
| 75 | if (dif === 0) return mid; |
| 76 | if (dif < 0) { |
| 77 | high = mid - 1; |
| 78 | } else { |
| 79 | low = mid + 1; |
| 80 | } |
| 81 | } |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | export function diffKeys(input: Uint8Array, index: number, dataView: DataView, offset: number, width: number): number { |
| 86 | const keyOffset = offset + index * width; |
| 87 | const keyIndirectOffset = keyOffset - (readUInt(dataView, keyOffset, fromByteWidth(width)) as number); |
| 88 | for (let i = 0; i < input.length; i++) { |
| 89 | const dif = input[i] - dataView.getUint8(keyIndirectOffset + i); |
| 90 | if (dif !== 0) { |
| 91 | return dif; |
| 92 | } |
| 93 | } |
| 94 | return dataView.getUint8(keyIndirectOffset + input.length) === 0 ? 0 : -1; |
| 95 | } |
| 96 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 97 | export function keyForIndex(index: number, dataView: DataView, offset: number, parentWidth: number, byteWidth: number): string { |
| 98 | const keysVectorOffset = indirect(dataView, offset, parentWidth) - byteWidth * 3; |
| 99 | const bitWidth = fromByteWidth(byteWidth); |
| 100 | const indirectOffset = keysVectorOffset - (readUInt(dataView, keysVectorOffset, bitWidth) as number); |
| 101 | const _byteWidth = readUInt(dataView, keysVectorOffset + byteWidth, bitWidth) as number; |
| 102 | const keyOffset = indirectOffset + index * _byteWidth; |
| 103 | const keyIndirectOffset = keyOffset - (readUInt(dataView, keyOffset, fromByteWidth(_byteWidth)) as number); |
| 104 | let length = 0; |
| 105 | while (dataView.getUint8(keyIndirectOffset + length) !== 0) { |
| 106 | length++; |
| 107 | } |
| 108 | return fromUTF8Array(new Uint8Array(dataView.buffer, keyIndirectOffset, length)); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 109 | } |