8000 Escape string · arduino/micropython.js@df8be89 · GitHub
[go: up one dir, main page]

Skip to content

Commit df8be89

Browse files
committed
Escape string
1 parent 74b0af3 commit df8be89

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

micropython.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ function sleep(millis) {
1111
})
1212
}
1313

14+
function escape_string(string) {
15+
string = string.replace(/"""/g, `\\"\\"\\"`)
16+
string = string.replace(/\'/g, `\\'`)
17+
string = string.replace(/\"/g, `\\"`)
18+
return string
19+
}
20+
1421
class MicroPythonBoard {
1522
constructor() {
1623
this.device = null
@@ -244,7 +251,7 @@ class MicroPythonBoard {
244251
for (let i = 0; i < content.length; i+=128) {
245252
let slice = content.slice(i, i+128)
246253
slice = slice.toString()
247-
slice = slice.replace(/"""/g, `\\"\\"\\"`)
254+
slice = escape_string(slice)
248255
await this.serial.write(`w("""${slice}""")`)
249256
await this.serial.write(`\x04`)
250257
await sleep(100)
@@ -266,8 +273,7 @@ class MicroPythonBoard {
266273
for (let i = 0; i < content.length; i+=64) {
267274
let slice = content.slice(i, i+64)
268275
slice = slice.toString()
269-
slice = slice.replace(/"""/g, `\\"\\"\\"`)
270-
// slice = slice.replace(//g, ``)
276+
slice = escape_string(slice)
271277
await this.serial.write(`w("""${slice}""")\n`)
272278
await this.serial.write(`\x04`)
273279
await sleep(50)

0 commit comments

Comments
 (0)
0