Skip to content

Commit 849d41e

Browse files
committed
Removed padLeft and padRight in favour of String.prototype.padStart and padEnd. 'To Hex' now supports ArrayBuffers.
1 parent e18ec5f commit 849d41e

15 files changed

+77
-104
lines changed

src/core/Utils.js

+46-64
Original file line numberDiff line numberDiff line change
@@ -64,58 +64,6 @@ const Utils = {
6464
},
6565

6666

67-
/**
68-
* Adds leading zeros to strings
69-
*
70-
* @param {string} str - String to add leading characters to.
71-
* @param {number} max - Maximum width of the string.
72-
* @param {char} [chr='0'] - The character to pad with.
73-
* @returns {string}
74-
*
75-
* @example
76-
* // returns "0a"
77-
* Utils.padLeft("a", 2);
78-
*
79-
* // returns "000a"
80-
* Utils.padLeft("a", 4);
81-
*
82-
* // returns "xxxa"
83-
* Utils.padLeft("a", 4, "x");
84-
*
85-
* // returns "bcabchello"
86-
* Utils.padLeft("hello", 10, "abc");
87-
*/
88-
padLeft: function(str, max, chr) {
89-
chr = chr || "0";
90-
let startIndex = chr.length - (max - str.length);
91-
startIndex = startIndex < 0 ? 0 : startIndex;
92-
return str.length < max ?
93-
Utils.padLeft(chr.slice(startIndex, chr.length) + str, max, chr) : str;
94-
},
95-
96-
97-
/**
98-
* Adds trailing spaces to strings.
99-
*
100-
* @param {string} str - String to add trailing characters to.
101-
* @param {number} max - Maximum width of the string.
102-
* @param {char} [chr='0'] - The character to pad with.
103-
* @returns {string}
104-
*
105-
* @example
106-
* // returns "a "
107-
* Utils.padRight("a", 4);
108-
*
109-
* // returns "axxx"
110-
* Utils.padRight("a", 4, "x");
111-
*/
112-
padRight: function(str, max, chr) {
113-
chr = chr || " ";
114-
return str.length < max ?
115-
Utils.padRight(str + chr.slice(0, max-str.length), max, chr) : str;
116-
},
117-
118-
11967
/**
12068
* Adds trailing bytes to a byteArray.
12169
*
@@ -152,14 +100,6 @@ const Utils = {
152100
},
153101

154102

155-
/**
156-
* @alias Utils.padLeft
157-
*/
158-
pad: function(str, max, chr) {
159-
return Utils.padLeft(str, max, chr);
160-
},
161-
162-
163103
/**
164104
* Truncates a long string to max length and adds suffix.
165105
*
@@ -201,7 +141,7 @@ const Utils = {
201141
hex: function(c, length) {
202142
c = typeof c == "string" ? Utils.ord(c) : c;
203143
length = length || 2;
204-
return Utils.pad(c.toString(16), length);
144+
return c.toString(16).padStart(length, "0");
205145
},
206146

207147

@@ -222,7 +162,7 @@ const Utils = {
222162
bin: function(c, length) {
223163
c = typeof c == "string" ? Utils.ord(c) : c;
224164
length = length || 8;
225-
return Utils.pad(c.toString(2), length);
165+
return c.toString(2).padStart(length, "0");
226166
},
227167

228168

@@ -656,7 +596,7 @@ const Utils = {
656596
/**
657597
* Convert a byte array into a hex string.
658598
*
659-
* @param {byteArray} data
599+
* @param {Uint8Array|byteArray} data
660600
* @param {string} [delim=" "]
661601
* @param {number} [padding=2]
662602
* @returns {string}
@@ -676,7 +616,7 @@ const Utils = {
676616
let output = "";
677617

678618
for (let i = 0; i < data.length; i++) {
679-
output += Utils.pad(data[i].toString(16), padding) + delim;
619+
output += data[i].toString(16).padStart(padding, "0") + delim;
680620
}
681621

682622
// Add \x or 0x to beginning
@@ -1379,3 +1319,45 @@ Array.prototype.equals = function(other) {
13791319
String.prototype.count = function(chr) {
13801320
return this.split(chr).length - 1;
13811321
};
1322+
1323+
1324+
/*
1325+
* Polyfills
1326+
*/
1327+
1328+
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
1329+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
1330+
if (!String.prototype.padStart) {
1331+
String.prototype.padStart = function padStart(targetLength, padString) {
1332+
targetLength = targetLength>>0; //floor if number or convert non-number to 0;
1333+
padString = String((typeof padString !== "undefined" ? padString : " "));
1334+
if (this.length > targetLength) {
1335+
return String(this);
1336+
} else {
1337+
targetLength = targetLength-this.length;
1338+
if (targetLength > padString.length) {
1339+
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
1340+
}
1341+
return padString.slice(0, targetLength) + String(this);
1342+
}
1343+
};
1344+
}
1345+
1346+
1347+
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
1348+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
1349+
if (!String.prototype.padEnd) {
1350+
String.prototype.padEnd = function padEnd(targetLength, padString) {
1351+
targetLength = targetLength>>0; //floor if number or convert non-number to 0;
1352+
padString = String((typeof padString !== "undefined" ? padString : " "));
1353+
if (this.length > targetLength) {
1354+
return String(this);
1355+
} else {
1356+
targetLength = targetLength-this.length;
1357+
if (targetLength > padString.length) {
1358+
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
1359+
}
1360+
return String(this) + padString.slice(0, targetLength);
1361+
}
1362+
};
1363+
}

src/core/config/OperationConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ const OperationConfig = {
631631
description: "Converts the input string to hexadecimal bytes separated by the specified delimiter.<br><br>e.g. The UTF-8 encoded string <code>Γειά σου</code> becomes <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code>",
632632
highlight: "func",
633633
highlightReverse: "func",
634-
inputType: "byteArray",
634+
inputType: "ArrayBuffer",
635635
outputType: "string",
636636
args: [
637637
{

src/core/operations/BCD.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ const BCD = {
134134
switch (outputFormat) {
135135
case "Nibbles":
136136
return nibbles.map(n => {
137-
return Utils.padLeft(n.toString(2), 4);
137+
return n.toString(2).padStart(4, "0");
138138
}).join(" ");
139139
case "Bytes":
140140
return bytes.map(b => {
141-
return Utils.padLeft(b.toString(2), 8);
141+
return b.toString(2).padStart(8, "0");
142142
}).join(" ");
143143
case "Raw":
144144
default:

src/core/operations/ByteRepr.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ const ByteRepr = {
3131
/**
3232
* To Hex operation.
3333
*
34-
* @param {byteArray} input
34+
* @param {ArrayBuffer} input
3535
* @param {Object[]} args
3636
* @returns {string}
3737
*/
3838
runToHex: function(input, args) {
3939
const delim = Utils.charRep[args[0] || "Space"];
40-
return Utils.toHex(input, delim, 2);
40+
return Utils.toHex(new Uint8Array(input), delim, 2);
4141
},
4242

4343

@@ -266,7 +266,7 @@ const ByteRepr = {
266266
padding = 8;
267267

268268
for (let i = 0; i < input.length; i++) {
269-
output += Utils.pad(input[i].toString(2), padding) + delim;
269+
output += input[i].toString(2).padStart(padding, "0") + delim;
270270
}
271271

272272
if (delim.length) {

src/core/operations/Compress.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ const Compress = {
418418
}
419419
};
420420

421-
const fileSize = Utils.padLeft(input.length.toString(8), 11, "0");
421+
const fileSize = input.length.toString(8).padStart(11, "0");
422422
const currentUnixTimestamp = Math.floor(Date.now() / 1000);
423-
const lastModTime = Utils.padLeft(currentUnixTimestamp.toString(8), 11, "0");
423+
const lastModTime = currentUnixTimestamp.toString(8).padStart(11, "0");
424424

425425
const file = {
426426
fileName: Utils.padBytesRight(args[0], 100),
@@ -452,7 +452,7 @@ const Compress = {
452452
}
453453
});
454454
}
455-
checksum = Utils.padBytesRight(Utils.padLeft(checksum.toString(8), 7, "0"), 8);
455+
checksum = Utils.padBytesRight(checksum.toString(8).padStart(7, "0"), 8);
456456
file.checksum = checksum;
457457

458458
const tarball = new Tarball();

src/core/operations/Entropy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const Entropy = {
127127
for (i = 0; i < 256; i++) {
128128
if (distrib[i] || showZeroes) {
129129
output += " " + Utils.hex(i, 2) + " (" +
130-
Utils.padRight(percentages[i].toFixed(2).replace(".00", "") + "%)", 8) +
130+
(percentages[i].toFixed(2).replace(".00", "") + "%)").padEnd(8, " ") +
131131
Array(Math.ceil(percentages[i])+1).join("|") + "\n";
132132
}
133133
}

src/core/operations/HTML.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ const HTML = {
215215
k = k.toFixed(2);
216216

217217
let hex = "#" +
218-
Utils.padLeft(Math.round(r).toString(16), 2) +
219-
Utils.padLeft(Math.round(g).toString(16), 2) +
220-
Utils.padLeft(Math.round(b).toString(16), 2),
218+
Math.round(r).toString(16).padStart(2, "0") +
219+
Math.round(g).toString(16).padStart(2, "0") +
220+
Math.round(b).toString(16).padStart(2, "0"),
221221
rgb = "rgb(" + r + ", " + g + ", " + b + ")",
222222
rgba = "rgba(" + r + ", " + g + ", " + b + ", " + a + ")",
223223
hsl = "hsl(" + h + ", " + s + "%, " + l + "%)",

src/core/operations/Hexdump.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const Hexdump = {
5656
}
5757

5858
output += lineNo + " " +
59-
Utils.padRight(hexa, (length*(padding+1))) +
60-
" |" + Utils.padRight(Utils.printable(Utils.byteArrayToChars(buff)), buff.length) + "|\n";
59+
hexa.padEnd(length*(padding+1), " ") +
60+
" |" + Utils.printable(Utils.byteArrayToChars(buff)).padEnd(buff.length, " ") + "|\n";
6161

6262
if (includeFinalLength && i+buff.length === input.length) {
6363
output += Utils.hex(i+buff.length, 8) + "\n";

src/core/operations/PublicKey.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ const PublicKey = {
121121
// Format Public Key fields
122122
for (let i = 0; i < pkFields.length; i++) {
123123
pkStr += " " + pkFields[i].key + ":" +
124-
Utils.padLeft(
125-
pkFields[i].value + "\n",
124+
(pkFields[i].value + "\n").padStart(
126125
18 - (pkFields[i].key.length + 3) + pkFields[i].value.length + 1,
127126
" "
128127
);
@@ -286,9 +285,9 @@ ${extensions}`;
286285

287286
key = fields[i].split("=")[0];
288287
value = fields[i].split("=")[1];
289-
str = Utils.padRight(key, maxKeyLen) + " = " + value + "\n";
288+
str = key.padEnd(maxKeyLen, " ") + " = " + value + "\n";
290289

291-
output += Utils.padLeft(str, indent + str.length, " ");
290+
output += str.padStart(indent + str.length, " ");
292291
}
293292

294293
return output.slice(0, -1);
@@ -314,7 +313,7 @@ ${extensions}`;
314313
if (i === 0) {
315314
output += str;
316315
} else {
317-
output += Utils.padLeft(str, indent + str.length, " ");
316+
output += str.padStart(indent + str.length, " ");
318317
}
319318
}
320319

src/core/operations/SeqUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const SeqUtils = {
158158
width = lines.length.toString().length;
159159

160160
for (let n = 0; n < lines.length; n++) {
161-
output += Utils.pad((n+1).toString(), width, " ") + " " + lines[n] + "\n";
161+
output += (n+1).toString().padStart(width, " ") + " " + lines[n] + "\n";
162162
}
163163
return output.slice(0, output.length-1);
164164
},

src/core/operations/Tidy.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import Utils from "../Utils.js";
2-
3-
41
/**
52
* Tidy operations.
63
*
@@ -229,11 +226,11 @@ const Tidy = {
229226

230227
if (position === "Start") {
231228
for (i = 0; i < lines.length; i++) {
232-
output += Utils.padLeft(lines[i], lines[i].length+len, chr) + "\n";
229+
output += lines[i].padStart(lines[i].length+len, chr) + "\n";
233230
}
234231
} else if (position === "End") {
235232
for (i = 0; i < lines.length; i++) {
236-
output += Utils.padRight(lines[i], lines[i].length+len, chr) + "\n";
233+
output += lines[i].padEnd(lines[i].length+len, chr) + "\n";
237234
}
238235
}
239236

src/core/operations/URL.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* globals unescape */
2-
import Utils from "../Utils.js";
32
import url from "url";
43

54

@@ -78,7 +77,7 @@ const URL_ = {
7877

7978
output += "Arguments:\n";
8079
for (let key in uri.query) {
81-
output += "\t" + Utils.padRight(key, padding);
80+
output += "\t" + key.padEnd(padding, " ");
8281
if (uri.query[key].length) {
8382
output += " = " + uri.query[key] + "\n";
8483
} else {

src/web/HighlighterWaiter.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import Utils from "../core/Utils.js";
2-
3-
41
/**
52
* Waiter to handle events related to highlighting in CyberChef.
63
*
@@ -312,9 +309,9 @@ HighlighterWaiter.prototype.outputHtmlMousemove = function(e) {
312309
HighlighterWaiter.prototype.selectionInfo = function(start, end) {
313310
const len = end.toString().length;
314311
const width = len < 2 ? 2 : len;
315-
const startStr = Utils.pad(start.toString(), width, " ").replace(/ /g, "&nbsp;");
316-
const endStr = Utils.pad(end.toString(), width, " ").replace(/ /g, "&nbsp;");
317-
const lenStr = Utils.pad((end-start).toString(), width, " ").replace(/ /g, "&nbsp;");
312+
const startStr = start.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
313+
const endStr = end.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
314+
const lenStr = (end-start).toString().padStart(width, " ").replace(/ /g, "&nbsp;");
318315

319316
return "start: " + startStr + "<br>end: " + endStr + "<br>length: " + lenStr;
320317
};

src/web/InputWaiter.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Utils from "../core/Utils.js";
21
import LoaderWorker from "worker-loader?inline&fallback=false!./LoaderWorker.js";
32

43

@@ -100,8 +99,8 @@ InputWaiter.prototype.setInputInfo = function(length, lines) {
10099
let width = length.toString().length;
101100
width = width < 2 ? 2 : width;
102101

103-
const lengthStr = Utils.pad(length.toString(), width, " ").replace(/ /g, "&nbsp;");
104-
const linesStr = Utils.pad(lines.toString(), width, " ").replace(/ /g, "&nbsp;");
102+
const lengthStr = length.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
103+
const linesStr = lines.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
105104

106105
document.getElementById("input-info").innerHTML = "length: " + lengthStr + "<br>lines: " + linesStr;
107106
};

src/web/OutputWaiter.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ OutputWaiter.prototype.setOutputInfo = function(length, lines, duration) {
193193
let width = length.toString().length;
194194
width = width < 4 ? 4 : width;
195195

196-
const lengthStr = Utils.pad(length.toString(), width, " ").replace(/ /g, "&nbsp;");
197-
const timeStr = Utils.pad(duration.toString() + "ms", width, " ").replace(/ /g, "&nbsp;");
196+
const lengthStr = length.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
197+
const timeStr = (duration.toString() + "ms").padStart(width, " ").replace(/ /g, "&nbsp;");
198198

199199
let msg = "time: " + timeStr + "<br>length: " + lengthStr;
200200

201201
if (typeof lines === "number") {
202-
const linesStr = Utils.pad(lines.toString(), width, " ").replace(/ /g, "&nbsp;");
202+
const linesStr = lines.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
203203
msg += "<br>lines: " + linesStr;
204204
}
205205

0 commit comments

Comments
 (0)