From 849d41ee5600f1a389d1239d940256905b0e5c74 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Thu, 28 Dec 2017 14:38:57 +0000 Subject: [PATCH] Removed padLeft and padRight in favour of String.prototype.padStart and padEnd. 'To Hex' now supports ArrayBuffers. --- src/core/Utils.js | 110 ++++++++++++----------------- src/core/config/OperationConfig.js | 2 +- src/core/operations/BCD.js | 4 +- src/core/operations/ByteRepr.js | 6 +- src/core/operations/Compress.js | 6 +- src/core/operations/Entropy.js | 2 +- src/core/operations/HTML.js | 6 +- src/core/operations/Hexdump.js | 4 +- src/core/operations/PublicKey.js | 9 ++- src/core/operations/SeqUtils.js | 2 +- src/core/operations/Tidy.js | 7 +- src/core/operations/URL.js | 3 +- src/web/HighlighterWaiter.js | 9 +-- src/web/InputWaiter.js | 5 +- src/web/OutputWaiter.js | 6 +- 15 files changed, 77 insertions(+), 104 deletions(-) diff --git a/src/core/Utils.js b/src/core/Utils.js index 766bc118..a6ab48db 100755 --- a/src/core/Utils.js +++ b/src/core/Utils.js @@ -64,58 +64,6 @@ const Utils = { }, - /** - * Adds leading zeros to strings - * - * @param {string} str - String to add leading characters to. - * @param {number} max - Maximum width of the string. - * @param {char} [chr='0'] - The character to pad with. - * @returns {string} - * - * @example - * // returns "0a" - * Utils.padLeft("a", 2); - * - * // returns "000a" - * Utils.padLeft("a", 4); - * - * // returns "xxxa" - * Utils.padLeft("a", 4, "x"); - * - * // returns "bcabchello" - * Utils.padLeft("hello", 10, "abc"); - */ - padLeft: function(str, max, chr) { - chr = chr || "0"; - let startIndex = chr.length - (max - str.length); - startIndex = startIndex < 0 ? 0 : startIndex; - return str.length < max ? - Utils.padLeft(chr.slice(startIndex, chr.length) + str, max, chr) : str; - }, - - - /** - * Adds trailing spaces to strings. - * - * @param {string} str - String to add trailing characters to. - * @param {number} max - Maximum width of the string. - * @param {char} [chr='0'] - The character to pad with. - * @returns {string} - * - * @example - * // returns "a " - * Utils.padRight("a", 4); - * - * // returns "axxx" - * Utils.padRight("a", 4, "x"); - */ - padRight: function(str, max, chr) { - chr = chr || " "; - return str.length < max ? - Utils.padRight(str + chr.slice(0, max-str.length), max, chr) : str; - }, - - /** * Adds trailing bytes to a byteArray. * @@ -152,14 +100,6 @@ const Utils = { }, - /** - * @alias Utils.padLeft - */ - pad: function(str, max, chr) { - return Utils.padLeft(str, max, chr); - }, - - /** * Truncates a long string to max length and adds suffix. * @@ -201,7 +141,7 @@ const Utils = { hex: function(c, length) { c = typeof c == "string" ? Utils.ord(c) : c; length = length || 2; - return Utils.pad(c.toString(16), length); + return c.toString(16).padStart(length, "0"); }, @@ -222,7 +162,7 @@ const Utils = { bin: function(c, length) { c = typeof c == "string" ? Utils.ord(c) : c; length = length || 8; - return Utils.pad(c.toString(2), length); + return c.toString(2).padStart(length, "0"); }, @@ -656,7 +596,7 @@ const Utils = { /** * Convert a byte array into a hex string. * - * @param {byteArray} data + * @param {Uint8Array|byteArray} data * @param {string} [delim=" "] * @param {number} [padding=2] * @returns {string} @@ -676,7 +616,7 @@ const Utils = { let output = ""; for (let i = 0; i < data.length; i++) { - output += Utils.pad(data[i].toString(16), padding) + delim; + output += data[i].toString(16).padStart(padding, "0") + delim; } // Add \x or 0x to beginning @@ -1379,3 +1319,45 @@ Array.prototype.equals = function(other) { String.prototype.count = function(chr) { return this.split(chr).length - 1; }; + + +/* + * Polyfills + */ + +// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart +if (!String.prototype.padStart) { + String.prototype.padStart = function padStart(targetLength, padString) { + targetLength = targetLength>>0; //floor if number or convert non-number to 0; + padString = String((typeof padString !== "undefined" ? padString : " ")); + if (this.length > targetLength) { + return String(this); + } else { + targetLength = targetLength-this.length; + if (targetLength > padString.length) { + padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed + } + return padString.slice(0, targetLength) + String(this); + } + }; +} + + +// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd +if (!String.prototype.padEnd) { + String.prototype.padEnd = function padEnd(targetLength, padString) { + targetLength = targetLength>>0; //floor if number or convert non-number to 0; + padString = String((typeof padString !== "undefined" ? padString : " ")); + if (this.length > targetLength) { + return String(this); + } else { + targetLength = targetLength-this.length; + if (targetLength > padString.length) { + padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed + } + return String(this) + padString.slice(0, targetLength); + } + }; +} diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 0a4a6161..15f00df8 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -631,7 +631,7 @@ const OperationConfig = { description: "Converts the input string to hexadecimal bytes separated by the specified delimiter.

e.g. The UTF-8 encoded string Γειά σου becomes ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a", highlight: "func", highlightReverse: "func", - inputType: "byteArray", + inputType: "ArrayBuffer", outputType: "string", args: [ { diff --git a/src/core/operations/BCD.js b/src/core/operations/BCD.js index 63c0cda3..d3efbc71 100755 --- a/src/core/operations/BCD.js +++ b/src/core/operations/BCD.js @@ -134,11 +134,11 @@ const BCD = { switch (outputFormat) { case "Nibbles": return nibbles.map(n => { - return Utils.padLeft(n.toString(2), 4); + return n.toString(2).padStart(4, "0"); }).join(" "); case "Bytes": return bytes.map(b => { - return Utils.padLeft(b.toString(2), 8); + return b.toString(2).padStart(8, "0"); }).join(" "); case "Raw": default: diff --git a/src/core/operations/ByteRepr.js b/src/core/operations/ByteRepr.js index 87a543ff..986926ca 100755 --- a/src/core/operations/ByteRepr.js +++ b/src/core/operations/ByteRepr.js @@ -31,13 +31,13 @@ const ByteRepr = { /** * To Hex operation. * - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {string} */ runToHex: function(input, args) { const delim = Utils.charRep[args[0] || "Space"]; - return Utils.toHex(input, delim, 2); + return Utils.toHex(new Uint8Array(input), delim, 2); }, @@ -266,7 +266,7 @@ const ByteRepr = { padding = 8; for (let i = 0; i < input.length; i++) { - output += Utils.pad(input[i].toString(2), padding) + delim; + output += input[i].toString(2).padStart(padding, "0") + delim; } if (delim.length) { diff --git a/src/core/operations/Compress.js b/src/core/operations/Compress.js index 639b89c6..57b01027 100755 --- a/src/core/operations/Compress.js +++ b/src/core/operations/Compress.js @@ -418,9 +418,9 @@ const Compress = { } }; - const fileSize = Utils.padLeft(input.length.toString(8), 11, "0"); + const fileSize = input.length.toString(8).padStart(11, "0"); const currentUnixTimestamp = Math.floor(Date.now() / 1000); - const lastModTime = Utils.padLeft(currentUnixTimestamp.toString(8), 11, "0"); + const lastModTime = currentUnixTimestamp.toString(8).padStart(11, "0"); const file = { fileName: Utils.padBytesRight(args[0], 100), @@ -452,7 +452,7 @@ const Compress = { } }); } - checksum = Utils.padBytesRight(Utils.padLeft(checksum.toString(8), 7, "0"), 8); + checksum = Utils.padBytesRight(checksum.toString(8).padStart(7, "0"), 8); file.checksum = checksum; const tarball = new Tarball(); diff --git a/src/core/operations/Entropy.js b/src/core/operations/Entropy.js index db860720..baf9edb3 100755 --- a/src/core/operations/Entropy.js +++ b/src/core/operations/Entropy.js @@ -127,7 +127,7 @@ const Entropy = { for (i = 0; i < 256; i++) { if (distrib[i] || showZeroes) { output += " " + Utils.hex(i, 2) + " (" + - Utils.padRight(percentages[i].toFixed(2).replace(".00", "") + "%)", 8) + + (percentages[i].toFixed(2).replace(".00", "") + "%)").padEnd(8, " ") + Array(Math.ceil(percentages[i])+1).join("|") + "\n"; } } diff --git a/src/core/operations/HTML.js b/src/core/operations/HTML.js index 30eda63e..b1f7e065 100755 --- a/src/core/operations/HTML.js +++ b/src/core/operations/HTML.js @@ -215,9 +215,9 @@ const HTML = { k = k.toFixed(2); let hex = "#" + - Utils.padLeft(Math.round(r).toString(16), 2) + - Utils.padLeft(Math.round(g).toString(16), 2) + - Utils.padLeft(Math.round(b).toString(16), 2), + Math.round(r).toString(16).padStart(2, "0") + + Math.round(g).toString(16).padStart(2, "0") + + Math.round(b).toString(16).padStart(2, "0"), rgb = "rgb(" + r + ", " + g + ", " + b + ")", rgba = "rgba(" + r + ", " + g + ", " + b + ", " + a + ")", hsl = "hsl(" + h + ", " + s + "%, " + l + "%)", diff --git a/src/core/operations/Hexdump.js b/src/core/operations/Hexdump.js index a9ed7a10..6b322b1f 100755 --- a/src/core/operations/Hexdump.js +++ b/src/core/operations/Hexdump.js @@ -56,8 +56,8 @@ const Hexdump = { } output += lineNo + " " + - Utils.padRight(hexa, (length*(padding+1))) + - " |" + Utils.padRight(Utils.printable(Utils.byteArrayToChars(buff)), buff.length) + "|\n"; + hexa.padEnd(length*(padding+1), " ") + + " |" + Utils.printable(Utils.byteArrayToChars(buff)).padEnd(buff.length, " ") + "|\n"; if (includeFinalLength && i+buff.length === input.length) { output += Utils.hex(i+buff.length, 8) + "\n"; diff --git a/src/core/operations/PublicKey.js b/src/core/operations/PublicKey.js index 295a5bf4..66b177a5 100755 --- a/src/core/operations/PublicKey.js +++ b/src/core/operations/PublicKey.js @@ -121,8 +121,7 @@ const PublicKey = { // Format Public Key fields for (let i = 0; i < pkFields.length; i++) { pkStr += " " + pkFields[i].key + ":" + - Utils.padLeft( - pkFields[i].value + "\n", + (pkFields[i].value + "\n").padStart( 18 - (pkFields[i].key.length + 3) + pkFields[i].value.length + 1, " " ); @@ -286,9 +285,9 @@ ${extensions}`; key = fields[i].split("=")[0]; value = fields[i].split("=")[1]; - str = Utils.padRight(key, maxKeyLen) + " = " + value + "\n"; + str = key.padEnd(maxKeyLen, " ") + " = " + value + "\n"; - output += Utils.padLeft(str, indent + str.length, " "); + output += str.padStart(indent + str.length, " "); } return output.slice(0, -1); @@ -314,7 +313,7 @@ ${extensions}`; if (i === 0) { output += str; } else { - output += Utils.padLeft(str, indent + str.length, " "); + output += str.padStart(indent + str.length, " "); } } diff --git a/src/core/operations/SeqUtils.js b/src/core/operations/SeqUtils.js index 3272e706..fa900cf9 100755 --- a/src/core/operations/SeqUtils.js +++ b/src/core/operations/SeqUtils.js @@ -158,7 +158,7 @@ const SeqUtils = { width = lines.length.toString().length; for (let n = 0; n < lines.length; n++) { - output += Utils.pad((n+1).toString(), width, " ") + " " + lines[n] + "\n"; + output += (n+1).toString().padStart(width, " ") + " " + lines[n] + "\n"; } return output.slice(0, output.length-1); }, diff --git a/src/core/operations/Tidy.js b/src/core/operations/Tidy.js index 881508cf..41f0561d 100755 --- a/src/core/operations/Tidy.js +++ b/src/core/operations/Tidy.js @@ -1,6 +1,3 @@ -import Utils from "../Utils.js"; - - /** * Tidy operations. * @@ -229,11 +226,11 @@ const Tidy = { if (position === "Start") { for (i = 0; i < lines.length; i++) { - output += Utils.padLeft(lines[i], lines[i].length+len, chr) + "\n"; + output += lines[i].padStart(lines[i].length+len, chr) + "\n"; } } else if (position === "End") { for (i = 0; i < lines.length; i++) { - output += Utils.padRight(lines[i], lines[i].length+len, chr) + "\n"; + output += lines[i].padEnd(lines[i].length+len, chr) + "\n"; } } diff --git a/src/core/operations/URL.js b/src/core/operations/URL.js index 25526f0e..2f30c952 100755 --- a/src/core/operations/URL.js +++ b/src/core/operations/URL.js @@ -1,5 +1,4 @@ /* globals unescape */ -import Utils from "../Utils.js"; import url from "url"; @@ -78,7 +77,7 @@ const URL_ = { output += "Arguments:\n"; for (let key in uri.query) { - output += "\t" + Utils.padRight(key, padding); + output += "\t" + key.padEnd(padding, " "); if (uri.query[key].length) { output += " = " + uri.query[key] + "\n"; } else { diff --git a/src/web/HighlighterWaiter.js b/src/web/HighlighterWaiter.js index d9980fec..6e4ca599 100755 --- a/src/web/HighlighterWaiter.js +++ b/src/web/HighlighterWaiter.js @@ -1,6 +1,3 @@ -import Utils from "../core/Utils.js"; - - /** * Waiter to handle events related to highlighting in CyberChef. * @@ -312,9 +309,9 @@ HighlighterWaiter.prototype.outputHtmlMousemove = function(e) { HighlighterWaiter.prototype.selectionInfo = function(start, end) { const len = end.toString().length; const width = len < 2 ? 2 : len; - const startStr = Utils.pad(start.toString(), width, " ").replace(/ /g, " "); - const endStr = Utils.pad(end.toString(), width, " ").replace(/ /g, " "); - const lenStr = Utils.pad((end-start).toString(), width, " ").replace(/ /g, " "); + const startStr = start.toString().padStart(width, " ").replace(/ /g, " "); + const endStr = end.toString().padStart(width, " ").replace(/ /g, " "); + const lenStr = (end-start).toString().padStart(width, " ").replace(/ /g, " "); return "start: " + startStr + "
end: " + endStr + "
length: " + lenStr; }; diff --git a/src/web/InputWaiter.js b/src/web/InputWaiter.js index af5ed81f..af3f72ee 100755 --- a/src/web/InputWaiter.js +++ b/src/web/InputWaiter.js @@ -1,4 +1,3 @@ -import Utils from "../core/Utils.js"; import LoaderWorker from "worker-loader?inline&fallback=false!./LoaderWorker.js"; @@ -100,8 +99,8 @@ InputWaiter.prototype.setInputInfo = function(length, lines) { let width = length.toString().length; width = width < 2 ? 2 : width; - const lengthStr = Utils.pad(length.toString(), width, " ").replace(/ /g, " "); - const linesStr = Utils.pad(lines.toString(), width, " ").replace(/ /g, " "); + const lengthStr = length.toString().padStart(width, " ").replace(/ /g, " "); + const linesStr = lines.toString().padStart(width, " ").replace(/ /g, " "); document.getElementById("input-info").innerHTML = "length: " + lengthStr + "
lines: " + linesStr; }; diff --git a/src/web/OutputWaiter.js b/src/web/OutputWaiter.js index 06379b94..42742734 100755 --- a/src/web/OutputWaiter.js +++ b/src/web/OutputWaiter.js @@ -193,13 +193,13 @@ OutputWaiter.prototype.setOutputInfo = function(length, lines, duration) { let width = length.toString().length; width = width < 4 ? 4 : width; - const lengthStr = Utils.pad(length.toString(), width, " ").replace(/ /g, " "); - const timeStr = Utils.pad(duration.toString() + "ms", width, " ").replace(/ /g, " "); + const lengthStr = length.toString().padStart(width, " ").replace(/ /g, " "); + const timeStr = (duration.toString() + "ms").padStart(width, " ").replace(/ /g, " "); let msg = "time: " + timeStr + "
length: " + lengthStr; if (typeof lines === "number") { - const linesStr = Utils.pad(lines.toString(), width, " ").replace(/ /g, " "); + const linesStr = lines.toString().padStart(width, " ").replace(/ /g, " "); msg += "
lines: " + linesStr; }