mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
BREAKING(io): remove readLong()
(#6047)
* BREAKING(io/unstable): remove `sliceLongToBytes()` * BREAKINIG(io/unstable): remove `readLong()`
This commit is contained in:
parent
90c571cfd5
commit
498700fc52
@ -13,7 +13,6 @@
|
||||
"./read-all": "./read_all.ts",
|
||||
"./read-delim": "./read_delim.ts",
|
||||
"./read-int": "./read_int.ts",
|
||||
"./read-long": "./read_long.ts",
|
||||
"./read-short": "./read_short.ts",
|
||||
"./reader-from-stream-reader": "./reader_from_stream_reader.ts",
|
||||
"./string-reader": "./string_reader.ts",
|
||||
|
@ -26,7 +26,6 @@ export * from "./multi_reader.ts";
|
||||
export * from "./read_all.ts";
|
||||
export * from "./read_delim.ts";
|
||||
export * from "./read_int.ts";
|
||||
export * from "./read_long.ts";
|
||||
export * from "./read_short.ts";
|
||||
export * from "./reader_from_stream_reader.ts";
|
||||
export * from "./string_reader.ts";
|
||||
|
@ -1,43 +0,0 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import type { BufReader } from "./buf_reader.ts";
|
||||
import { readInt } from "./read_int.ts";
|
||||
|
||||
const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
|
||||
|
||||
/**
|
||||
* Read big endian 64bit long from a {@linkcode BufReader}.
|
||||
*
|
||||
* @example Usage
|
||||
* ```ts
|
||||
* import { Buffer } from "@std/io/buffer"
|
||||
* import { BufReader } from "@std/io/buf-reader";
|
||||
* import { readLong } from "@std/io/read-long";
|
||||
* import { assertEquals } from "@std/assert/equals";
|
||||
*
|
||||
* const buf = new BufReader(new Buffer(new Uint8Array([0, 0, 0, 0x12, 0x34, 0x56, 0x78, 0x9a])));
|
||||
* const long = await readLong(buf);
|
||||
* assertEquals(long, 0x123456789a);
|
||||
* ```
|
||||
*
|
||||
* @param buf The BufReader to read from
|
||||
* @returns The 64bit long
|
||||
* @throws {Deno.errors.UnexpectedEof} If the reader returns unexpected EOF
|
||||
* @throws {RangeError} If the long value is too big to be represented as a JavaScript number
|
||||
*
|
||||
* @deprecated This will be removed in 0.225.0.
|
||||
*/
|
||||
export async function readLong(buf: BufReader): Promise<number | null> {
|
||||
const high = await readInt(buf);
|
||||
if (high === null) return null;
|
||||
const low = await readInt(buf);
|
||||
if (low === null) throw new Deno.errors.UnexpectedEof();
|
||||
const big = (BigInt(high) << 32n) | BigInt(low);
|
||||
// We probably should provide a similar API that returns BigInt values.
|
||||
if (big > MAX_SAFE_INTEGER) {
|
||||
throw new RangeError(
|
||||
"Long value too big to be represented as a JavaScript number.",
|
||||
);
|
||||
}
|
||||
return Number(big);
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { readLong } from "./read_long.ts";
|
||||
import { BufReader } from "./buf_reader.ts";
|
||||
import { BinaryReader } from "./_test_common.ts";
|
||||
|
||||
Deno.test("testReadLong", async function () {
|
||||
const r = new BinaryReader(
|
||||
new Uint8Array([0x00, 0x00, 0x00, 0x78, 0x12, 0x34, 0x56, 0x78]),
|
||||
);
|
||||
const long = await readLong(new BufReader(r));
|
||||
assertEquals(long, 0x7812345678);
|
||||
});
|
||||
|
||||
Deno.test("testReadLong2", async function () {
|
||||
const r = new BinaryReader(
|
||||
new Uint8Array([0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]),
|
||||
);
|
||||
const long = await readLong(new BufReader(r));
|
||||
assertEquals(long, 0x12345678);
|
||||
});
|
Loading…
Reference in New Issue
Block a user