bin-serde 1.6.2 → 1.6.3

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.
Files changed (3) hide show
  1. package/index.ts +27 -2
  2. package/lib/index.js +23 -2
  3. package/package.json +1 -1
package/index.ts CHANGED
@@ -53,27 +53,51 @@ export class Writer {
53
53
 
54
54
  writeUVarint(val: number) {
55
55
  if (val < 0x80) {
56
+ // 1 byte: 0-127
56
57
  this.writeUInt8(val);
57
58
  } else if (val < 0x4000) {
59
+ // 2 bytes: 128 - 16,383
58
60
  this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
59
61
  } else if (val < 0x200000) {
62
+ // 3 bytes: 16,384 - 2,097,151
60
63
  this.writeUInt8((val >> 14) | 0x80);
61
64
  this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
62
65
  } else if (val < 0x10000000) {
66
+ // 4 bytes: 2,097,152 - 268,435,455
63
67
  this.writeUInt32(
64
68
  (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
65
69
  );
66
70
  } else if (val < 0x800000000) {
71
+ // 5 bytes: 268,435,456 - 34,359,738,367
67
72
  this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
68
73
  this.writeUInt32(
69
74
  (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
70
75
  );
71
76
  } else if (val < 0x40000000000) {
77
+ // 6 bytes: 34,359,738,368 - 4,398,046,511,103
72
78
  const shiftedVal = Math.floor(val / 0x10000000);
73
79
  this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
74
80
  this.writeUInt32(
75
81
  (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
76
82
  );
83
+ } else if (val < 0x2000000000000) {
84
+ // 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
85
+ const shiftedVal = Math.floor(val / 0x10000000);
86
+ this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
87
+ this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
88
+ this.writeUInt32(
89
+ (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
90
+ );
91
+ } else if (val <= Number.MAX_SAFE_INTEGER) {
92
+ // 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
93
+ const shiftedVal = Math.floor(val / 0x10000000);
94
+ this.writeUInt16(
95
+ (Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080
96
+ );
97
+ this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
98
+ this.writeUInt32(
99
+ (val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000
100
+ );
77
101
  } else {
78
102
  throw new Error("Value out of range");
79
103
  }
@@ -81,7 +105,8 @@ export class Writer {
81
105
  }
82
106
 
83
107
  writeVarint(val: number) {
84
- return this.writeUVarint((val << 1) ^ (val >> 31));
108
+ const encoded = val >= 0 ? val * 2 : val * -2 - 1;
109
+ return this.writeUVarint(encoded);
85
110
  }
86
111
 
87
112
  writeFloat(val: number) {
@@ -229,7 +254,7 @@ export class Reader {
229
254
 
230
255
  readVarint() {
231
256
  const val = this.readUVarint();
232
- return (val >>> 1) ^ -(val & 1);
257
+ return val % 2 === 0 ? val / 2 : -(val + 1) / 2;
233
258
  }
234
259
 
235
260
  readFloat() {
package/lib/index.js CHANGED
@@ -44,34 +44,55 @@ export class Writer {
44
44
  }
45
45
  writeUVarint(val) {
46
46
  if (val < 0x80) {
47
+ // 1 byte: 0-127
47
48
  this.writeUInt8(val);
48
49
  }
49
50
  else if (val < 0x4000) {
51
+ // 2 bytes: 128 - 16,383
50
52
  this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
51
53
  }
52
54
  else if (val < 0x200000) {
55
+ // 3 bytes: 16,384 - 2,097,151
53
56
  this.writeUInt8((val >> 14) | 0x80);
54
57
  this.writeUInt16((val & 0x7f) | ((val & 0x3f80) << 1) | 0x8000);
55
58
  }
56
59
  else if (val < 0x10000000) {
60
+ // 4 bytes: 2,097,152 - 268,435,455
57
61
  this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
58
62
  }
59
63
  else if (val < 0x800000000) {
64
+ // 5 bytes: 268,435,456 - 34,359,738,367
60
65
  this.writeUInt8(Math.floor(val / 0x10000000) | 0x80);
61
66
  this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
62
67
  }
63
68
  else if (val < 0x40000000000) {
69
+ // 6 bytes: 34,359,738,368 - 4,398,046,511,103
64
70
  const shiftedVal = Math.floor(val / 0x10000000);
65
71
  this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
66
72
  this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
67
73
  }
74
+ else if (val < 0x2000000000000) {
75
+ // 7 bytes: 4,398,046,511,104 - 562,949,953,421,311
76
+ const shiftedVal = Math.floor(val / 0x10000000);
77
+ this.writeUInt8((Math.floor(shiftedVal / 0x4000) & 0x7f) | 0x80);
78
+ this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
79
+ this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
80
+ }
81
+ else if (val <= Number.MAX_SAFE_INTEGER) {
82
+ // 8 bytes: 562,949,953,421,312 - 9,007,199,254,740,991 (MAX_SAFE_INTEGER)
83
+ const shiftedVal = Math.floor(val / 0x10000000);
84
+ this.writeUInt16((Math.floor(shiftedVal / 0x4000) & 0x7f) | ((Math.floor(shiftedVal / 0x4000) & 0x3f80) << 1) | 0x8080);
85
+ this.writeUInt16((shiftedVal & 0x7f) | ((shiftedVal & 0x3f80) << 1) | 0x8080);
86
+ this.writeUInt32((val & 0x7f) | ((val & 0x3f80) << 1) | ((val & 0x1fc000) << 2) | ((val & 0xfe00000) << 3) | 0x80808000);
87
+ }
68
88
  else {
69
89
  throw new Error("Value out of range");
70
90
  }
71
91
  return this;
72
92
  }
73
93
  writeVarint(val) {
74
- return this.writeUVarint((val << 1) ^ (val >> 31));
94
+ const encoded = val >= 0 ? val * 2 : val * -2 - 1;
95
+ return this.writeUVarint(encoded);
75
96
  }
76
97
  writeFloat(val) {
77
98
  this.ensureSize(4);
@@ -203,7 +224,7 @@ export class Reader {
203
224
  }
204
225
  readVarint() {
205
226
  const val = this.readUVarint();
206
- return (val >>> 1) ^ -(val & 1);
227
+ return val % 2 === 0 ? val / 2 : -(val + 1) / 2;
207
228
  }
208
229
  readFloat() {
209
230
  const val = this.view.getFloat32(this.pos, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.6.2",
3
+ "version": "1.6.3",
4
4
  "description": "A low level library for efficiently writing and reading binary data in javascript",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",