mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-20 13:35:06 +00:00
bulk code format fix (#8707)
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
import { BitWidth } from './bit-width.js'
|
||||
import { toByteWidth, fromByteWidth } from './bit-width-util.js'
|
||||
import { toUTF8Array, fromUTF8Array } from './flexbuffers-util.js'
|
||||
import {fromByteWidth, toByteWidth} from './bit-width-util.js';
|
||||
import {BitWidth} from './bit-width.js';
|
||||
import {fromUTF8Array, toUTF8Array} from './flexbuffers-util.js';
|
||||
|
||||
export function validateOffset(dataView: DataView, offset: number, width: number): void {
|
||||
if (dataView.byteLength <= offset + width || (offset & (toByteWidth(width) - 1)) !== 0) {
|
||||
throw "Bad offset: " + offset + ", width: " + width;
|
||||
export function validateOffset(
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
width: number,
|
||||
): void {
|
||||
if (
|
||||
dataView.byteLength <= offset + width ||
|
||||
(offset & (toByteWidth(width) - 1)) !== 0
|
||||
) {
|
||||
throw 'Bad offset: ' + offset + ', width: ' + width;
|
||||
}
|
||||
}
|
||||
|
||||
export function readInt(dataView: DataView, offset: number, width: number): number | bigint {
|
||||
export function readInt(
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
width: number,
|
||||
): number | bigint {
|
||||
if (width < 2) {
|
||||
if (width < 1) {
|
||||
return dataView.getInt8(offset);
|
||||
@@ -17,17 +28,24 @@ export function readInt(dataView: DataView, offset: number, width: number): numb
|
||||
}
|
||||
} else {
|
||||
if (width < 3) {
|
||||
return dataView.getInt32(offset, true)
|
||||
return dataView.getInt32(offset, true);
|
||||
} else {
|
||||
if (dataView.setBigInt64 === undefined) {
|
||||
return BigInt(dataView.getUint32(offset, true)) + (BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32));
|
||||
return (
|
||||
BigInt(dataView.getUint32(offset, true)) +
|
||||
(BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32))
|
||||
);
|
||||
}
|
||||
return dataView.getBigInt64(offset, true)
|
||||
return dataView.getBigInt64(offset, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function readUInt(dataView: DataView, offset: number, width: number): number | bigint {
|
||||
export function readUInt(
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
width: number,
|
||||
): number | bigint {
|
||||
if (width < 2) {
|
||||
if (width < 1) {
|
||||
return dataView.getUint8(offset);
|
||||
@@ -36,19 +54,26 @@ export function readUInt(dataView: DataView, offset: number, width: number): num
|
||||
}
|
||||
} else {
|
||||
if (width < 3) {
|
||||
return dataView.getUint32(offset, true)
|
||||
return dataView.getUint32(offset, true);
|
||||
} else {
|
||||
if (dataView.getBigUint64 === undefined) {
|
||||
return BigInt(dataView.getUint32(offset, true)) + (BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32));
|
||||
return (
|
||||
BigInt(dataView.getUint32(offset, true)) +
|
||||
(BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32))
|
||||
);
|
||||
}
|
||||
return dataView.getBigUint64(offset, true)
|
||||
return dataView.getBigUint64(offset, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function readFloat(dataView: DataView, offset: number, width: number): number {
|
||||
export function readFloat(
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
width: number,
|
||||
): number {
|
||||
if (width < BitWidth.WIDTH32) {
|
||||
throw "Bad width: " + width;
|
||||
throw 'Bad width: ' + width;
|
||||
}
|
||||
if (width === BitWidth.WIDTH32) {
|
||||
return dataView.getFloat32(offset, true);
|
||||
@@ -56,17 +81,32 @@ export function readFloat(dataView: DataView, offset: number, width: number): nu
|
||||
return dataView.getFloat64(offset, true);
|
||||
}
|
||||
|
||||
export function indirect(dataView: DataView, offset: number, width: number): number {
|
||||
export function indirect(
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
width: number,
|
||||
): number {
|
||||
const step = readUInt(dataView, offset, width) as number;
|
||||
return offset - step;
|
||||
}
|
||||
|
||||
export function keyIndex(key: string, dataView: DataView, offset: number, parentWidth: number, byteWidth: number, length: number): number | null {
|
||||
export function keyIndex(
|
||||
key: string,
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
parentWidth: number,
|
||||
byteWidth: number,
|
||||
length: number,
|
||||
): number | null {
|
||||
const input = toUTF8Array(key);
|
||||
const keysVectorOffset = indirect(dataView, offset, parentWidth) - byteWidth * 3;
|
||||
const keysVectorOffset =
|
||||
indirect(dataView, offset, parentWidth) - byteWidth * 3;
|
||||
const bitWidth = fromByteWidth(byteWidth);
|
||||
const indirectOffset = keysVectorOffset - Number(readUInt(dataView, keysVectorOffset, bitWidth));
|
||||
const _byteWidth = Number(readUInt(dataView, keysVectorOffset + byteWidth, bitWidth));
|
||||
const indirectOffset =
|
||||
keysVectorOffset - Number(readUInt(dataView, keysVectorOffset, bitWidth));
|
||||
const _byteWidth = Number(
|
||||
readUInt(dataView, keysVectorOffset + byteWidth, bitWidth),
|
||||
);
|
||||
let low = 0;
|
||||
let high = length - 1;
|
||||
while (low <= high) {
|
||||
@@ -82,9 +122,16 @@ export function keyIndex(key: string, dataView: DataView, offset: number, parent
|
||||
return null;
|
||||
}
|
||||
|
||||
export function diffKeys(input: Uint8Array, index: number, dataView: DataView, offset: number, width: number): number {
|
||||
export function diffKeys(
|
||||
input: Uint8Array,
|
||||
index: number,
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
width: number,
|
||||
): number {
|
||||
const keyOffset = offset + index * width;
|
||||
const keyIndirectOffset = keyOffset - Number(readUInt(dataView, keyOffset, fromByteWidth(width)));
|
||||
const keyIndirectOffset =
|
||||
keyOffset - Number(readUInt(dataView, keyOffset, fromByteWidth(width)));
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const dif = input[i] - dataView.getUint8(keyIndirectOffset + i);
|
||||
if (dif !== 0) {
|
||||
@@ -94,16 +141,30 @@ export function diffKeys(input: Uint8Array, index: number, dataView: DataView, o
|
||||
return dataView.getUint8(keyIndirectOffset + input.length) === 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
export function keyForIndex(index: number, dataView: DataView, offset: number, parentWidth: number, byteWidth: number): string {
|
||||
const keysVectorOffset = indirect(dataView, offset, parentWidth) - byteWidth * 3;
|
||||
export function keyForIndex(
|
||||
index: number,
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
parentWidth: number,
|
||||
byteWidth: number,
|
||||
): string {
|
||||
const keysVectorOffset =
|
||||
indirect(dataView, offset, parentWidth) - byteWidth * 3;
|
||||
const bitWidth = fromByteWidth(byteWidth);
|
||||
const indirectOffset = keysVectorOffset - Number(readUInt(dataView, keysVectorOffset, bitWidth));
|
||||
const _byteWidth = Number(readUInt(dataView, keysVectorOffset + byteWidth, bitWidth));
|
||||
const indirectOffset =
|
||||
keysVectorOffset - Number(readUInt(dataView, keysVectorOffset, bitWidth));
|
||||
const _byteWidth = Number(
|
||||
readUInt(dataView, keysVectorOffset + byteWidth, bitWidth),
|
||||
);
|
||||
const keyOffset = indirectOffset + index * _byteWidth;
|
||||
const keyIndirectOffset = keyOffset - Number(readUInt(dataView, keyOffset, fromByteWidth(_byteWidth)));
|
||||
const keyIndirectOffset =
|
||||
keyOffset -
|
||||
Number(readUInt(dataView, keyOffset, fromByteWidth(_byteWidth)));
|
||||
let length = 0;
|
||||
while (dataView.getUint8(keyIndirectOffset + length) !== 0) {
|
||||
length++;
|
||||
}
|
||||
return fromUTF8Array(new Uint8Array(dataView.buffer, keyIndirectOffset, length));
|
||||
return fromUTF8Array(
|
||||
new Uint8Array(dataView.buffer, keyIndirectOffset, length),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user