"use strict";
/**
 * Shared text encoding/decoding utilities.
 *
 * Caches encoder/decoder instances for performance.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.utf8Decoder = exports.utf8Encoder = void 0;
exports.encodeUtf8 = encodeUtf8;
exports.decodeUtf8 = decodeUtf8;
exports.decodeLatin1 = decodeLatin1;
exports.utf8Encoder = new TextEncoder();
exports.utf8Decoder = new TextDecoder("utf-8");
/**
 * Encode a string as UTF-8.
 */
function encodeUtf8(value) {
    return exports.utf8Encoder.encode(value);
}
/**
 * Decode bytes as UTF-8.
 */
function decodeUtf8(bytes) {
    return exports.utf8Decoder.decode(bytes);
}
/**
 * Decode bytes as Latin-1/byte-to-char.
 * Used as a fallback when UTF-8 flag is not set.
 */
function decodeLatin1(bytes) {
    // Avoid spreading huge arrays; build incrementally.
    let out = "";
    const chunkSize = 0x8000;
    for (let i = 0; i < bytes.length; i += chunkSize) {
        const chunk = bytes.subarray(i, i + chunkSize);
        out += String.fromCharCode(...chunk);
    }
    return out;
}
