MCPcopy Create free account
hub / github.com/denoland/std / encodeBigInt

Function encodeBigInt

cbor/_common_encode.ts:155–178  ·  view source on GitHub ↗
(
  input: bigint,
  output: Uint8Array,
  offset: number,
)

Source from the content-addressed store, hash-verified

153}
154
155function encodeBigInt(
156 input: bigint,
157 output: Uint8Array,
158 offset: number,
159): number {
160 const isNegative = input < 0n;
161 if (isNegative) input = -input - 1n;
162 if (input >= 2n ** 64n) {
163 output[offset++] = isNegative ? 0b110_00011 : 0b110_00010;
164 const bytes = calcBytes(input);
165 offset = encodeHeader(0b010_00000, bytes, output, offset);
166 for (let i = bytes - 1; i >= 0; --i) {
167 output[offset + i] = Number(input & 0xFFn);
168 input >>= 8n;
169 }
170 return offset + bytes;
171 }
172 return encodeHeader(
173 isNegative ? 0b001_00000 : 0b000_00000,
174 input,
175 output,
176 offset,
177 );
178}
179
180function encodeUint8Array(
181 input: Uint8Array,

Callers 1

encodeFunction · 0.85

Calls 2

calcBytesFunction · 0.85
encodeHeaderFunction · 0.85

Tested by

no test coverage detected