File size: 2,702 Bytes
b0bfea8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import { IndexedByteArray } from "../indexed_array.js";
/**
* @param dataArray {IndexedByteArray}
* @param bytes {number}
* @param encoding {string} the textElement encoding
* @param trimEnd {boolean} if we should trim once we reach an invalid byte
* @returns {string}
*/
export function readBytesAsString(dataArray, bytes, encoding = undefined, trimEnd = true)
{
if (!encoding)
{
let finished = false;
let string = "";
for (let i = 0; i < bytes; i++)
{
let byte = dataArray[dataArray.currentIndex++];
if (finished)
{
continue;
}
if ((byte < 32 || byte > 127) && byte !== 10) // 10 is "\n"
{
if (trimEnd)
{
finished = true;
continue;
}
else
{
if (byte === 0)
{
finished = true;
continue;
}
}
}
string += String.fromCharCode(byte);
}
return string;
}
else
{
let byteBuffer = dataArray.slice(dataArray.currentIndex, dataArray.currentIndex + bytes);
dataArray.currentIndex += bytes;
let decoder = new TextDecoder(encoding.replace(/[^\x20-\x7E]/g, ""));
return decoder.decode(byteBuffer.buffer);
}
}
/**
* @param string {string}
* @param padLength {number}
* @returns {IndexedByteArray}
*/
export function getStringBytes(string, padLength = 0)
{
let len = string.length;
if (padLength > 0)
{
len = padLength;
}
const arr = new IndexedByteArray(len);
writeStringAsBytes(arr, string, padLength);
return arr;
}
/**
* @param string {string}
* @returns {IndexedByteArray}
*/
export function getStringBytesZero(string)
{
return getStringBytes(string, string.length + 1);
}
/**
* @param string {string}
* @param outArray {IndexedByteArray}
* @param padLength {number}
* @returns {IndexedByteArray} modified IN PLACE
*/
export function writeStringAsBytes(outArray, string, padLength = 0)
{
if (padLength > 0)
{
if (string.length > padLength)
{
string = string.slice(0, padLength);
}
}
for (let i = 0; i < string.length; i++)
{
outArray[outArray.currentIndex++] = string.charCodeAt(i);
}
// pad with zeros if needed
if (padLength > string.length)
{
for (let i = 0; i < padLength - string.length; i++)
{
outArray[outArray.currentIndex++] = 0;
}
}
return outArray;
} |