Skip to content

Commit 84c0ca8

Browse files
committed
Add getFloat()
1 parent 7814c0d commit 84c0ca8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

denops/ddx/buffer.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,52 @@ export class DdxBuffer {
11531153

11541154
return Number((BigInt(high) << 32n) + BigInt(low));
11551155
}
1156+
1157+
/**
1158+
* General-purpose floating point number getter.
1159+
* Supports both 32-bit and 64-bit floating point values.
1160+
*/
1161+
getFloat(pos: number, size: number, isLittle: boolean): number {
1162+
// Based on provided size, call appropriate method
1163+
switch (size) {
1164+
case 4:
1165+
return this.getFloat32(pos, isLittle); // 32-bit float
1166+
case 8:
1167+
return this.getFloat64(pos, isLittle); // 64-bit float
1168+
default:
1169+
throw new Error(`Unsupported floating-point size: ${size}`);
1170+
}
1171+
}
1172+
1173+
/**
1174+
* 32-bit floating-point (float) little-endian/big-endian.
1175+
* Returns NaN on OOB.
1176+
*/
1177+
getFloat32(pos: number, isLittle: boolean): number {
1178+
pos -= this.#offset;
1179+
if (pos < 0 || pos + 4 > this.#bytes.length) return NaN;
1180+
1181+
const buffer = this.#bytes.buffer;
1182+
const byteOffset = this.#bytes.byteOffset + pos;
1183+
const view = new DataView(buffer, byteOffset, 4);
1184+
1185+
return view.getFloat32(0, isLittle);
1186+
}
1187+
1188+
/**
1189+
* 64-bit floating-point (double) little-endian/big-endian.
1190+
* Returns NaN on OOB.
1191+
*/
1192+
getFloat64(pos: number, isLittle: boolean): number {
1193+
pos -= this.#offset;
1194+
if (pos < 0 || pos + 8 > this.#bytes.length) return NaN;
1195+
1196+
const buffer = this.#bytes.buffer;
1197+
const byteOffset = this.#bytes.byteOffset + pos;
1198+
const view = new DataView(buffer, byteOffset, 8);
1199+
1200+
return view.getFloat64(0, isLittle);
1201+
}
11561202
}
11571203

11581204
const exists = async (path: string) => {

0 commit comments

Comments
 (0)