<commit_before>const wchar_t = require('ref-wchar');

import { lib, xelib } from './lib';
import { PWChar, PCardinal, PInteger, PWordBool, PDouble, PByte} from './types';

// HELPER FUNCTIONS
export let createTypedBuffer = function(size, type) {
    let buf = new Buffer(size);
    buf.type = type;
    return buf;
};

export let readPWCharString = function(buf) {
    return wchar_t.toString(buf);
};

export let readCardinalArray = function(buf, len) {
    let a = [];
    for (let i = 0; i < 4 * len; i += 4)
        a.push(buf.readUInt32LE(i));
    return a;
};

export let wcb = function(value) {
    let buf = new Buffer((value.length + 1) * 2);
    buf.write(value, 0, 'ucs2');
    buf.type = PWChar;
    return buf;
};

export let elementContext = function(_id, path) {
    return `${_id}, "${path}"`;
};

export let flagContext = function(_id, path, name) {
    return `${_id}, "${path}\\${name}"`;
};

export let Fail = function(message) {
    try {
        let libMessage = xelib.GetExceptionMessage();
        if (libMessage) {
            console.log(libMessage);
            message +=  '\r\n' + libMessage;
        }
    } catch (e) {
        console.log('Unknown critical exception!');
    }
    throw new Error(message);
};

export let GetString = function(callback, method = 'GetResultString') {
    let _len = createTypedBuffer(4, PInteger);
    callback(_len);
    let len = _len.readInt32LE(0);
    if (len < 1) return '';
    let str = createTypedBuffer(2 * len, PWChar);
    if (!lib[method](str, len))
        Fail(`${method} failed.`);
    return readPWCharString(str);
};

export let GetHandle = function(callback) {<commit_msg>removed automatic console.logging of xelib failures<commit_after>const wchar_t = require('ref-wchar');

import { lib, xelib } from './lib';
import { PWChar, PCardinal, PInteger, PWordBool, PDouble, PByte} from './types';

// HELPER FUNCTIONS
export let createTypedBuffer = function(size, type) {
    let buf = new Buffer(size);
    buf.type = type;
    return buf;
};

export let readPWCharString = function(buf) {
    return wchar_t.toString(buf);
};

export let readCardinalArray = function(buf, len) {
    let a = [];
    for (let i = 0; i < 4 * len; i += 4)
        a.push(buf.readUInt32LE(i));
    return a;
};

export let wcb = function(value) {
    let buf = new Buffer((value.length + 1) * 2);
    buf.write(value, 0, 'ucs2');
    buf.type = PWChar;
    return buf;
};

export let elementContext = function(_id, path) {
    return `${_id}, "${path}"`;
};

export let flagContext = function(_id, path, name) {
    return `${_id}, "${path}\\${name}"`;
};

export let Fail = function(message) {
    try {
        let libMessage = xelib.GetExceptionMessage();
        if (libMessage) message +=  '\r\n' + libMessage;
    } catch (e) {
        console.log('Unknown critical exception!');
    }
    throw new Error(message);
};

export let GetString = function(callback, method = 'GetResultString') {
    let _len = createTypedBuffer(4, PInteger);
    callback(_len);
    let len = _len.readInt32LE(0);
    if (len < 1) return '';
    let str = createTypedBuffer(2 * len, PWChar);
    if (!lib[method](str, len))
        Fail(`${method} failed.`);
    return readPWCharString(str);
};

export let GetHandle = function(callback) {
{'fraction_non_alphanumeric': 0.15555555555555556, 'fraction_numerical': 0.0091324200913242, 'mean_word_length': 3.1859872611464968, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3846298', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1093, 'n_words': 298}