Get result in string variable in OCaml

I have this function that prints a value in an offset map:

let pretty_offsetmap_original lv fmt offsetmap = begin match offsetmap with | None -> Format.fprintf fmt "<BOTTOM>" | Some off -> let typ = Some (typeOfLval lv) in Format.fprintf fmt "%a%a" pretty_lval_or_absolute lv (Cvalue.V_Offsetmap.pretty_typ typ) off end 

Now I would like to get the value in a string variable in order to convert it for my purpose. I replaced Format.fprintf fmt with Printf.sprintf , but it does not work. Compilation Error:

 Error: This expression has type Format.formatter -> Cvalue.V_Offsetmap.t -> unit but an expression was expected of type unit -> 'a -> string 
+4
source share
2 answers

Unfortunately, you are right: Format.sprintf not of a good type. Inside Frama-C, the Pretty_utils.sfprintf function will do exactly what you need. You can also see Pretty_utils.to_string .

+4
source

It looks like you need to replace Format.fprintf with Format.sprintf not Printf.sprintf .

+3
source

Source: https://habr.com/ru/post/1489370/


All Articles