From: Boris Y. <bo...@ya...> - 2004-11-16 15:49:28
|
Hi, I've started using IO for one of my projects, but I've found it to lack a Printf.bprintf-like function, which would permit to use "%a" formats more easily. For example, Xml printing can be written as let rec print_xml o xml = function | Text t -> nwrite o t | Tag (name, args, xml) -> bprintf "<%s%a>%a</%s> name print_args args print_lxml xml s and print_args o args = ... and print_lxml o args = ... With the current printf function, you have no such syntax. I propose to add this function, based on the existing code of bprintf in the ocaml distribution: val bprintf : 'a output -> ('b, 'a output, unit, unit) format4 -> 'b let rec bprintf dest fmt = let fmt = string_of_format fmt in let len = String.length fmt in let rec doprn i = if i >= len then Obj.magic () else match String.unsafe_get fmt i with | '%' -> Printf.scan_format fmt i cont_s cont_a cont_t cont_f cont_m | c -> write dest c; doprn (i+1) and cont_s s i = nwrite dest s; doprn i and cont_a printer arg i = printer dest arg; doprn i and cont_t printer i = printer dest; doprn i and cont_f i = doprn i and cont_m sfmt i = bprintf dest sfmt; doprn i in doprn 0 -- Boris [please CC-me as I'm not subscribed] |