forked from BigfootDev/flatbuffers
[TS] Use TextEncoder and TextDecoder (#7400)
* [TS] Use TextEncoder TextEncoder is well supported these days, see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#browser_compatibility. * Reuse instance * Also use TextDecoder * Forgot to add text_decoder_ member Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -24,6 +24,7 @@ export class Builder {
|
||||
private force_defaults = false;
|
||||
|
||||
private string_maps: Map<string | Uint8Array, number> | null = null;
|
||||
private text_encoder = new TextEncoder();
|
||||
|
||||
/**
|
||||
* Create a FlatBufferBuilder.
|
||||
@@ -531,40 +532,7 @@ export class Builder {
|
||||
if (s instanceof Uint8Array) {
|
||||
utf8 = s;
|
||||
} else {
|
||||
utf8 = [];
|
||||
let i = 0;
|
||||
|
||||
while (i < s.length) {
|
||||
let codePoint;
|
||||
|
||||
// Decode UTF-16
|
||||
const a = s.charCodeAt(i++);
|
||||
if (a < 0xD800 || a >= 0xDC00) {
|
||||
codePoint = a;
|
||||
} else {
|
||||
const b = s.charCodeAt(i++);
|
||||
codePoint = (a << 10) + b + (0x10000 - (0xD800 << 10) - 0xDC00);
|
||||
}
|
||||
|
||||
// Encode UTF-8
|
||||
if (codePoint < 0x80) {
|
||||
utf8.push(codePoint);
|
||||
} else {
|
||||
if (codePoint < 0x800) {
|
||||
utf8.push(((codePoint >> 6) & 0x1F) | 0xC0);
|
||||
} else {
|
||||
if (codePoint < 0x10000) {
|
||||
utf8.push(((codePoint >> 12) & 0x0F) | 0xE0);
|
||||
} else {
|
||||
utf8.push(
|
||||
((codePoint >> 18) & 0x07) | 0xF0,
|
||||
((codePoint >> 12) & 0x3F) | 0x80);
|
||||
}
|
||||
utf8.push(((codePoint >> 6) & 0x3F) | 0x80);
|
||||
}
|
||||
utf8.push((codePoint & 0x3F) | 0x80);
|
||||
}
|
||||
}
|
||||
utf8 = this.text_encoder.encode(s);
|
||||
}
|
||||
|
||||
this.addInt8(0);
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Encoding } from "./encoding.js";
|
||||
|
||||
export class ByteBuffer {
|
||||
private position_ = 0;
|
||||
private text_decoder_ = new TextDecoder();
|
||||
|
||||
/**
|
||||
* Create a new ByteBuffer with a given array of bytes (`Uint8Array`)
|
||||
@@ -187,70 +188,22 @@ export class ByteBuffer {
|
||||
* Create a JavaScript string from UTF-8 data stored inside the FlatBuffer.
|
||||
* This allocates a new string and converts to wide chars upon each access.
|
||||
*
|
||||
* To avoid the conversion to UTF-16, pass Encoding.UTF8_BYTES as
|
||||
* the "optionalEncoding" argument. This is useful for avoiding conversion to
|
||||
* and from UTF-16 when the data will just be packaged back up in another
|
||||
* FlatBuffer later on.
|
||||
* To avoid the conversion to string, pass Encoding.UTF8_BYTES as the
|
||||
* "optionalEncoding" argument. This is useful for avoiding conversion when
|
||||
* the data will just be packaged back up in another FlatBuffer later on.
|
||||
*
|
||||
* @param offset
|
||||
* @param opt_encoding Defaults to UTF16_STRING
|
||||
*/
|
||||
__string(offset: number, opt_encoding?: Encoding): string | Uint8Array {
|
||||
offset += this.readInt32(offset);
|
||||
|
||||
const length = this.readInt32(offset);
|
||||
let result = '';
|
||||
let i = 0;
|
||||
|
||||
offset += SIZEOF_INT;
|
||||
|
||||
if (opt_encoding === Encoding.UTF8_BYTES) {
|
||||
return this.bytes_.subarray(offset, offset + length);
|
||||
}
|
||||
|
||||
while (i < length) {
|
||||
let codePoint;
|
||||
|
||||
// Decode UTF-8
|
||||
const a = this.readUint8(offset + i++);
|
||||
if (a < 0xC0) {
|
||||
codePoint = a;
|
||||
} else {
|
||||
const b = this.readUint8(offset + i++);
|
||||
if (a < 0xE0) {
|
||||
codePoint =
|
||||
((a & 0x1F) << 6) |
|
||||
(b & 0x3F);
|
||||
} else {
|
||||
const c = this.readUint8(offset + i++);
|
||||
if (a < 0xF0) {
|
||||
codePoint =
|
||||
((a & 0x0F) << 12) |
|
||||
((b & 0x3F) << 6) |
|
||||
(c & 0x3F);
|
||||
} else {
|
||||
const d = this.readUint8(offset + i++);
|
||||
codePoint =
|
||||
((a & 0x07) << 18) |
|
||||
((b & 0x3F) << 12) |
|
||||
((c & 0x3F) << 6) |
|
||||
(d & 0x3F);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Encode UTF-16
|
||||
if (codePoint < 0x10000) {
|
||||
result += String.fromCharCode(codePoint);
|
||||
} else {
|
||||
codePoint -= 0x10000;
|
||||
result += String.fromCharCode(
|
||||
(codePoint >> 10) + 0xD800,
|
||||
(codePoint & ((1 << 10) - 1)) + 0xDC00);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
const utf8bytes = this.bytes_.subarray(offset, offset + length);
|
||||
if (opt_encoding === Encoding.UTF8_BYTES)
|
||||
return utf8bytes;
|
||||
else
|
||||
return this.text_decoder_.decode(utf8bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user