Skip to content

Commit 14c91d4

Browse files
committed
Changes
1 parent 162a249 commit 14c91d4

File tree

2 files changed

+16
-81
lines changed

2 files changed

+16
-81
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
## Unreleased
44

55
- The `bit_array` module gains the `inspect` function.
6+
- The base 64 encoding and decoding functions in the `bit_array` module no
7+
longer insert newlines in the output on JavaScript, making them consistent
8+
with the Erlang target.
69
- The `set` module gains the `difference` function.
710
- The deprecated `bit_string`, `bit_builder`, `base`, and `map` modules have
811
been removed.

src/gleam_stdlib.mjs

Lines changed: 13 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export function bit_array_to_string(bit_array) {
286286
try {
287287
const decoder = new TextDecoder("utf-8", { fatal: true });
288288
return new Ok(decoder.decode(bit_array.buffer));
289-
} catch (_error) {
289+
} catch {
290290
return new Error(Nil);
291291
}
292292
}
@@ -459,7 +459,7 @@ function unsafe_percent_decode(string) {
459459
export function percent_decode(string) {
460460
try {
461461
return new Ok(unsafe_percent_decode(string));
462-
} catch (_error) {
462+
} catch {
463463
return new Error(Nil);
464464
}
465465
}
@@ -477,94 +477,26 @@ export function parse_query(query) {
477477
pairs.push([unsafe_percent_decode(key), unsafe_percent_decode(value)]);
478478
}
479479
return new Ok(List.fromArray(pairs));
480-
} catch (_error) {
480+
} catch {
481481
return new Error(Nil);
482482
}
483483
}
484484

485-
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
485+
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64
486486
export function encode64(bit_array) {
487-
const aBytes = bit_array.buffer;
488-
let nMod3 = 2;
489-
let sB64Enc = "";
490-
491-
for (let nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
492-
nMod3 = nIdx % 3;
493-
if (nIdx > 0 && ((nIdx * 4) / 3) % 76 === 0) {
494-
sB64Enc += "\r\n";
495-
}
496-
nUint24 |= aBytes[nIdx] << ((16 >>> nMod3) & 24);
497-
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
498-
sB64Enc += String.fromCharCode(
499-
uint6ToB64((nUint24 >>> 18) & 63),
500-
uint6ToB64((nUint24 >>> 12) & 63),
501-
uint6ToB64((nUint24 >>> 6) & 63),
502-
uint6ToB64(nUint24 & 63),
503-
);
504-
nUint24 = 0;
505-
}
506-
}
507-
508-
return (
509-
sB64Enc.substr(0, sB64Enc.length - 2 + nMod3) +
510-
(nMod3 === 2 ? "" : nMod3 === 1 ? "=" : "==")
511-
);
487+
const binString = String.fromCodePoint(...bit_array.buffer);
488+
return btoa(binString);
512489
}
513490

514-
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
515-
function uint6ToB64(nUint6) {
516-
return nUint6 < 26
517-
? nUint6 + 65
518-
: nUint6 < 52
519-
? nUint6 + 71
520-
: nUint6 < 62
521-
? nUint6 - 4
522-
: nUint6 === 62
523-
? 43
524-
: nUint6 === 63
525-
? 47
526-
: 65;
527-
}
528-
529-
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
530-
function b64ToUint6(nChr) {
531-
return nChr > 64 && nChr < 91
532-
? nChr - 65
533-
: nChr > 96 && nChr < 123
534-
? nChr - 71
535-
: nChr > 47 && nChr < 58
536-
? nChr + 4
537-
: nChr === 43
538-
? 62
539-
: nChr === 47
540-
? 63
541-
: 0;
542-
}
543-
544-
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
491+
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64
545492
export function decode64(sBase64) {
546-
if (sBase64.match(/[^A-Za-z0-9\+\/=]/g)) return new Error(Nil);
547-
const sB64Enc = sBase64.replace(/=/g, "");
548-
const nInLen = sB64Enc.length;
549-
const nOutLen = (nInLen * 3 + 1) >> 2;
550-
const taBytes = new Uint8Array(nOutLen);
551-
552-
for (
553-
let nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0;
554-
nInIdx < nInLen;
555-
nInIdx++
556-
) {
557-
nMod4 = nInIdx & 3;
558-
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (6 * (3 - nMod4));
559-
if (nMod4 === 3 || nInLen - nInIdx === 1) {
560-
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
561-
taBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255;
562-
}
563-
nUint24 = 0;
564-
}
493+
try {
494+
const binString = atob(sBase64);
495+
const array = Uint8Array.from(binString, (c) => c.charCodeAt(0));
496+
return new Ok(new BitArray(array));
497+
} catch {
498+
return new Error(Nil);
565499
}
566-
567-
return new Ok(new BitArray(taBytes));
568500
}
569501

570502
export function classify_dynamic(data) {

0 commit comments

Comments
 (0)