bin-serde 1.6.9 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +9 -51
- package/lib/index.js +9 -47
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -59,55 +59,11 @@ export class Writer {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
writeUVarint(val: number) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} else if (val < 0x4000) {
|
|
66
|
-
// 2 bytes: 128 - 16,383
|
|
67
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
68
|
-
} else if (val < 0x200000) {
|
|
69
|
-
// 3 bytes: 16,384 - 2,097,151
|
|
70
|
-
this.writeUInt8((val >> 14) | 0x80);
|
|
71
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
72
|
-
} else if (val < 0x10000000) {
|
|
73
|
-
// 4 bytes: 2,097,152 - 268,435,455
|
|
74
|
-
this.writeUInt32(
|
|
75
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
76
|
-
);
|
|
77
|
-
} else if (val < 0x800000000) {
|
|
78
|
-
// 5 bytes: 268,435,456 - 34,359,738,367
|
|
79
|
-
this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
|
|
80
|
-
this.writeUInt32(
|
|
81
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
82
|
-
);
|
|
83
|
-
} else if (val < 0x40000000000) {
|
|
84
|
-
// 6 bytes: 34,359,738,368 - 4,398,046,511,103
|
|
85
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
86
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
87
|
-
this.writeUInt32(
|
|
88
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
89
|
-
);
|
|
90
|
-
} else if (val < 0x2000000000000) {
|
|
91
|
-
// 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
|
|
92
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
93
|
-
this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
|
|
94
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
95
|
-
this.writeUInt32(
|
|
96
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
97
|
-
);
|
|
98
|
-
} else if (val <= Number.MAX_SAFE_INTEGER) {
|
|
99
|
-
// 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
|
|
100
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
101
|
-
this.writeUInt16(
|
|
102
|
-
(Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080
|
|
103
|
-
);
|
|
104
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
105
|
-
this.writeUInt32(
|
|
106
|
-
(val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
|
|
107
|
-
);
|
|
108
|
-
} else {
|
|
109
|
-
throw new Error("Value out of range");
|
|
62
|
+
while (val >= 0x80) {
|
|
63
|
+
this.writeUInt8((val & 0x7f) | 0x80);
|
|
64
|
+
val >>>= 7;
|
|
110
65
|
}
|
|
66
|
+
this.writeUInt8(val);
|
|
111
67
|
return this;
|
|
112
68
|
}
|
|
113
69
|
|
|
@@ -251,13 +207,15 @@ export class Reader {
|
|
|
251
207
|
}
|
|
252
208
|
|
|
253
209
|
readUVarint() {
|
|
254
|
-
let
|
|
210
|
+
let result = 0;
|
|
211
|
+
let shift = 0;
|
|
255
212
|
while (true) {
|
|
256
213
|
const byte = this.readUInt8();
|
|
214
|
+
result |= (byte & 0x7f) << shift;
|
|
257
215
|
if (byte < 0x80) {
|
|
258
|
-
return
|
|
216
|
+
return result;
|
|
259
217
|
}
|
|
260
|
-
|
|
218
|
+
shift += 7;
|
|
261
219
|
}
|
|
262
220
|
}
|
|
263
221
|
|
package/lib/index.js
CHANGED
|
@@ -48,51 +48,11 @@ export class Writer {
|
|
|
48
48
|
return this;
|
|
49
49
|
}
|
|
50
50
|
writeUVarint(val) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
else if (val < 0x4000) {
|
|
56
|
-
// 2 bytes: 128 - 16,383
|
|
57
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
58
|
-
}
|
|
59
|
-
else if (val < 0x200000) {
|
|
60
|
-
// 3 bytes: 16,384 - 2,097,151
|
|
61
|
-
this.writeUInt8((val >> 14) | 0x80);
|
|
62
|
-
this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
|
|
63
|
-
}
|
|
64
|
-
else if (val < 0x10000000) {
|
|
65
|
-
// 4 bytes: 2,097,152 - 268,435,455
|
|
66
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
67
|
-
}
|
|
68
|
-
else if (val < 0x800000000) {
|
|
69
|
-
// 5 bytes: 268,435,456 - 34,359,738,367
|
|
70
|
-
this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
|
|
71
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
72
|
-
}
|
|
73
|
-
else if (val < 0x40000000000) {
|
|
74
|
-
// 6 bytes: 34,359,738,368 - 4,398,046,511,103
|
|
75
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
76
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
77
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
78
|
-
}
|
|
79
|
-
else if (val < 0x2000000000000) {
|
|
80
|
-
// 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
|
|
81
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
82
|
-
this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
|
|
83
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
84
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
85
|
-
}
|
|
86
|
-
else if (val <= Number.MAX_SAFE_INTEGER) {
|
|
87
|
-
// 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
|
|
88
|
-
const shiftedVal = Math.floor(val / 0x10000000);
|
|
89
|
-
this.writeUInt16((Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080);
|
|
90
|
-
this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
|
|
91
|
-
this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
throw new Error("Value out of range");
|
|
51
|
+
while (val >= 0x80) {
|
|
52
|
+
this.writeUInt8((val & 0x7f) | 0x80);
|
|
53
|
+
val >>>= 7;
|
|
95
54
|
}
|
|
55
|
+
this.writeUInt8(val);
|
|
96
56
|
return this;
|
|
97
57
|
}
|
|
98
58
|
writeVarint(val) {
|
|
@@ -220,13 +180,15 @@ export class Reader {
|
|
|
220
180
|
return val;
|
|
221
181
|
}
|
|
222
182
|
readUVarint() {
|
|
223
|
-
let
|
|
183
|
+
let result = 0;
|
|
184
|
+
let shift = 0;
|
|
224
185
|
while (true) {
|
|
225
186
|
const byte = this.readUInt8();
|
|
187
|
+
result |= (byte & 0x7f) << shift;
|
|
226
188
|
if (byte < 0x80) {
|
|
227
|
-
return
|
|
189
|
+
return result;
|
|
228
190
|
}
|
|
229
|
-
|
|
191
|
+
shift += 7;
|
|
230
192
|
}
|
|
231
193
|
}
|
|
232
194
|
readVarint() {
|