'Take bytes' and 'Drop bytes' operations now support ArrayBuffers

This commit is contained in:
n1474335 2018-01-02 15:33:02 +00:00
parent 50b24d9a56
commit 90d9e087f7
2 changed files with 31 additions and 23 deletions

View File

@ -1913,9 +1913,9 @@ const OperationConfig = {
}, },
"Drop bytes": { "Drop bytes": {
module: "Default", module: "Default",
description: "Cuts the specified number of bytes out of the data.", description: "Cuts a slice of the specified number of bytes out of the data.",
inputType: "byteArray", inputType: "ArrayBuffer",
outputType: "byteArray", outputType: "ArrayBuffer",
args: [ args: [
{ {
name: "Start", name: "Start",
@ -1937,8 +1937,8 @@ const OperationConfig = {
"Take bytes": { "Take bytes": {
module: "Default", module: "Default",
description: "Takes a slice of the specified number of bytes from the data.", description: "Takes a slice of the specified number of bytes from the data.",
inputType: "byteArray", inputType: "ArrayBuffer",
outputType: "byteArray", outputType: "ArrayBuffer",
args: [ args: [
{ {
name: "Start", name: "Start",

View File

@ -101,32 +101,39 @@ const Tidy = {
/** /**
* Drop bytes operation. * Drop bytes operation.
* *
* @param {byteArray} input * @param {ArrayBuffer} input
* @param {Object[]} args * @param {Object[]} args
* @returns {byteArray} * @returns {ArrayBuffer}
*/ */
runDropBytes: function(input, args) { runDropBytes: function(input, args) {
let start = args[0], const start = args[0],
length = args[1], length = args[1],
applyToEachLine = args[2]; applyToEachLine = args[2];
if (start < 0 || length < 0) if (start < 0 || length < 0)
throw "Error: Invalid value"; throw "Error: Invalid value";
if (!applyToEachLine) if (!applyToEachLine) {
return input.slice(0, start).concat(input.slice(start+length, input.length)); const left = input.slice(0, start),
right = input.slice(start + length, input.byteLength);
let result = new Uint8Array(left.byteLength + right.byteLength);
result.set(new Uint8Array(left), 0);
result.set(new Uint8Array(right), left.byteLength);
return result.buffer;
}
// Split input into lines // Split input into lines
const data = new Uint8Array(input);
let lines = [], let lines = [],
line = [], line = [],
i; i;
for (i = 0; i < input.length; i++) { for (i = 0; i < data.length; i++) {
if (input[i] === 0x0a) { if (data[i] === 0x0a) {
lines.push(line); lines.push(line);
line = []; line = [];
} else { } else {
line.push(input[i]); line.push(data[i]);
} }
} }
lines.push(line); lines.push(line);
@ -136,7 +143,7 @@ const Tidy = {
output = output.concat(lines[i].slice(0, start).concat(lines[i].slice(start+length, lines[i].length))); output = output.concat(lines[i].slice(0, start).concat(lines[i].slice(start+length, lines[i].length)));
output.push(0x0a); output.push(0x0a);
} }
return output.slice(0, output.length-1); return new Uint8Array(output.slice(0, output.length-1)).buffer;
}, },
@ -154,12 +161,12 @@ const Tidy = {
/** /**
* Take bytes operation. * Take bytes operation.
* *
* @param {byteArray} input * @param {ArrayBuffer} input
* @param {Object[]} args * @param {Object[]} args
* @returns {byteArray} * @returns {ArrayBuffer}
*/ */
runTakeBytes: function(input, args) { runTakeBytes: function(input, args) {
let start = args[0], const start = args[0],
length = args[1], length = args[1],
applyToEachLine = args[2]; applyToEachLine = args[2];
@ -170,16 +177,17 @@ const Tidy = {
return input.slice(start, start+length); return input.slice(start, start+length);
// Split input into lines // Split input into lines
const data = new Uint8Array(input);
let lines = [], let lines = [],
line = []; line = [],
let i; i;
for (i = 0; i < input.length; i++) { for (i = 0; i < data.length; i++) {
if (input[i] === 0x0a) { if (data[i] === 0x0a) {
lines.push(line); lines.push(line);
line = []; line = [];
} else { } else {
line.push(input[i]); line.push(data[i]);
} }
} }
lines.push(line); lines.push(line);
@ -189,7 +197,7 @@ const Tidy = {
output = output.concat(lines[i].slice(start, start+length)); output = output.concat(lines[i].slice(start, start+length));
output.push(0x0a); output.push(0x0a);
} }
return output.slice(0, output.length-1); return new Uint8Array(output.slice(0, output.length-1)).buffer;
}, },