Skip to content

Commit 1489cf6

Browse files
committed
Fix #1, Intentional escape sequences are replaced
Signed-off-by: paulober <44974737+paulober@users.noreply.github.com>
1 parent 90324a3 commit 1489cf6

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

src/escapeCoder.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,37 @@ import type { FileHandle } from "fs/promises";
33

44
/**
55
* Replace simple escape sequences in a string with their respective characters.
6+
* (only escape sequences used by the python bytes object)
67
*
78
* @param str The string to process.
89
* @returns The string with all simple escape sequences replaced.
910
*/
1011
function replaceSimpleEscapeSequences(str: string): string {
1112
// TODO: single pass maybe loop or match any \any and then replace with lambda
12-
return str
13-
.replace(/\\'/g, "'")
14-
.replace(/\\"/g, '"')
15-
.replace(/\\a/g, "\x07") // Bell/alert
16-
.replace(/\\b/g, "\b") // Backspace
17-
.replace(/\\f/g, "\f") // Form feed
18-
.replace(/\\n/g, "\n") // New line
19-
.replace(/\\r/g, "\r") // Carriage return
20-
.replace(/\\t/g, "\t") // Tab
21-
.replace(/\\v/g, "\v") // Vertical tab
22-
.replace(/\\\\/g, "\\"); // Backslash
13+
return (
14+
str // TODO: support that there is no extra backslashes in front of the escape sequences
15+
.replace(/\\'/g, "'")
16+
.replace(/\\"/g, '"')
17+
.replace(/\\a/g, "\x07") // Bell/alert
18+
// eslint-disable-next-line no-control-regex
19+
.replace(/\\\x07/g, "\\a") // Intentional escape sequence
20+
// DOES HAVE MANY ISSUES
21+
//.replace(/\\b/g, "\b") // Backspace
22+
.replace(/\\f/g, "\f") // Form feed
23+
.replace(/\\\f/g, "\\f") // Intentional escape sequence
24+
.replace(/\\n/g, "\n") // New line
25+
.replace(/\\\n/g, "\\n") // Intentional escape sequence
26+
.replace(/\\r/g, "\r") // Carriage return
27+
.replace(/\\\r/g, "\\r") // Intentional escape sequence
28+
.replace(/\\t/g, "\t") // Tab
29+
.replace(/\\\t/g, "\\t") // Intentional escape sequence
30+
.replace(/\\v/g, "\v") // Vertical tab
31+
.replace(/\\\v/g, "\\v") // Intentional escape sequence
32+
// don't do this as escaped backslashes are handled also
33+
// below where if \ after \ it will only print one
34+
// and also they are needed to identify intentional escape sequences by the user below
35+
//.replace(/\\\\/g, "\\") // Backslash
36+
);
2337
}
2438

2539
/**
@@ -66,9 +80,9 @@ export async function writeEncodedBufferToFile(
6680
waitingForHex = true;
6781
} else if (byte === 92) {
6882
// second backslash,
69-
// mean first one was false alarm, write one and keep one in cache
70-
// because it could be the the second one is now starting an escape sequence
71-
await fileHandle.write(Buffer.from([byte]));
83+
// means first one was false alarm, write one as the first one escaped the second one
84+
await fileHandle.write(Buffer.from(cache));
85+
cache.length = 0;
7286
} else {
7387
// ok, false alarm, put everything into file and reset cache
7488
await fileHandle.write(Buffer.from(cache));
@@ -100,17 +114,28 @@ export async function writeEncodedBufferToFile(
100114
* @returns The string with all simple escape sequences replaced.
101115
*/
102116
function encodeSimpleEscapeSequences(str: string): string {
103-
return str
104-
.replace(/\\/g, "\\\\") // Backslash
105-
.replace(/\n/g, "\\n") // New line
106-
.replace(/\t/g, "\\t") // Tab
107-
.replace(/\r/g, "\\r") // Carriage return
108-
.replace(/\f/g, "\\f") // Form feed
109-
.replace("\b", "\\b") // Backspace
110-
.replace(/\v/g, "\\v") // Vertical tab
111-
.replace("\x07", "\\a") // Bell/alert
112-
.replace(/'/g, "\\'") // Single quote
113-
.replace(/"/g, '\\"'); // Double quote
117+
return (
118+
str
119+
// not required
120+
//.replace(/\\n/g, "\\\\n") // New line esc sequence by the user
121+
//.replace(/\\t/g, "\\\\t") // Tab esc sequence by the user
122+
//.replace(/\\r/g, "\\\\r") // Carriage return esc sequence by the user
123+
//.replace(/\\f/g, "\\\\f") // Form feed esc sequence by the user
124+
//.replace(/\\b/g, "\\\\b") // Backspace esc sequence by the user
125+
//.replace(/\\v/g, "\\\\v") // Vertical tab esc sequence by the user
126+
//.replace("\\x07", "\\\\x07") // Bell/alert esc sequence by the user
127+
//.replace(/\\/g, "\\\\") // Backslash
128+
129+
.replace(/\n/g, "\\n") // New line
130+
.replace(/\t/g, "\\t") // Tab
131+
.replace(/\r/g, "\\r") // Carriage return
132+
.replace(/\f/g, "\\f") // Form feed
133+
.replace("\b", "\\b") // Backspace
134+
.replace(/\v/g, "\\v") // Vertical tab
135+
.replace("\x07", "\\a") // Bell/alert
136+
.replace(/'/g, "\\'") // Single quote
137+
.replace(/"/g, '\\"') // Double quote
138+
);
114139
}
115140

116141
/**

0 commit comments

Comments
 (0)