UNPKG

jtc-utils

Version:
50 lines 1.31 kB
import { StandardDecoder, } from "./charset.js"; class Utf8Charset { get name() { return "utf-8"; } createDecoder(options) { return new StandardDecoder("utf-8", options); } createEncoder(options) { return new Utf8Encoder(); } isUnicode() { return true; } isEbcdic() { return false; } } class Utf8Encoder { constructor() { this.encoder = new TextEncoder(); } canEncode(str) { return true; } encode(str, options) { const limit = options?.limit ?? Number.POSITIVE_INFINITY; const encoded = this.encoder.encode(str); if (encoded.length > limit) { let len = limit; if (encoded[limit - 1] >= 0xc2) { len = len - 1; } else if (encoded[limit - 1] >= 0x80) { if (encoded[limit - 2] >= 0xe0) { len = len - 2; } else if (encoded[limit - 2] >= 0x80) { if (encoded[limit - 3] >= 0xf0) { len = len - 3; } } } return encoded.subarray(len); } return encoded; } } export const utf8 = new Utf8Charset(); //# sourceMappingURL=utf8.js.map