@@ -3,23 +3,37 @@ import type { FileHandle } from "fs/promises";
3
3
4
4
/**
5
5
* Replace simple escape sequences in a string with their respective characters.
6
+ * (only escape sequences used by the python bytes object)
6
7
*
7
8
* @param str The string to process.
8
9
* @returns The string with all simple escape sequences replaced.
9
10
*/
10
11
function replaceSimpleEscapeSequences ( str : string ) : string {
11
12
// 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
+ ) ;
23
37
}
24
38
25
39
/**
@@ -66,9 +80,9 @@ export async function writeEncodedBufferToFile(
66
80
waitingForHex = true ;
67
81
} else if ( byte === 92 ) {
68
82
// 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 ;
72
86
} else {
73
87
// ok, false alarm, put everything into file and reset cache
74
88
await fileHandle . write ( Buffer . from ( cache ) ) ;
@@ -100,17 +114,28 @@ export async function writeEncodedBufferToFile(
100
114
* @returns The string with all simple escape sequences replaced.
101
115
*/
102
116
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
+ ) ;
114
139
}
115
140
116
141
/**
0 commit comments