deno.land / std@0.166.0 / node / string_decoder.ts

string_decoder.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { Buffer } from "./buffer.ts";import { normalizeEncoding as castEncoding, notImplemented } from "./_utils.ts";
enum NotImplemented { "ascii", "latin1", "utf16le",}
function normalizeEncoding(enc?: string): string { const encoding = castEncoding(enc ?? null); if (encoding && encoding in NotImplemented) notImplemented(encoding); if (!encoding && typeof enc === "string" && enc.toLowerCase() !== "raw") { throw new Error(`Unknown encoding: ${enc}`); } return String(encoding);}
/** * Check is `ArrayBuffer` and not `TypedArray`. Typescript allowed `TypedArray` to be passed as `ArrayBuffer` and does not do a deep check */
function isBufferType(buf: Buffer) { return buf instanceof ArrayBuffer && buf.BYTES_PER_ELEMENT;}
/* * Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a * continuation byte. If an invalid byte is detected, -2 is returned. * */function utf8CheckByte(byte: number): number { if (byte <= 0x7f) return 0; else if (byte >> 5 === 0x06) return 2; else if (byte >> 4 === 0x0e) return 3; else if (byte >> 3 === 0x1e) return 4; return byte >> 6 === 0x02 ? -1 : -2;}
/* * Checks at most 3 bytes at the end of a Buffer in order to detect an * incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4) * needed to complete the UTF-8 character (if applicable) are returned. * */function utf8CheckIncomplete( self: StringDecoderBase, buf: Buffer, i: number,): number { let j = buf.length - 1; if (j < i) return 0; let nb = utf8CheckByte(buf[j]); if (nb >= 0) { if (nb > 0) self.lastNeed = nb - 1; return nb; } if (--j < i || nb === -2) return 0; nb = utf8CheckByte(buf[j]); if (nb >= 0) { if (nb > 0) self.lastNeed = nb - 2; return nb; } if (--j < i || nb === -2) return 0; nb = utf8CheckByte(buf[j]); if (nb >= 0) { if (nb > 0) { if (nb === 2) nb = 0; else self.lastNeed = nb - 3; } return nb; } return 0;}
/* * Validates as many continuation bytes for a multi-byte UTF-8 character as * needed or are available. If we see a non-continuation byte where we expect * one, we "replace" the validated continuation bytes we've seen so far with * a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding * behavior. The continuation byte check is included three times in the case * where all of the continuation bytes for a character exist in the same buffer. * It is also done this way as a slight performance increase instead of using a * loop. * */function utf8CheckExtraBytes( self: StringDecoderBase, buf: Buffer,): string | undefined { if ((buf[0] & 0xc0) !== 0x80) { self.lastNeed = 0; return "\ufffd"; } if (self.lastNeed > 1 && buf.length > 1) { if ((buf[1] & 0xc0) !== 0x80) { self.lastNeed = 1; return "\ufffd"; } if (self.lastNeed > 2 && buf.length > 2) { if ((buf[2] & 0xc0) !== 0x80) { self.lastNeed = 2; return "\ufffd"; } } }}
/* * Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer. * */function utf8FillLastComplete( this: StringDecoderBase, buf: Buffer,): string | undefined { const p = this.lastTotal - this.lastNeed; const r = utf8CheckExtraBytes(this, buf); if (r !== undefined) return r; if (this.lastNeed <= buf.length) { buf.copy(this.lastChar, p, 0, this.lastNeed); return this.lastChar.toString(this.encoding, 0, this.lastTotal); } buf.copy(this.lastChar, p, 0, buf.length); this.lastNeed -= buf.length;}
/* * Attempts to complete a partial non-UTF-8 character using bytes from a Buffer * */function utf8FillLastIncomplete( this: StringDecoderBase, buf: Buffer,): string | undefined { if (this.lastNeed <= buf.length) { buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed); return this.lastChar.toString(this.encoding, 0, this.lastTotal); } buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length); this.lastNeed -= buf.length;}
/* * Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a * partial character, the character's bytes are buffered until the required * number of bytes are available. * */function utf8Text(this: StringDecoderBase, buf: Buffer, i: number): string { const total = utf8CheckIncomplete(this, buf, i); if (!this.lastNeed) return buf.toString("utf8", i); this.lastTotal = total; const end = buf.length - (total - this.lastNeed); buf.copy(this.lastChar, 0, end); return buf.toString("utf8", i, end);}
/* * For UTF-8, a replacement character is added when ending on a partial * character. * */function utf8End(this: Utf8Decoder, buf?: Buffer): string { const r = buf && buf.length ? this.write(buf) : ""; if (this.lastNeed) return r + "\ufffd"; return r;}
function utf8Write( this: Utf8Decoder | Base64Decoder, buf: Buffer | string,): string { if (typeof buf === "string") { return buf; } if (buf.length === 0) return ""; let r; let i; // Because `TypedArray` is recognized as `ArrayBuffer` but in the reality, there are some fundamental difference. We would need to cast it properly const normalizedBuffer: Buffer = isBufferType(buf) ? buf : Buffer.from(buf); if (this.lastNeed) { r = this.fillLast(normalizedBuffer); if (r === undefined) return ""; i = this.lastNeed; this.lastNeed = 0; } else { i = 0; } if (i < buf.length) { return r ? r + this.text(normalizedBuffer, i) : this.text(normalizedBuffer, i); } return r || "";}
function base64Text(this: StringDecoderBase, buf: Buffer, i: number): string { const n = (buf.length - i) % 3; if (n === 0) return buf.toString("base64", i); this.lastNeed = 3 - n; this.lastTotal = 3; if (n === 1) { this.lastChar[0] = buf[buf.length - 1]; } else { this.lastChar[0] = buf[buf.length - 2]; this.lastChar[1] = buf[buf.length - 1]; } return buf.toString("base64", i, buf.length - n);}
function base64End(this: Base64Decoder, buf?: Buffer): string { const r = buf && buf.length ? this.write(buf) : ""; if (this.lastNeed) { return r + this.lastChar.toString("base64", 0, 3 - this.lastNeed); } return r;}
function simpleWrite( this: StringDecoderBase, buf: Buffer | string,): string { if (typeof buf === "string") { return buf; } return buf.toString(this.encoding);}
function simpleEnd(this: GenericDecoder, buf?: Buffer): string { return buf && buf.length ? this.write(buf) : "";}
class StringDecoderBase { public lastChar: Buffer; public lastNeed = 0; public lastTotal = 0; constructor(public encoding: string, nb: number) { this.lastChar = Buffer.allocUnsafe(nb); }}
class Base64Decoder extends StringDecoderBase { public end = base64End; public fillLast = utf8FillLastIncomplete; public text = base64Text; public write = utf8Write;
constructor(encoding?: string) { super(normalizeEncoding(encoding), 3); }}
class GenericDecoder extends StringDecoderBase { public end = simpleEnd; public fillLast = undefined; public text = utf8Text; public write = simpleWrite;
constructor(encoding?: string) { super(normalizeEncoding(encoding), 4); }}
class Utf8Decoder extends StringDecoderBase { public end = utf8End; public fillLast = utf8FillLastComplete; public text = utf8Text; public write = utf8Write;
constructor(encoding?: string) { super(normalizeEncoding(encoding), 4); }}
/* * StringDecoder provides an interface for efficiently splitting a series of * buffers into a series of JS strings without breaking apart multi-byte * characters. * */export class StringDecoder { public encoding: string; public end: (buf?: Buffer) => string; public fillLast: ((buf: Buffer) => string | undefined) | undefined; public lastChar: Buffer; public lastNeed: number; public lastTotal: number; public text: (buf: Buffer, n: number) => string; public write: (buf: Buffer) => string;
constructor(encoding?: string) { const normalizedEncoding = normalizeEncoding(encoding); let decoder: Utf8Decoder | Base64Decoder | GenericDecoder; switch (normalizedEncoding) { case "utf8": decoder = new Utf8Decoder(encoding); break; case "base64": decoder = new Base64Decoder(encoding); break; default: decoder = new GenericDecoder(encoding); } this.encoding = decoder.encoding; this.end = decoder.end; this.fillLast = decoder.fillLast; this.lastChar = decoder.lastChar; this.lastNeed = decoder.lastNeed; this.lastTotal = decoder.lastTotal; this.text = decoder.text; this.write = decoder.write; }}// Allow calling StringDecoder() without newconst PStringDecoder = new Proxy(StringDecoder, { apply(_target, thisArg, args) { // @ts-ignore tedious to replicate types ... return Object.assign(thisArg, new StringDecoder(...args)); },});
export default { StringDecoder: PStringDecoder };
std

Version Info

Tagged at
a year ago