let safe_string s =
if s = ""
then "\"\""
else if (
try match s.[0] with
| 'a'..'z' | 'A'..'Z' ->
for i = 1 to String.length s - 1 do
match s.[i] with
'a'..'z' | 'A'..'Z' | '_' | '0'..'9' -> ()
| _ -> raise Unsafe_string
done;
false
| _ ->
try
string_of_int (int_of_string s) <> s ||
string_of_float (float_of_string s) <> s
with Failure "int_of_string" | Failure "float_of_string" -> true
with Unsafe_string -> true)
then Printf.sprintf "\"%s\"" (String.escaped s)
else s