mirror of
https://github.com/gchq/CyberChef.git
synced 2025-06-14 10:14:53 -04:00
Merge f92d780764
into c57556f49f
This commit is contained in:
commit
8224d8454c
4 changed files with 124 additions and 0 deletions
|
@ -299,6 +299,7 @@
|
|||
"From Case Insensitive Regex",
|
||||
"Add line numbers",
|
||||
"Remove line numbers",
|
||||
"Line Break",
|
||||
"Get All Casings",
|
||||
"To Table",
|
||||
"Reverse",
|
||||
|
|
79
src/core/operations/LineBreak.mjs
Normal file
79
src/core/operations/LineBreak.mjs
Normal file
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* @author ThomasNotTom
|
||||
* @copyright Crown Copyright 2025
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
|
||||
|
||||
/**
|
||||
* Line Break operation
|
||||
*/
|
||||
class LineBreak extends Operation {
|
||||
|
||||
/**
|
||||
* LineBreak constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Line Break";
|
||||
this.module = "Default";
|
||||
this.description = "Breaks the input text every <code>n</code> characters.";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Line break width",
|
||||
"type": "number",
|
||||
"value": 16,
|
||||
"min": 1
|
||||
}, {
|
||||
"name": "Remove leading whitespace",
|
||||
"type": "boolean",
|
||||
"value": false
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const lines = [];
|
||||
const lineWidth = args[0];
|
||||
const removeLeading = args[1];
|
||||
const msg = Utils.arrayBufferToStr(input, false);
|
||||
let i = 0;
|
||||
|
||||
while (i < msg.length) {
|
||||
let slice = msg.slice(i, i + lineWidth);
|
||||
let leadingWhitespace = 0;
|
||||
|
||||
if (removeLeading) {
|
||||
const match = slice.match(/^\s*/);
|
||||
leadingWhitespace = match ? match[0].length : 0;
|
||||
slice = slice.trimStart();
|
||||
}
|
||||
|
||||
slice = msg.slice(i, i + lineWidth + leadingWhitespace);
|
||||
|
||||
if (removeLeading) {
|
||||
slice = slice.trimStart();
|
||||
}
|
||||
|
||||
i += lineWidth + leadingWhitespace;
|
||||
|
||||
if (slice.length === 0) continue;
|
||||
lines.push(slice);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
export default LineBreak;
|
|
@ -100,6 +100,7 @@ import "./tests/JWTDecode.mjs";
|
|||
import "./tests/JWTSign.mjs";
|
||||
import "./tests/JWTVerify.mjs";
|
||||
import "./tests/LevenshteinDistance.mjs";
|
||||
import "./tests/LineBreak.mjs";
|
||||
import "./tests/Lorenz.mjs";
|
||||
import "./tests/LS47.mjs";
|
||||
import "./tests/LuhnChecksum.mjs";
|
||||
|
|
43
tests/operations/tests/LineBreak.mjs
Normal file
43
tests/operations/tests/LineBreak.mjs
Normal file
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* @author ThomasNotTom
|
||||
* @copyright Crown Copyright 2025
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
import TestRegister from "../../lib/TestRegister.mjs";
|
||||
|
||||
TestRegister.addTests([
|
||||
{
|
||||
// Check line remains unbroken
|
||||
name: "Line Break: No break",
|
||||
input: "Hello, world!",
|
||||
expectedOutput: "Hello, world!",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Line Break",
|
||||
"args": [32, false]
|
||||
}
|
||||
]
|
||||
}, {
|
||||
// Check line breaks
|
||||
name: "Line Break: With break",
|
||||
input: "Hello, world!",
|
||||
expectedOutput: "Hello,\n world\n!",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Line Break",
|
||||
"args": [6, false]
|
||||
}
|
||||
]
|
||||
}, {
|
||||
// Check line breaks and leading whitespace is removed.
|
||||
name: "Line Break: With break and no leading whitespace",
|
||||
input: "Hello, world!",
|
||||
expectedOutput: "Hello,\nworld!",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Line Break",
|
||||
"args": [6, true]
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
Loading…
Add table
Add a link
Reference in a new issue