open System.Text let escape_string (str : string) = let buf = StringBuilder(str.Length) let replaceOrLeave c = match c with | '\r' -> buf.Append "\\r" | '\n' -> buf.Append "\\n" | '\t' -> buf.Append "\\t" | '"' -> buf.Append "\\\"" | _ -> buf.Append c str.ToCharArray() |> Array.iter (replaceOrLeave >> ignore) buf.ToString()