Fix escaping

This commit is contained in:
Roman Venediktov 2024-07-11 14:57:30 +02:00
parent 97a47f403f
commit 475ce90e9b

View file

@ -1284,13 +1284,33 @@ class env prg mode =
let n = String.length x in let n = String.length x in
let buf = Buffer.create (n * 2) in let buf = Buffer.create (n * 2) in
let rec iterate i = let rec iterate i =
if i < n then ( if i < n then
(match x.[i] with match x.[i] with
| '"' -> | '"' ->
Buffer.add_char buf '\\'; Buffer.add_char buf '\\';
Buffer.add_char buf '"' Buffer.add_char buf '"';
| c -> Buffer.add_char buf c); iterate (i + 1)
iterate (i + 1)) | '\\' -> (
if i + 1 >= n then (
Buffer.add_char buf '\\';
Buffer.add_char buf '\\')
else
match x.[i + 1] with
| 'n' ->
Buffer.add_char buf '\\';
Buffer.add_char buf 'n';
iterate (i + 2)
| 't' ->
Buffer.add_char buf '\\';
Buffer.add_char buf 't';
iterate (i + 2)
| _ ->
Buffer.add_char buf '\\';
Buffer.add_char buf '\\';
iterate (i + 1))
| c ->
Buffer.add_char buf c;
iterate (i + 1)
in in
iterate 0; iterate 0;
Buffer.contents buf Buffer.contents buf