|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Rev | Line | |
|---|
| [9af201e] | 1 | import type { StringReader, StringWriter } from './strings';
|
|---|
| 2 |
|
|---|
| 3 | export const comma = ','.charCodeAt(0);
|
|---|
| 4 | export const semicolon = ';'.charCodeAt(0);
|
|---|
| 5 |
|
|---|
| 6 | const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|---|
| 7 | const intToChar = new Uint8Array(64); // 64 possible chars.
|
|---|
| 8 | const charToInt = new Uint8Array(128); // z is 122 in ASCII
|
|---|
| 9 |
|
|---|
| 10 | for (let i = 0; i < chars.length; i++) {
|
|---|
| 11 | const c = chars.charCodeAt(i);
|
|---|
| 12 | intToChar[i] = c;
|
|---|
| 13 | charToInt[c] = i;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | export function decodeInteger(reader: StringReader, relative: number): number {
|
|---|
| 17 | let value = 0;
|
|---|
| 18 | let shift = 0;
|
|---|
| 19 | let integer = 0;
|
|---|
| 20 |
|
|---|
| 21 | do {
|
|---|
| 22 | const c = reader.next();
|
|---|
| 23 | integer = charToInt[c];
|
|---|
| 24 | value |= (integer & 31) << shift;
|
|---|
| 25 | shift += 5;
|
|---|
| 26 | } while (integer & 32);
|
|---|
| 27 |
|
|---|
| 28 | const shouldNegate = value & 1;
|
|---|
| 29 | value >>>= 1;
|
|---|
| 30 |
|
|---|
| 31 | if (shouldNegate) {
|
|---|
| 32 | value = -0x80000000 | -value;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | return relative + value;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | export function encodeInteger(builder: StringWriter, num: number, relative: number): number {
|
|---|
| 39 | let delta = num - relative;
|
|---|
| 40 |
|
|---|
| 41 | delta = delta < 0 ? (-delta << 1) | 1 : delta << 1;
|
|---|
| 42 | do {
|
|---|
| 43 | let clamped = delta & 0b011111;
|
|---|
| 44 | delta >>>= 5;
|
|---|
| 45 | if (delta > 0) clamped |= 0b100000;
|
|---|
| 46 | builder.write(intToChar[clamped]);
|
|---|
| 47 | } while (delta > 0);
|
|---|
| 48 |
|
|---|
| 49 | return num;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | export function hasMoreVlq(reader: StringReader, max: number) {
|
|---|
| 53 | if (reader.pos >= max) return false;
|
|---|
| 54 | return reader.peek() !== comma;
|
|---|
| 55 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.