UNPKG

uae-dap

Version:

Debug Adapter Protocol for Amiga development with FS-UAE or WinUAE

197 lines 5.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceAsync = exports.bitValue = exports.splitLines = exports.formatNumber = exports.NumberFormat = exports.formatDecimal = exports.formatAddress = exports.formatBinary = exports.formatHexadecimal = exports.compareStringsLowerCase = exports.base64ToHex = exports.hexToBase64 = exports.bytesToHex = exports.hexToBytes = exports.int32ToASCII = exports.int32ToBytes = exports.hexStringToASCII = exports.byteToASCII = exports.chunk = void 0; /** * Chunks a string * @param str String to chunk * @param n Array of check elements */ function chunk(str, n) { const out = []; for (let i = 0; i < str.length; i += n) { out.push(str.substring(i, i + n)); } return out; } exports.chunk = chunk; /** * Converts a byte to a character * @param byte byte to convert * @return character in string */ function byteToASCII(byte) { let asciiContents; if (byte < 32 || (byte > 127 && byte < 161) || byte > 255) { asciiContents = "."; } else { asciiContents = String.fromCharCode(byte); } return asciiContents; } exports.byteToASCII = byteToASCII; /** * Converts a string containing hex values to an ascii string * @param value string to convert * @param chunkSize Size of the chuck of hex values * @return ascii string */ function hexStringToASCII(value, chunkSize) { let asciiContents = ""; const chunks = chunk(value, chunkSize); for (const c of chunks) { const i = parseInt(c, 16); asciiContents += byteToASCII(i); } return asciiContents; } exports.hexStringToASCII = hexStringToASCII; /** * Converts a int32 in an array of bytes * @param num Number to convert * @return array of bytes */ function int32ToBytes(num) { return [ (num & 0xff000000) >> 24, (num & 0x00ff0000) >> 16, (num & 0x0000ff00) >> 8, num & 0x000000ff, ]; } exports.int32ToBytes = int32ToBytes; /** * Converts a int 32 to an ascii string * @param value integer to convert * @return ascii string */ function int32ToASCII(value) { let asciiContents = ""; const bytes = int32ToBytes(value); for (const i of bytes) { asciiContents += byteToASCII(i); } return asciiContents; } exports.int32ToASCII = int32ToASCII; /** * Convert a hex string to a byte array **/ function hexToBytes(hex) { const bytes = new Array(); for (let c = 0; c < hex.length; c += 2) { bytes.push(parseInt(hex.substring(c, c + 2), 16)); } return bytes; } exports.hexToBytes = hexToBytes; /** * Convert a byte array to a hex string **/ function bytesToHex(bytes) { const hex = Array(); for (let i = 0; i < bytes.length; i++) { const current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i]; hex.push((current >>> 4).toString(16)); hex.push((current & 0xf).toString(16)); } return hex.join(""); } exports.bytesToHex = bytesToHex; /** * Convert a hex string to a base64 string **/ function hexToBase64(hexString) { // Conversion to bytes const buffer = Buffer.from(hexToBytes(hexString)); return buffer.toString("base64"); } exports.hexToBase64 = hexToBase64; /** * Convert a base64 string to a hex string **/ function base64ToHex(base64String) { // Conversion to bytes return Buffer.from(base64String, "base64").toString("hex"); } exports.base64ToHex = base64ToHex; /** * Compare two strings. * @param a First string * @param b Second string * @return <0 if a>b, 0 if a=b, >0 if a<b */ function compareStringsLowerCase(a, b) { const aL = a.toLowerCase(); const bL = b.toLowerCase(); if (aL > bL) { return 1; } else if (aL < bL) { return -1; } else { return 0; } } exports.compareStringsLowerCase = compareStringsLowerCase; function formatHexadecimal(value, pad = 8) { const prefix = value < 0 ? "-0x" : "0x"; return prefix + Math.abs(value).toString(16).padStart(pad, "0"); } exports.formatHexadecimal = formatHexadecimal; function formatBinary(value, pad = 32) { return `0b${value.toString(2).padStart(pad, "0")}`; } exports.formatBinary = formatBinary; function formatAddress(value) { return `$${value.toString(16)}`; } exports.formatAddress = formatAddress; function formatDecimal(value) { return value.toString(10); } exports.formatDecimal = formatDecimal; var NumberFormat; (function (NumberFormat) { NumberFormat[NumberFormat["BINARY"] = 0] = "BINARY"; NumberFormat[NumberFormat["DECIMAL"] = 1] = "DECIMAL"; NumberFormat[NumberFormat["HEXADECIMAL"] = 2] = "HEXADECIMAL"; })(NumberFormat = exports.NumberFormat || (exports.NumberFormat = {})); function formatNumber(value, displayFormat = NumberFormat.DECIMAL, minBytes = 0) { switch (displayFormat) { case NumberFormat.BINARY: return formatBinary(value, minBytes * 8); case NumberFormat.HEXADECIMAL: return formatHexadecimal(value, minBytes * 2); case NumberFormat.DECIMAL: return formatDecimal(value); } } exports.formatNumber = formatNumber; function splitLines(value) { return value.split(/\r?\n/g); } exports.splitLines = splitLines; function bitValue(num, hi, lo = hi) { const mask = (((-1 << (hi - lo + 1)) ^ -1) << lo) >>> 0; return (num & mask) >> lo; } exports.bitValue = bitValue; /** * String replace with async callback */ async function replaceAsync(str, regex, // eslint-disable-next-line @typescript-eslint/no-explicit-any asyncFn) { const promises = []; str.replace(regex, (match, ...args) => { const promise = asyncFn(match, ...args); promises.push(promise); return match; }); const data = await Promise.all(promises); return str.replace(regex, () => data.shift()); } exports.replaceAsync = replaceAsync; //# sourceMappingURL=strings.js.map