UNPKG

chardetng-wasm

Version:

Makes chardetng, Firefox's character encoding detection, available to JS through Web Assembly

179 lines (150 loc) 5.17 kB
import * as wasm from './chardetng_wasm_bg.wasm'; const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); let cachegetUint8Memory0 = null; function getUint8Memory0() { if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) { cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer); } return cachegetUint8Memory0; } function getStringFromWasm0(ptr, len) { return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); } let WASM_VECTOR_LEN = 0; function passArray8ToWasm0(arg, malloc) { const ptr = malloc(arg.length * 1); getUint8Memory0().set(arg, ptr / 1); WASM_VECTOR_LEN = arg.length; return ptr; } let cachegetInt32Memory0 = null; function getInt32Memory0() { if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); } return cachegetInt32Memory0; } /** * @param {Uint8Array} buffer * @returns {string} */ export function detect(buffer) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); var ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc); var len0 = WASM_VECTOR_LEN; wasm.detect(retptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; return getStringFromWasm0(r0, r1); } finally { wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_free(r0, r1); } } function isLikeNone(x) { return x === undefined || x === null; } const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder; let cachedTextEncoder = new lTextEncoder('utf-8'); const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' ? function (arg, view) { return cachedTextEncoder.encodeInto(arg, view); } : function (arg, view) { const buf = cachedTextEncoder.encode(arg); view.set(buf); return { read: arg.length, written: buf.length }; }); function passStringToWasm0(arg, malloc, realloc) { if (realloc === undefined) { const buf = cachedTextEncoder.encode(arg); const ptr = malloc(buf.length); getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); WASM_VECTOR_LEN = buf.length; return ptr; } let len = arg.length; let ptr = malloc(len); const mem = getUint8Memory0(); let offset = 0; for (; offset < len; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } if (offset !== len) { if (offset !== 0) { arg = arg.slice(offset); } ptr = realloc(ptr, len, len = offset + arg.length * 3); const view = getUint8Memory0().subarray(ptr + offset, ptr + len); const ret = encodeString(arg, view); offset += ret.written; } WASM_VECTOR_LEN = offset; return ptr; } /** */ export class EncodingDetector { static __wrap(ptr) { const obj = Object.create(EncodingDetector.prototype); obj.ptr = ptr; return obj; } __destroy_into_raw() { const ptr = this.ptr; this.ptr = 0; return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_encodingdetector_free(ptr); } /** * @returns {EncodingDetector} */ static new() { var ret = wasm.encodingdetector_new(); return EncodingDetector.__wrap(ret); } /** * @param {Uint8Array} buffer * @param {boolean | undefined} is_last * @returns {boolean} */ feed(buffer, is_last) { var ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc); var len0 = WASM_VECTOR_LEN; var ret = wasm.encodingdetector_feed(this.ptr, ptr0, len0, isLikeNone(is_last) ? 0xFFFFFF : is_last ? 1 : 0); return ret !== 0; } /** * @param {string | undefined} top_level_domain * @param {boolean | undefined} allow_utf8 * @returns {string} */ guess(top_level_domain, allow_utf8) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); var ptr0 = isLikeNone(top_level_domain) ? 0 : passStringToWasm0(top_level_domain, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); var len0 = WASM_VECTOR_LEN; wasm.encodingdetector_guess(retptr, this.ptr, ptr0, len0, isLikeNone(allow_utf8) ? 0xFFFFFF : allow_utf8 ? 1 : 0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; return getStringFromWasm0(r0, r1); } finally { wasm.__wbindgen_add_to_stack_pointer(16); wasm.__wbindgen_free(r0, r1); } } } export const __wbindgen_throw = function(arg0, arg1) { throw new Error(getStringFromWasm0(arg0, arg1)); };