Base85 improvements

This commit is contained in:
John L 2022-06-14 10:23:13 +01:00
parent 54fdc05e3a
commit 906727f133
5 changed files with 88 additions and 4 deletions

View file

@ -32,6 +32,11 @@ class FromBase85 extends Operation {
type: "editableOption",
value: ALPHABET_OPTIONS
},
{
name: "Remove non-alphabet chars",
type: "boolean",
value: true
},
];
}
@ -43,6 +48,7 @@ class FromBase85 extends Operation {
run(input, args) {
const alphabet = Utils.expandAlphRange(args[0]).join(""),
encoding = alphabetName(alphabet),
removeNonAlphChars = args[1],
result = [];
if (alphabet.length !== 85 ||
@ -50,6 +56,12 @@ class FromBase85 extends Operation {
throw new OperationError("Alphabet must be of length 85");
}
// Remove non-alphabet characters
if (removeNonAlphChars) {
const re = new RegExp("[^" + alphabet.replace(/[[\]\\\-^$]/g, "\\$&") + "]", "g");
input = input.replace(re, "");
}
if (input.length === 0) return [];
const matches = input.match(/<~(.+?)~>/);
@ -69,7 +81,7 @@ class FromBase85 extends Operation {
.map((chr, idx) => {
const digit = alphabet.indexOf(chr);
if (digit < 0 || digit > 84) {
throw `Invalid character '${chr}' at index ${idx}`;
throw `Invalid character '${chr}' at index ${i + idx}`;
}
return digit;
});