bin-serde 1.3.0 → 1.4.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 CHANGED
@@ -103,7 +103,19 @@ export class Writer {
103
103
  return this;
104
104
  }
105
105
 
106
- public writeString(val: string) {
106
+ public writeStringAscii(val: string) {
107
+ if (val.length === 0) {
108
+ this.writeUInt8(0);
109
+ return this;
110
+ }
111
+ this.ensureSize(val.length);
112
+ for (let i = 0; i < val.length; i++) {
113
+ this.view.setUint8(this.pos++, val.charCodeAt(i));
114
+ }
115
+ return this;
116
+ }
117
+
118
+ public writeStringUtf8(val: string) {
107
119
  if (val.length > 0) {
108
120
  const byteSize = utf8Size(val);
109
121
  this.writeUVarint(byteSize);
@@ -201,7 +213,18 @@ export class Reader {
201
213
  return bits;
202
214
  }
203
215
 
204
- public readString(len?: number) {
216
+ public readStringAscii(len: number) {
217
+ if (len === 0) {
218
+ return "";
219
+ }
220
+ let val = "";
221
+ for (let i = 0; i < len; i++) {
222
+ val += String.fromCharCode(this.view.getUint8(this.pos++));
223
+ }
224
+ return val;
225
+ }
226
+
227
+ public readStringUtf8(len?: number) {
205
228
  if (len === undefined) {
206
229
  len = this.readUVarint();
207
230
  }
package/lib/index.d.ts CHANGED
@@ -10,7 +10,8 @@ export declare class Writer {
10
10
  writeVarint(val: number): this;
11
11
  writeFloat(val: number): this;
12
12
  writeBits(bits: boolean[]): this;
13
- writeString(val: string): this;
13
+ writeStringAscii(val: string): this;
14
+ writeStringUtf8(val: string): this;
14
15
  writeBuffer(buf: Uint8Array): this;
15
16
  toBuffer(): Uint8Array;
16
17
  private ensureSize;
@@ -27,7 +28,8 @@ export declare class Reader {
27
28
  readVarint(): number;
28
29
  readFloat(): number;
29
30
  readBits(numBits: number): boolean[];
30
- readString(len?: number): string;
31
+ readStringAscii(len: number): string;
32
+ readStringUtf8(len?: number): string;
31
33
  readBuffer(numBytes: number): Uint8Array;
32
34
  remaining(): number;
33
35
  }
package/lib/index.js CHANGED
@@ -90,7 +90,18 @@ export class Writer {
90
90
  }
91
91
  return this;
92
92
  }
93
- writeString(val) {
93
+ writeStringAscii(val) {
94
+ if (val.length === 0) {
95
+ this.writeUInt8(0);
96
+ return this;
97
+ }
98
+ this.ensureSize(val.length);
99
+ for (let i = 0; i < val.length; i++) {
100
+ this.view.setUint8(this.pos++, val.charCodeAt(i));
101
+ }
102
+ return this;
103
+ }
104
+ writeStringUtf8(val) {
94
105
  if (val.length > 0) {
95
106
  const byteSize = utf8Size(val);
96
107
  this.writeUVarint(byteSize);
@@ -176,7 +187,17 @@ export class Reader {
176
187
  this.pos += numBytes;
177
188
  return bits;
178
189
  }
179
- readString(len) {
190
+ readStringAscii(len) {
191
+ if (len === 0) {
192
+ return "";
193
+ }
194
+ let val = "";
195
+ for (let i = 0; i < len; i++) {
196
+ val += String.fromCharCode(this.view.getUint8(this.pos++));
197
+ }
198
+ return val;
199
+ }
200
+ readStringUtf8(len) {
180
201
  if (len === undefined) {
181
202
  len = this.readUVarint();
182
203
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
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",