Skip to content

Commit

Permalink
Escape string on save and put
Browse files Browse the repository at this point in the history
Strings with `\'` or `\"` gets double escaping: `\\'` and `\\"`
  • Loading branch information
murilopolese committed Nov 18, 2022
1 parent 8210ba0 commit d3b5920
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions micropython.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ function sleep(millis) {
})
}

function escape_string(string) {
string = string.replace(/"""/g, `\\"\\"\\"`)
string = string.replace(/\'/g, `\\'`)
string = string.replace(/\"/g, `\\"`)
return string
}

class MicroPythonBoard {
constructor() {
this.device = null
Expand Down Expand Up @@ -221,7 +228,7 @@ class MicroPythonBoard {
for (let i = 0; i < content.length; i+=128) {
let slice = content.slice(i, i+128)
slice = slice.toString()
slice = slice.replace(/"""/g, `\\"\\"\\"`)
slice = escape_string(slice)
await this.serial.write(`w("""${slice}""")`)
await this.serial.write(`\x04`)
await sleep(100)
Expand All @@ -243,8 +250,7 @@ class MicroPythonBoard {
for (let i = 0; i < content.length; i+=64) {
let slice = content.slice(i, i+64)
slice = slice.toString()
slice = slice.replace(/"""/g, `\\"\\"\\"`)
// slice = slice.replace(//g, ``)
slice = escape_string(slice)
await this.serial.write(`w("""${slice}""")\n`)
await this.serial.write(`\x04`)
await sleep(50)
Expand Down

0 comments on commit d3b5920

Please sign in to comment.