Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/variableLengthQuantity.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { VSBuffer } from '../vs/base/common/buffer';
7
8
/** Reads a 32-bit integer from the buffer */
9
export function readVariableLengthQuantity(buffer: VSBuffer, offset: number): { value: number; consumed: number } {
10
let result = 0;
11
let consumed = 0;
12
let byte: number;
13
14
do {
15
byte = buffer.readUInt8(offset + consumed);
16
result |= (byte & 0x7f) << (consumed * 7);
17
consumed++;
18
} while (byte & 0x80);
19
20
return { value: result, consumed };
21
}
22
23
/** Writes a 32 bit integer to the buffer */
24
export function writeVariableLengthQuantity(i: number): VSBuffer {
25
if (i !== (i | 0)) {
26
throw new Error(`${i} is not an int32.`);
27
}
28
29
const result: number[] = [];
30
do {
31
let byte = i & 0x7f;
32
i >>>= 7;
33
if (i !== 0) {
34
byte |= 0x80;
35
}
36
result.push(byte);
37
} while (i !== 0);
38
39
return VSBuffer.fromByteArray(result);
40
}
41
42