Skip to content

Commit 8c99ef9

Browse files
committed
cleanup: remove dead code (C) + hoist shared TextEncoder (JS)
- C: drop the unreachable fprintf/exit after load_map_from_embedded() (the embedded map is the guaranteed fallback; the error path could never run). - JS: allocate one module-level TextEncoder instead of 'new TextEncoder()' per character in the decode passthrough loops (audit dim 6/8).
1 parent 77c8b99 commit 8c99ef9

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

js/printable_binary.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
const isNodeEnv = typeof process !== 'undefined' && !!process.versions?.node;
1010
const isDenoEnv = typeof Deno !== 'undefined' && typeof Deno.readTextFileSync === 'function';
1111

12+
// A TextEncoder is stateless; share one instead of allocating per character.
13+
const sharedTextEncoder = new TextEncoder();
14+
1215
const CONTROL_NAMES = [
1316
'NUL','SOH','STX','ETX','EOT','ENQ','ACK','BEL',
1417
'BS','TAB','LF','VT','FF','CR','SO','SI',
@@ -363,7 +366,7 @@ class PrintableBinary {
363366
if (codePoint !== undefined) {
364367
// Encode the code point to UTF-8 bytes and add to result
365368
const char = String.fromCodePoint(codePoint);
366-
const encoder = new TextEncoder();
369+
const encoder = sharedTextEncoder;
367370
const bytes = encoder.encode(char);
368371
for (const byte of bytes) {
369372
result.push(byte);
@@ -550,7 +553,7 @@ class PrintableBinary {
550553
const codePoint = printableString.codePointAt(i);
551554
if (codePoint !== undefined) {
552555
const char = String.fromCodePoint(codePoint);
553-
const encoder = new TextEncoder();
556+
const encoder = sharedTextEncoder;
554557
const bytes = encoder.encode(char);
555558
for (const byte of bytes) {
556559
result.push(byte);
@@ -622,7 +625,7 @@ class PrintableBinary {
622625
* @returns {string} The encoded printable string
623626
*/
624627
encodeString(str, options = {}) {
625-
const encoder = new TextEncoder();
628+
const encoder = sharedTextEncoder;
626629
const bytes = encoder.encode(str);
627630
return this.encode(bytes, options);
628631
}

src/printable_binary.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,9 @@ static void load_character_map(const char *argv0) {
438438
return;
439439
}
440440

441+
/* The embedded map is the guaranteed final fallback (load_map_from_embedded
442+
* exits on a malformed embed), so reaching here always succeeds. */
441443
load_map_from_embedded();
442-
return;
443-
444-
fprintf(stderr, "Error: Unable to load character_map.txt. Set PRINTABLE_BINARY_MAP or place the file alongside the executable.\n");
445-
exit(1);
446444
}
447445

448446
static const char *ascii_name_for_byte(uint8_t value, char *buffer, size_t buffer_size) {

0 commit comments

Comments
 (0)