How to print sexp with non-ASCII characters?

I’m using [@@deriving sexp_of] and I’m not sure how to handle the logging of non-ASCII characters.

How can you print a s-expression with non-ASCII characters?

running this in utop:

#require "Base" ;;
let e = "é";;
let s = sexp_of_string e;;
let z = Sexp.to_string s;;
print_endline z;;

results in:

utop # let e = "é";;
val e : string/2 = "é"

utop # let s = sexp_of_string e;;
val s : Sexp.t = Sexplib0.Sexp.Atom "é"

utop # let z = Sexp.to_string s;;
val z : string/2 = "\"\\195\\169\""

print_endline z;;

so Sexp.to_string results in an escaped UTF8 encoding of the string. But how can you get the decoded value: é?

From memory, sexplib will always escape any non-ASCII characters when converting s-expressions into strings.

You can always write your own function to convert s-expressions into strings that does not escape UTF-8 valid atoms and use that instead.


1 Like

thanks, I thought it is a common use case to print non-english characters, so I was hoping that there is an existing solution

1 Like

Scanf.unescaped converts an escaped string into the value it would represent as a literal, although in this case you need to deal with the quotation marks because "\"\195\169\"" would correspond to using ""é"" as a literal (double quoting intended).