From 7db1f394731c803c0b7f3c9c1555dc8b9bdf710e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Fri, 17 Sep 2021 08:32:08 +0200 Subject: [PATCH] base45: Implement highlighting --- src/core/lib/Base45.mjs | 27 +++++++++++++++++++++++++++ src/core/operations/FromBase45.mjs | 7 +++++-- src/core/operations/ToBase45.mjs | 7 +++++-- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/core/lib/Base45.mjs diff --git a/src/core/lib/Base45.mjs b/src/core/lib/Base45.mjs new file mode 100644 index 00000000..d3e279fa --- /dev/null +++ b/src/core/lib/Base45.mjs @@ -0,0 +1,27 @@ +/** + * Base45 resources. + * + * @author Thomas Weißschuh [thomas@t-8ch.de] + * @copyright Crown Copyright 2021 + * @license Apache-2.0 + */ + +/** + * Highlight to Base45 + */ +export function highlightToBase45(pos, args) { + pos[0].start = Math.floor(pos[0].start / 2) * 3; + pos[0].end = Math.ceil(pos[0].end / 2) * 3; + return pos; +} + +/** + * Highlight from Base45 + */ +export function highlightFromBase45(pos, args) { + pos[0].start = Math.floor(pos[0].start / 3) * 2; + pos[0].end = Math.ceil(pos[0].end / 3) * 2; + return pos; +} + +export const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; diff --git a/src/core/operations/FromBase45.mjs b/src/core/operations/FromBase45.mjs index 8e78ed0c..890d35d6 100644 --- a/src/core/operations/FromBase45.mjs +++ b/src/core/operations/FromBase45.mjs @@ -4,6 +4,7 @@ * @license Apache-2.0 */ +import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs"; import Operation from "../Operation.mjs"; import OperationError from "../errors/OperationError.mjs"; import Utils from "../Utils.mjs"; @@ -26,6 +27,9 @@ class FromBase45 extends Operation { this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems"; this.inputType = "string"; this.outputType = "byteArray"; + + this.highlight = highlightFromBase45; + this.highlightReverse = highlightToBase45; } /** @@ -34,7 +38,6 @@ class FromBase45 extends Operation { * @returns {byteArray} */ run(input, args) { - const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join(""); if (!input) return []; const res = []; @@ -43,7 +46,7 @@ class FromBase45 extends Operation { triple.reverse(); let b = 0; for (const c of triple) { - const idx = alphabet.indexOf(c); + const idx = ALPHABET.indexOf(c); if (idx === -1) { throw new OperationError(`Character not in alphabet: '${c}'`); } diff --git a/src/core/operations/ToBase45.mjs b/src/core/operations/ToBase45.mjs index 019c3dd9..d7455dca 100644 --- a/src/core/operations/ToBase45.mjs +++ b/src/core/operations/ToBase45.mjs @@ -4,6 +4,7 @@ * @license Apache-2.0 */ +import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs"; import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; @@ -31,6 +32,9 @@ class ToBase45 extends Operation { value: "0-9A-Za-z" } ]; + + this.highlight = highlightToBase45; + this.highlightReverse = highlightFromBase45; } /** @@ -39,7 +43,6 @@ class ToBase45 extends Operation { * @returns {string} */ run(input, args) { - const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join(""); input = new Uint8Array(input); if (!input) return ""; @@ -54,7 +57,7 @@ class ToBase45 extends Operation { let chars = 0; do { - res.push(alphabet[b % 45]); + res.push(ALPHABET[b % 45]); chars++; b = Math.floor(b / 45); } while (b > 0);