#2355 - fix ToBase32 operation breaking UTF-16 surrogate pairs in custom alphabets
This commit is contained in:
parent
d8aec9c43f
commit
ab7e5006af
@ -43,11 +43,19 @@ class ToBase32 extends Operation {
|
|||||||
if (!input) return "";
|
if (!input) return "";
|
||||||
input = new Uint8Array(input);
|
input = new Uint8Array(input);
|
||||||
|
|
||||||
const alphabet = args[0] ? Utils.expandAlphRange(args[0]).join("") : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
|
const alphabet = args[0]
|
||||||
|
? Utils.expandAlphRange(args[0]).join("")
|
||||||
|
: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
|
||||||
|
|
||||||
|
// Unicode-safe alphabet handling
|
||||||
|
// Supports BMP + non-BMP characters (emoji, Mahjong tiles, etc.)
|
||||||
|
const alphabetChars = Array.from(alphabet);
|
||||||
|
|
||||||
let output = "",
|
let output = "",
|
||||||
chr1, chr2, chr3, chr4, chr5,
|
chr1, chr2, chr3, chr4, chr5,
|
||||||
enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8,
|
enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8,
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
while (i < input.length) {
|
while (i < input.length) {
|
||||||
chr1 = input[i++];
|
chr1 = input[i++];
|
||||||
chr2 = input[i++];
|
chr2 = input[i++];
|
||||||
@ -74,10 +82,19 @@ class ToBase32 extends Operation {
|
|||||||
enc8 = 32;
|
enc8 = 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
output += alphabet.charAt(enc1) + alphabet.charAt(enc2) + alphabet.charAt(enc3) +
|
// Preserve original charAt() behavior:
|
||||||
alphabet.charAt(enc4) + alphabet.charAt(enc5) + alphabet.charAt(enc6) +
|
// out-of-range indexes return ""
|
||||||
alphabet.charAt(enc7) + alphabet.charAt(enc8);
|
output +=
|
||||||
|
(alphabetChars[enc1] || "") +
|
||||||
|
(alphabetChars[enc2] || "") +
|
||||||
|
(alphabetChars[enc3] || "") +
|
||||||
|
(alphabetChars[enc4] || "") +
|
||||||
|
(alphabetChars[enc5] || "") +
|
||||||
|
(alphabetChars[enc6] || "") +
|
||||||
|
(alphabetChars[enc7] || "") +
|
||||||
|
(alphabetChars[enc8] || "");
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user