From 90d9e087f7cbc5cda2d13a51975f50b54e43dbf5 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Tue, 2 Jan 2018 15:33:02 +0000 Subject: [PATCH] 'Take bytes' and 'Drop bytes' operations now support ArrayBuffers --- src/core/config/OperationConfig.js | 10 +++---- src/core/operations/Tidy.js | 44 ++++++++++++++++++------------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index a020c990..43e06c31 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -1913,9 +1913,9 @@ const OperationConfig = { }, "Drop bytes": { module: "Default", - description: "Cuts the specified number of bytes out of the data.", - inputType: "byteArray", - outputType: "byteArray", + description: "Cuts a slice of the specified number of bytes out of the data.", + inputType: "ArrayBuffer", + outputType: "ArrayBuffer", args: [ { name: "Start", @@ -1937,8 +1937,8 @@ const OperationConfig = { "Take bytes": { module: "Default", description: "Takes a slice of the specified number of bytes from the data.", - inputType: "byteArray", - outputType: "byteArray", + inputType: "ArrayBuffer", + outputType: "ArrayBuffer", args: [ { name: "Start", diff --git a/src/core/operations/Tidy.js b/src/core/operations/Tidy.js index 41f0561d..175d888e 100755 --- a/src/core/operations/Tidy.js +++ b/src/core/operations/Tidy.js @@ -101,32 +101,39 @@ const Tidy = { /** * Drop bytes operation. * - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args - * @returns {byteArray} + * @returns {ArrayBuffer} */ runDropBytes: function(input, args) { - let start = args[0], + const start = args[0], length = args[1], applyToEachLine = args[2]; if (start < 0 || length < 0) throw "Error: Invalid value"; - if (!applyToEachLine) - return input.slice(0, start).concat(input.slice(start+length, input.length)); + if (!applyToEachLine) { + 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 + const data = new Uint8Array(input); let lines = [], line = [], i; - for (i = 0; i < input.length; i++) { - if (input[i] === 0x0a) { + for (i = 0; i < data.length; i++) { + if (data[i] === 0x0a) { lines.push(line); line = []; } else { - line.push(input[i]); + line.push(data[i]); } } 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.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. * - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args - * @returns {byteArray} + * @returns {ArrayBuffer} */ runTakeBytes: function(input, args) { - let start = args[0], + const start = args[0], length = args[1], applyToEachLine = args[2]; @@ -170,16 +177,17 @@ const Tidy = { return input.slice(start, start+length); // Split input into lines + const data = new Uint8Array(input); let lines = [], - line = []; - let i; + line = [], + i; - for (i = 0; i < input.length; i++) { - if (input[i] === 0x0a) { + for (i = 0; i < data.length; i++) { + if (data[i] === 0x0a) { lines.push(line); line = []; } else { - line.push(input[i]); + line.push(data[i]); } } lines.push(line); @@ -189,7 +197,7 @@ const Tidy = { output = output.concat(lines[i].slice(start, start+length)); output.push(0x0a); } - return output.slice(0, output.length-1); + return new Uint8Array(output.slice(0, output.length-1)).buffer; },